From dadd9fa42fb8c3884c95cbec915f73be2d0c8439 Mon Sep 17 00:00:00 2001 From: Mike Bell Date: Sat, 21 Jan 2023 14:00:58 +0000 Subject: [PATCH] Add optional SCD41 CO2 sensor to Enviro Indoor --- enviro/boards/indoor.py | 32 +++++++++++++++++++++++++++++--- 1 file changed, 29 insertions(+), 3 deletions(-) diff --git a/enviro/boards/indoor.py b/enviro/boards/indoor.py index a06bc24..6ebe6f0 100644 --- a/enviro/boards/indoor.py +++ b/enviro/boards/indoor.py @@ -1,9 +1,18 @@ import math +import time from breakout_bme68x import BreakoutBME68X from breakout_bh1745 import BreakoutBH1745 -from enviro import i2c +from enviro import i2c, i2c_devices +# Detect whether an SCD41 CO2+ sensor is connected +have_scd41 = 98 in i2c_devices +if have_scd41: + import breakout_scd41 + + breakout_scd41.init(i2c) + breakout_scd41.start() + bme688 = BreakoutBME68X(i2c, address=0x77) bh1745 = BreakoutBH1745(i2c) @@ -54,9 +63,22 @@ def get_sensor_readings(seconds_since_last): bh1745.measurement_time_ms(160) r, g, b, c = bh1745.rgbc_raw() + + co2 = 0 + if have_scd41: + retries = 25 + while retries > 0 and not breakout_scd41.ready(): + time.sleep(0.2) + retries -= 1 + + if retries != 0: + co2, temp_scd, hum_scd = breakout_scd41.measure() + + # Prefer the SCD41 temperature as it is further from the potentially warm Pico + temperature = round(temp_scd, 2) from ucollections import OrderedDict - return OrderedDict({ + d = OrderedDict({ "temperature": temperature, "humidity": humidity, "pressure": pressure, @@ -64,4 +86,8 @@ def get_sensor_readings(seconds_since_last): "aqi": aqi, "luminance": lux_from_rgbc(r, g, b, c), "color_temperature": colour_temperature_from_rgbc(r, g, b, c) - }) \ No newline at end of file + }) + if co2 > 0: + d["CO2"] = co2 + + return d \ No newline at end of file