forked from RequestForCoffee/scd30
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcontinuous_measurement.py
63 lines (50 loc) · 1.84 KB
/
continuous_measurement.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
from scd30_i2c import SCD30
import logging
import time
def continuous_reading(scd30: SCD30):
while True:
if scd30.get_data_ready():
measurement = scd30.read_measurement()
if measurement is not None:
co2, temp, rh = measurement
print(f"CO2: {co2:.2f}ppm, temp: {temp:.2f}'C, rh: {rh:.2f}%")
time.sleep(measurement_interval)
else:
time.sleep(0.2)
scd30 = SCD30()
logging.basicConfig(level=logging.INFO)
retries = 30
logging.info("Probing sensor...")
ready = None
while ready is None and retries:
try:
ready = scd30.get_data_ready()
except OSError:
# The sensor may need a couple of seconds to boot up after power-on
# and may not be ready to respond, raising I2C errors during this time.
pass
time.sleep(1)
retries -= 1
if not retries:
logging.error("Timed out waiting for SCD30.")
exit(1)
logging.info("Link to sensor established.")
logging.info("Getting firmware version...")
logging.info(f"Sensor firmware version: {scd30.get_firmware_version()}")
# 2 seconds is the minimum supported interval.
measurement_interval = 2
logging.info("Setting measurement interval to 2s...")
scd30.set_measurement_interval(measurement_interval)
logging.info("Enabling automatic self-calibration...")
scd30.set_auto_self_calibration(active=True)
logging.info("Starting periodic measurement...")
scd30.start_periodic_measurement()
time.sleep(measurement_interval)
logging.info(f"ASC status: {scd30.get_auto_self_calibration_active()}")
logging.info(f"Measurement interval: {scd30.get_measurement_interval()}s")
logging.info(f"Temperature offset: {scd30.get_temperature_offset()}'C")
try:
continuous_reading(scd30)
except KeyboardInterrupt:
logging.info("Stopping periodic measurement...")
scd30.stop_periodic_measurement()