Commit b454c410 authored by Cordelya Sharpe's avatar Cordelya Sharpe
Browse files

adjustment to main loop to not clobber CO2 sensor w/ requests

changed LED device from Pimoroni LED SHIM to Sparkfun Qwiic LED Stick
parent c44c0d2e
......@@ -15,7 +15,7 @@ import os
import sys
import getopt
import pygame
import ledshim
import qwiic_led_stick as ledstick
from ft5406 import Touchscreen, TS_PRESS, TS_RELEASE, TS_MOVE
debug = False
......@@ -26,35 +26,27 @@ filename = True
grafana = False # grafana logging set to false by default. To turn on, pass a location using -l [location] when starting this script
logfile_path = "/home/pi/Apps/circuitpython/sd/" # set this to the full path of where you want logfiles to live
# init the LED SHIM because we'll use it to indicate load progress
ledshim.set_clear_on_exit()
# init the LED STICK because we'll use it to indicate load progress
ledstick = ledstick.QwiicLEDStick()
step = 7
def led_next(step):
color = step % 7
if color == 0:
ledshim.set_all(209, 0, 0)
ledshim.show()
ledstick.set_all_LED_color(209, 0, 0)
elif color == 1:
ledshim.set_all(255, 102, 34)
ledshim.show()
ledstick.set_all_LED_color(255, 102, 34)
elif color == 2:
ledshim.set_all(255, 218, 33)
ledshim.show()
ledstick.set_all_LED_color(255, 218, 33)
elif color == 3:
ledshim.set_all(51, 221, 0)
ledshim.show()
ledstick.set_all_LED_color(51, 221, 0)
elif color == 4:
ledshim.set_all(17, 51,204)
ledshim.show()
ledstick.set_all_LED_color(17, 51,204)
elif color == 5:
ledshim.set_all(34, 0, 102)
ledshim.show()
ledstick.set_all_LED_color(34, 0, 102)
elif color == 6:
ledshim.set_all(51, 0, 68)
ledshim.show()
ledstick.set_all_LED_color(51, 0, 68)
else:
ledshim.set_all(255, 255, 255)
ledshim.show()
ledstick.set_all_LED_color(255, 255, 255)
return
led_next(step)
......@@ -328,94 +320,159 @@ led_next(step)
while True:
#if debug == True:
#print("Successfully entered the main loop...")
# wait for the sensor to report that it has a reading, then store the readings into local variables
if sensor.data_available and (int(time.monotonic()) - time_last) >= DATA_REFRESH:
if debug == True:
print("Successfully met conditions for taking a reading.")
# convert the temperature reading to F, and trim to 1 decimal place
temperature = round(((sensor.temperature - temperature_delta)* 9 / 5) + 32, 1)
co2 = sensor.CO2 # store the CO2 reading
humidity = round(sensor.relative_humidity, 1) # store the rH reading and trim to 1 decimal place
try:
pressure = pressure_sen.pressure
except:
print("Could not update the pressure")
finally:
sensor.pressure = pressure
temperature = round((pressure_sen.temperature * 9 / 5) + 32, 1)
if debug == True:
print("Successfully updated pressure")
# determine what color we should use based on CO2 concentration
if co2 > CO2_HIGH:
text_color = red
ledshim.set_all(red[0], red[1], red[2])
ledshim.show()
if pygame.mixer.Sound.get_num_channels(red_alarm) < 1:
if pygame.mixer.Sound.get_num_channels(yellow_alarm) > 0:
pygame.mixer.Sound.fadeout(yellow_alarm, 1000)
pygame.mixer.Sound.play(red_alarm, loops=-1, fade_ms=1000)
elif co2 > CO2_LOW:
text_color = yellow
ledshim.set_all(yellow[0], yellow[1], yellow[2])
ledshim.show()
if pygame.mixer.Sound.get_num_channels(yellow_alarm) < 1:
if pygame.mixer.Sound.get_num_channels(red_alarm) > 0:
pygame.mixer.Sound.fadeout(red_alarm, 1000)
pygame.mixer.Sound.play(yellow_alarm, loops=-1, fade_ms=1000)
else:
text_color = green
ledshim.set_all(green[0], green[1], green[2])
ledshim.show()
if pygame.mixer.Sound.get_num_channels(yellow_alarm) > 0:
pygame.mixer.Sound.fadeout(yellow_alarm, 1000)
if pygame.mixer.Sound.get_num_channels(red_alarm) > 0:
pygame.mixer.Sound.fadeout(red_alarm, 1000)
# redraw the screen
screen.fill(pygame.Color("black"))
co2_text = str(int(co2)) # update the co2_text label with the fresh data
co2_label_width = big_font.size(co2_text)
co2_label = big_font.render(co2_text, True, text_color)
screen.blit(co2_label, (DISPLAY_WIDTH / 2 - co2_label_width[0] / 2, (DISPLAY_HEIGHT - co2_label_width[1]) * 0.15))
ppm_label = ppm_font.render(ppm, True, text_color)
ppm_label_width = ppm_font.size(ppm)
screen.blit(ppm_label, (DISPLAY_WIDTH / 2 - ppm_label_width[0] / 2, (DISPLAY_HEIGHT - ppm_label_width[1]) * 0.7))
other_text = '%0.1f°F | %0.1f%%rH | %0.1f mbar' % (temperature, humidity, pressure) # update the temp/rH label
other_label = little_font.render(other_text, True, text_color)
other_text_width = little_font.size(other_text)
screen.blit(other_label, (DISPLAY_WIDTH / 2 - other_text_width[0] / 2, (DISPLAY_HEIGHT - other_text_width[1]) * 0.95))
pygame.display.update()
if rtc_time != False:
current = (time.monotonic())
seconds = (current - time_delta)
date_time = (time.mktime(rtc_time) + int(seconds))
t = time.localtime(date_time)
else:
date_time = time.monotonic()
if pressure_sen:
pressure = pressure_sen.pressure
if logging == True:
datestring = '%04d-%02d-%02d_%02d:%02d:%02d' % (t.tm_year, t.tm_mon, t.tm_mday, t.tm_hour, t.tm_min, t.tm_sec)
with open(filename, "a") as f:
f.write(str(datestring) + "," + str(co2) + "," + str(temperature) + "," + str(humidity) + "," + str(pressure) +"\n")
try:
if (int(time.monotonic()) - time_last) >= DATA_REFRESH:
if sensor.data_available:
if debug == True:
print(datestring, " ", co2, " ", temperature, " ", humidity, " ", pressure, " written to log")
if grafana == True: # epoch_time, metric, location, reading
epoch_time = time.mktime(time.localtime())
with open(g_filename, "a") as f:
f.write(str(int(epoch_time)) + ",co2," + location + "," + str(round(co2, 3)) + "\n")
f.write(str(int(epoch_time)) + ",temperature," + location + "," + str(round(temperature, 3)) + "\n")
f.write(str(int(epoch_time)) + ",humidity," + location + "," + str(round(humidity, 3)) + "\n")
f.write(str(int(epoch_time)) + ",pressure," + location + "," + str(round(pressure, 3)) + "\n")
print("Successfully met conditions for taking a reading.")
time.sleep(1)
# convert the temperature reading to F, and trim to 1 decimal place
temperature = round(((sensor.temperature - temperature_delta)* 9 / 5) + 32, 1)
co2 = sensor.CO2 # store the CO2 reading
humidity = round(sensor.relative_humidity, 1) # store the rH reading and trim to 1 decimal place
try:
pressure = pressure_sen.pressure
except:
exception_type, exception_object, exception_traceback = sys.exc_info()
error_file = exception_traceback.tb_frame.f_code.co_filename
line_number = exception_traceback.tb_lineno
print("I had a problem updating the pressure")
print("Exception Type: ", exception_type)
print("File name: ", error_file)
print("Line: ", line_number)
else:
if int(time.monotonic()) - time_last > 60:
sensor.pressure = pressure
if debug == True:
print("Updated the CO2 Sensor with current pressure reading")
temperature = round((pressure_sen.temperature * 9 / 5) + 32, 1)
if debug == True:
print(int(epoch_time), " ", location, " ", round(co2, 3), " ", round(temperature, 3), " ", round(humidity, 3), " ", round(pressure, 3), " written to Grafana log")
time_last = int(time.monotonic())
print("Successfully updated pressure and temperature reading")
# determine what color we should use based on CO2 concentration
try:
if co2 > CO2_HIGH:
text_color = red
ledstick.set_all_LED_color(red[0], red[1], red[2])
if pygame.mixer.Sound.get_num_channels(red_alarm) < 1:
if pygame.mixer.Sound.get_num_channels(yellow_alarm) > 0:
pygame.mixer.Sound.fadeout(yellow_alarm, 1000)
pygame.mixer.Sound.play(red_alarm, loops=-1, fade_ms=1000)
elif co2 > CO2_LOW:
text_color = yellow
ledstick.set_all_LED_color(yellow[0], yellow[1], yellow[2])
if pygame.mixer.Sound.get_num_channels(yellow_alarm) < 1:
if pygame.mixer.Sound.get_num_channels(red_alarm) > 0:
pygame.mixer.Sound.fadeout(red_alarm, 1000)
pygame.mixer.Sound.play(yellow_alarm, loops=-1, fade_ms=1000)
else:
text_color = green
ledstick.set_all_LED_color(green[0], green[1], green[2])
if pygame.mixer.Sound.get_num_channels(yellow_alarm) > 0:
pygame.mixer.Sound.fadeout(yellow_alarm, 1000)
if pygame.mixer.Sound.get_num_channels(red_alarm) > 0:
pygame.mixer.Sound.fadeout(red_alarm, 1000)
except:
exception_type, exception_object, exception_traceback = sys.exc_info()
error_file = exception_traceback.tb_frame.f_code.co_filename
line_number = exception_traceback.tb_lineno
print("Nope!")
print("Exception: ", exception_type)
print("File: ", error_file)
print("Line: ", line_number)
# redraw the screen
try:
screen.fill(pygame.Color("black"))
co2_text = str(int(co2)) # update the co2_text label with the fresh data
co2_label_width = big_font.size(co2_text)
co2_label = big_font.render(co2_text, True, text_color)
screen.blit(co2_label, (DISPLAY_WIDTH / 2 - co2_label_width[0] / 2, (DISPLAY_HEIGHT - co2_label_width[1]) * 0.15))
ppm_label = ppm_font.render(ppm, True, text_color)
ppm_label_width = ppm_font.size(ppm)
screen.blit(ppm_label, (DISPLAY_WIDTH / 2 - ppm_label_width[0] / 2, (DISPLAY_HEIGHT - ppm_label_width[1]) * 0.7))
other_text = '%0.1f°F | %0.1f%%rH | %0.1f mbar' % (temperature, humidity, pressure) # update the temp/rH label
other_label = little_font.render(other_text, True, text_color)
other_text_width = little_font.size(other_text)
screen.blit(other_label, (DISPLAY_WIDTH / 2 - other_text_width[0] / 2, (DISPLAY_HEIGHT - other_text_width[1]) * 0.95))
pygame.display.update()
except:
exception_type, exception_object, exception_traceback = sys.exc_info()
error_file = exception_traceback.tb_frame.f_code.co_filename
line_number = exception_traceback.tb_lineno
print("Nope!")
print("Exception: ", exception_type)
print("File: ", error_file)
print("Line: ", line_number)
if rtc_time != False:
try:
current = (time.monotonic())
seconds = (current - time_delta)
date_time = (time.mktime(rtc_time) + int(seconds))
t = time.localtime(date_time)
except:
exception_type, exception_object, exception_traceback = sys.exc_info()
error_file = exception_traceback.tb_frame.f_code.co_filename
line_number = exception_traceback.tb_lineno
print("Nope!")
print("Exception: ", exception_type)
print("File: ", error_file)
print("Line: ", line_number)
else:
date_time = time.monotonic()
if pressure_sen:
pressure = pressure_sen.pressure
if logging == True:
try:
datestring = '%04d-%02d-%02d_%02d:%02d:%02d' % (t.tm_year, t.tm_mon, t.tm_mday, t.tm_hour, t.tm_min, t.tm_sec)
with open(filename, "a") as f:
f.write(str(datestring) + "," + str(co2) + "," + str(temperature) + "," + str(humidity) + "," + str(pressure) +"\n")
if debug == True:
print(datestring, " ", co2, " ", temperature, " ", humidity, " ", pressure, " written to log")
if grafana == True: # epoch_time, metric, location, reading
epoch_time = time.mktime(time.localtime())
with open(g_filename, "a") as f:
f.write(str(int(epoch_time)) + ",co2," + location + "," + str(round(co2, 3)) + "\n")
f.write(str(int(epoch_time)) + ",temperature," + location + "," + str(round(temperature, 3)) + "\n")
f.write(str(int(epoch_time)) + ",humidity," + location + "," + str(round(humidity, 3)) + "\n")
f.write(str(int(epoch_time)) + ",pressure," + location + "," + str(round(pressure, 3)) + "\n")
if debug == True:
print(int(epoch_time), " ", location, " ", round(co2, 3), " ", round(temperature, 3), " ", round(humidity, 3), " ", round(pressure, 3), " written to Grafana log")
except:
exception_type, exception_object, exception_traceback = sys.exc_info()
error_file = exception_traceback.tb_frame.f_code.co_filename
line_number = exception_traceback.tb_lineno
print("Nope!")
print("Exception: ", exception_type)
print("File: ", error_file)
print("Line: ", line_number)
time_last = int(time.monotonic())
time.sleep(1)
except OSError:
exception_type, exception_object, exception_traceback = sys.exc_info()
error_file = exception_traceback.tb_frame.f_code.co_filename
line_number = exception_traceback.tb_lineno
print("Hmmm...")
print("Exception: ", exception_type)
print("File: ", error_file)
print("Line: ", line_number)
except Exception:
exception_type, exception_object, exception_traceback = sys.exc_info()
error_file = exception_traceback.tb_frame.f_code.co_filename
line_number = exception_traceback.tb_lineno
print("What.")
print("Exception: ", exception_type)
print("File: ", error_file)
print("Line: ", line_number)
if repeat == False:
ledstick.LED_off()
break
else:
continue
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment