Last active
December 1, 2023 02:29
-
-
Save vitorio/db818bcb21f29fae08892fff18a32dc3 to your computer and use it in GitHub Desktop.
CO2 monitor using Adafruit Trinkey QT2040 + SCD-40 + ST25DV16K, CircuitPython 8.x
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
# SPDX-FileCopyrightText: 2021 Carter Nelson for Adafruit Industries | |
# SPDX-License-Identifier: MIT | |
import time | |
import board | |
import adafruit_scd4x | |
import neopixel | |
import adafruit_24lc32 | |
## https://github.com/Neradoc/circuitpython-st25dv | |
class ST25DV16(adafruit_24lc32.EEPROM_I2C): | |
def __init__(self, i2c_bus, address=0x53, write_protect=False, wp_pin=None): | |
from adafruit_bus_device.i2c_device import I2CDevice | |
self._i2c = I2CDevice(i2c_bus, address) | |
adafruit_24lc32.EEPROM.__init__(self, adafruit_24lc32.const(0x800), write_protect, wp_pin) | |
## https://github.com/Neradoc/circuitpython-st25dv | |
def payload_text(text, language='en'): | |
text_data = text.encode() | |
language_code = language.encode() | |
type_length = len(language_code) + 1 | |
text_field_length = len(text_data) + type_length | |
rfid_payload = bytes([0xD1, 0x01, text_field_length]) + b"T" + bytes([len(language_code)]) + language_code + text_data | |
return bytes([0xE1, 0x40, 0x40, 0x05]) + bytes([0x03, len(rfid_payload)]) + rfid_payload + b"\xFE" | |
# setup stuff | |
i2c = board.STEMMA_I2C() # For using the built-in STEMMA QT connector on a microcontroller | |
scd = adafruit_scd4x.SCD4X(i2c) | |
scd.start_periodic_measurement() | |
pixel = neopixel.NeoPixel(board.NEOPIXEL, 1) | |
eeprom = ST25DV16(i2c) | |
# init time tracking and heartbeat | |
last_beat = time.monotonic() | |
beat_color = 0xFFFF00 | |
beat_brightness = 0.1 | |
beat_rate = 1 | |
# loop forever! | |
while True: | |
current_time = time.monotonic() | |
# data | |
if scd.data_ready: | |
CURR_CO2 = scd.CO2 | |
CO2_DATA = "CO2: {}".format(CURR_CO2) | |
RFID_DATA = payload_text(CO2_DATA) | |
beat_color = 0xFFFF00 | |
beat_brightness = 1.0 | |
if CURR_CO2 < 600: | |
beat_color = 0x00FF00 | |
elif CURR_CO2 >= 800: | |
beat_color = 0xFF0000 | |
print(CO2_DATA) | |
try: | |
eeprom[0:len(RFID_DATA)] = RFID_DATA | |
except: | |
pass | |
# heart beat | |
if current_time - last_beat > beat_rate: | |
r,g,b = pixel[0] | |
if r or g or b: | |
pixel.fill(0) | |
else: | |
pixel.brightness = beat_brightness | |
pixel.fill(beat_color) | |
beat_brightness = 0.1 | |
last_beat = current_time |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment