Last active
August 20, 2020 22:56
-
-
Save jameseleach/ef6d16232976899146fc00fba5e86384 to your computer and use it in GitHub Desktop.
Temp and Humidity Sensor for the Adafruit Clue
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""Monitor customisable temperature and humidity ranges, with an optional audible alarm tone.""" | |
# Modification to allow changing between degrees Fahrenheit and Celsius | |
# Original source: | |
# https://github.com/adafruit/Adafruit_CircuitPython_CLUE/blob/master/examples/clue_temperature_humidity_monitor.py | |
from adafruit_clue import clue | |
from time import sleep | |
# Set desired temperature range in degrees Celsius. | |
min_temperature = 24 | |
max_temperature = 30 | |
# Set desired humidity range in percent. | |
min_humidity = 20 | |
max_humidity = 65 | |
# Set to true to enable audible alarm tone. | |
alarm_enable = False | |
# Default temperature scale | |
temp_scale = ["c", "f"] | |
clue_display = clue.simple_text_display(text_scale=3, colors=(clue.WHITE,)) | |
allow_user_input = True | |
while True: | |
if alarm_enable: | |
clue_display[0].text = " Temp & *" | |
else: | |
clue_display[0].text = " Temp & " | |
clue_display[1].text = " Humidity " | |
alarm = False | |
temperature_c = clue.temperature | |
temperature_f = (clue.temperature * 9 / 5) + 32 | |
humidity = clue.humidity | |
click = clue.were_pressed | |
if allow_user_input and click: | |
allow_user_input = False | |
if 'A' in click: | |
print("Button A detected") | |
# toggle temperature scale from C to F / F to C | |
temp_scale[0], temp_scale[1] = temp_scale[1], temp_scale[0] | |
if 'B' in click: | |
print("Button B detected") | |
# toggle alarm_enable | |
alarm_enable = not alarm_enable | |
if temp_scale[0] == "f": | |
clue_display[3].text = "Temp: {:.1f} F".format(temperature_f) | |
else: | |
clue_display[3].text = "Temp: {:.1f} C".format(temperature_c) | |
clue_display[5].text = "Humi: {:.1f} %".format(humidity) | |
if temperature_c < min_temperature: | |
clue_display[3].color = clue.BLUE | |
alarm = True | |
elif temperature_c > max_temperature: | |
clue_display[3].color = clue.RED | |
alarm = True | |
else: | |
clue_display[3].color = clue.WHITE | |
if humidity < min_humidity: | |
clue_display[5].color = clue.BLUE | |
alarm = True | |
elif humidity > max_humidity: | |
clue_display[5].color = clue.RED | |
alarm = True | |
else: | |
clue_display[5].color = clue.WHITE | |
clue_display.show() | |
if alarm and alarm_enable: | |
clue.start_tone(2000) | |
else: | |
clue.stop_tone() | |
if not allow_user_input: | |
if clue.button_a or clue.button_b: | |
print("lay off the buttons!") | |
else: | |
print("no buttons detected") | |
discard = clue.were_pressed | |
allow_user_input = True |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment