diff --git a/enviro/__init__.py b/enviro/__init__.py index f92753d..f4c4fda 100644 --- a/enviro/__init__.py +++ b/enviro/__init__.py @@ -10,9 +10,9 @@ i2c = PimoroniI2C(I2C_SDA_PIN, I2C_SCL_PIN, 100000) i2c_devices = i2c.scan() model = None -if 56 in i2c_devices: # 56 = colour / light sensor and only present on Indoor +if I2C_ADDR_BH1745 in i2c_devices: # colour / light sensor and only present on Indoor model = "indoor" -elif 35 in i2c_devices: # 35 = ltr-599 on grow & weather +elif I2C_ADDR_LTR559 in i2c_devices: # ltr-599 on grow & weather pump3_pin = Pin(12, Pin.IN, Pin.PULL_UP) model = "grow" if pump3_pin.value() == False else "weather" pump3_pin.init(pull=None) @@ -30,7 +30,17 @@ def get_board(): if model == "urban": import enviro.boards.urban as board return board - + +# return any additional sensors connected with Qw/ST +def get_additional_sensors(): + if I2C_ADDR_SCD41 in i2c_devices: + try: + import enviro.sensors.scd41 as scd41 + yield scd41 + except RuntimeError: + # Likely another device present on the SCD41 address + pass + # set up the activity led # =========================================================================== from machine import PWM, Timer @@ -411,9 +421,14 @@ def get_sensor_readings(): readings = get_board().get_sensor_readings(seconds_since_last, vbus_present) # readings["voltage"] = 0.0 # battery_voltage #Temporarily removed until issue is fixed + # Add any additional sensor readings + for sensor in get_additional_sensors(): + for key, value in sensor.get_sensor_readings(seconds_since_last).items(): + readings[key] = value + # write out the last time log with open("last_time.txt", "w") as timefile: - timefile.write(now_str) + timefile.write(now_str) return readings diff --git a/enviro/constants.py b/enviro/constants.py index 78cfe8b..1cdf3fd 100644 --- a/enviro/constants.py +++ b/enviro/constants.py @@ -47,3 +47,8 @@ WATER_VAPOR_SPECIFIC_GAS_CONSTANT = 461.5 CRITICAL_WATER_TEMPERATURE = 647.096 CRITICAL_WATER_PRESSURE = 22064000 + +# I2C addresses +I2C_ADDR_BH1745 = 0x38 +I2C_ADDR_LTR559 = 0x23 +I2C_ADDR_SCD41 = 0x62 diff --git a/enviro/sensors/scd41.py b/enviro/sensors/scd41.py new file mode 100644 index 0000000..ae0f307 --- /dev/null +++ b/enviro/sensors/scd41.py @@ -0,0 +1,26 @@ +import time + +import breakout_scd41 + +from enviro import i2c + +breakout_scd41.init(i2c) +breakout_scd41.start() + +def get_sensor_readings(seconds_since_last): + retries = 25 + while retries > 0 and not breakout_scd41.ready(): + time.sleep(0.2) + retries -= 1 + + if retries == 0: + return {} + + scd_co2, scd_temp, scd_humidity = breakout_scd41.measure() + + from ucollections import OrderedDict + return OrderedDict({ + "scd_co2": scd_co2, + "scd_temperature": scd_temp, + "scd_humidity": scd_humidity + }) \ No newline at end of file diff --git a/main.py b/main.py index 95eff3b..de938bb 100644 --- a/main.py +++ b/main.py @@ -73,9 +73,9 @@ # here you can customise the sensor readings by adding extra information # or removing readings that you don't want, for example: # - # del readings["temperature"] # remove the temperature reading + # del reading["temperature"] # remove the temperature reading # - # readings["custom"] = my_reading() # add my custom reading value + # reading["custom"] = my_reading() # add my custom reading value # is an upload destination set? if enviro.config.destination: