Last active
November 9, 2022 03:50
-
-
Save psifertex/b7c72a5e88ab34746a3de6b99cdc1e55 to your computer and use it in GitHub Desktop.
wip co2 sensor based on a bunch of adafruit stemma qt compatible boards
This file contains 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
import time | |
import sys | |
import board | |
import neopixel | |
import busio | |
import digitalio | |
import displayio | |
import terminalio | |
from adafruit_display_text import label | |
from adafruit_displayio_sh1107 import SH1107, DISPLAY_OFFSET_ADAFRUIT_FEATHERWING_OLED_4650 | |
from adafruit_bme280 import basic as adafruit_bme280 | |
import adafruit_scd4x | |
DELAY = 5 # Must be >= 5 | |
GOODCO2 = 400 | |
BADCO2 = 1100 | |
RGCOLORBLIND = False | |
displayio.release_displays() | |
i2c = board.I2C() | |
def debug(msg): | |
print(msg,end='') | |
try: | |
display_bus = displayio.I2CDisplay(i2c, device_address=0x3C) | |
display = SH1107(display_bus, width=128, height=64) | |
debug("Found display\n") | |
except: | |
display = None | |
pixels = neopixel.NeoPixel(board.NEOPIXEL, 1) | |
#i2c = busio.I2C(board.SCL, board.SDA, frequency=100000) | |
temp = True | |
pres = True | |
try: | |
bme280 = adafruit_bme280.Adafruit_BME280_I2C(i2c) | |
debug("Found temperature sensor\n") | |
except: | |
temp = False | |
try: | |
scd40 = adafruit_scd4x.SCD4X(i2c) | |
debug("Found CO2 sensor\n") | |
# debug("Serial number: %s" % repr([hex(i) for i in scd40.serial_number]))) | |
scd40.start_periodic_measurement() | |
debug("Requesting intial CO2 measurement\n") | |
except: | |
pres = False | |
button = digitalio.DigitalInOut(board.BUTTON) | |
button.switch_to_input(pull=digitalio.Pull.UP) | |
def reportstuff(): | |
fillcolor = (0, 0, 255) | |
scale = 0 | |
if temp: | |
temperature = (bme280.temperature * 1.8 + 32) | |
relative_humidity = bme280.relative_humidity | |
pressure = bme280.pressure | |
altitude = bme280.altitude | |
co2 = 0 | |
if pres: | |
if scd40.data_ready: | |
co2 = scd40.CO2 | |
#scale 600-1600 to 0-127 | |
scale = min(max((co2 - GOODCO2) / (BADCO2 - GOODCO2) * 255, 0), 255) | |
if RGCOLORBLIND: | |
fillcolor = (scale, 0, 255 - scale) | |
else: | |
fillcolor = (scale, 255 - scale, 0) | |
if not temp: | |
temperature = (scd40.temperature * 1.8 + 32) | |
relative_humidity = scd40.relative_humidity | |
pixels.fill(fillcolor) | |
debug("\n\nRefresh: %ds\n" % DELAY) | |
debug("%0.1fF | %0.1f%%\n" % (temperature, relative_humidity)) | |
debug("%dhPa | %0.2fm\n" % (pressure, altitude)) | |
debug("CO2: %dppm" % co2) | |
unlocked = True | |
# Todo for later when the button will do something | |
mode = True | |
while True: | |
if not button.value: | |
mode = not mode | |
if unlocked and int(time.time()) % DELAY == 0: | |
reportstuff() | |
unlocked = False | |
time.sleep(0.1) | |
if (int(time.time()) % DELAY == 2): | |
unlocked = True |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment