1
1
import math
2
+ import time
2
3
from breakout_bme68x import BreakoutBME68X
3
4
from breakout_bh1745 import BreakoutBH1745
4
5
5
- from enviro import i2c
6
+ from enviro import i2c , i2c_devices
6
7
8
+ # Detect whether an SCD41 CO2+ sensor is connected
9
+ have_scd41 = 98 in i2c_devices
10
+ if have_scd41 :
11
+ import breakout_scd41
12
+
13
+ breakout_scd41 .init (i2c )
14
+ breakout_scd41 .start ()
15
+
7
16
bme688 = BreakoutBME68X (i2c , address = 0x77 )
8
17
9
18
bh1745 = BreakoutBH1745 (i2c )
@@ -54,14 +63,31 @@ def get_sensor_readings(seconds_since_last):
54
63
55
64
bh1745 .measurement_time_ms (160 )
56
65
r , g , b , c = bh1745 .rgbc_raw ()
66
+
67
+ co2 = 0
68
+ if have_scd41 :
69
+ retries = 25
70
+ while retries > 0 and not breakout_scd41 .ready ():
71
+ time .sleep (0.2 )
72
+ retries -= 1
73
+
74
+ if retries != 0 :
75
+ co2 , temp_scd , hum_scd = breakout_scd41 .measure ()
76
+
77
+ # Prefer the SCD41 temperature as it is further from the potentially warm Pico
78
+ temperature = round (temp_scd , 2 )
57
79
58
80
from ucollections import OrderedDict
59
- return OrderedDict ({
81
+ d = OrderedDict ({
60
82
"temperature" : temperature ,
61
83
"humidity" : humidity ,
62
84
"pressure" : pressure ,
63
85
"gas_resistance" : gas_resistance ,
64
86
"aqi" : aqi ,
65
87
"luminance" : lux_from_rgbc (r , g , b , c ),
66
88
"color_temperature" : colour_temperature_from_rgbc (r , g , b , c )
67
- })
89
+ })
90
+ if co2 > 0 :
91
+ d ["CO2" ] = co2
92
+
93
+ return d
0 commit comments