Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add optional SCD41 CO2 sensor to Enviro Indoor #143

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 19 additions & 4 deletions enviro/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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

Expand Down
5 changes: 5 additions & 0 deletions enviro/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
26 changes: 26 additions & 0 deletions enviro/sensors/scd41.py
Original file line number Diff line number Diff line change
@@ -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
})
4 changes: 2 additions & 2 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down