Skip to content

Commit 54dd247

Browse files
authored
Merge pull request #1906 from adafruit/co2_logger
Adding code for CO2 logger
2 parents daa0947 + 68ca906 commit 54dd247

1 file changed

Lines changed: 87 additions & 0 deletions

File tree

  • Disconnected_CO2_Data_Logger
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
# SPDX-FileCopyrightText: 2021 Liz Clark for Adafruit Industries
2+
#
3+
# SPDX-License-Identifier: MIT
4+
5+
import time
6+
import board
7+
import digitalio
8+
import adafruit_scd4x
9+
import adafruit_sdcard
10+
import busio
11+
import storage
12+
import adafruit_pcf8523
13+
14+
# setup for I2C
15+
i2c = board.I2C()
16+
# setup for SCD40
17+
scd4x = adafruit_scd4x.SCD4X(i2c)
18+
# setup for RTC
19+
rtc = adafruit_pcf8523.PCF8523(i2c)
20+
# start measuring co2 with SCD40
21+
scd4x.start_periodic_measurement()
22+
# list of days to print to the text file on boot
23+
days = ("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday")
24+
25+
# SPI SD_CS pin
26+
SD_CS = board.D10
27+
28+
# SPI setup for SD card
29+
spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
30+
cs = digitalio.DigitalInOut(SD_CS)
31+
sdcard = adafruit_sdcard.SDCard(spi, cs)
32+
vfs = storage.VfsFat(sdcard)
33+
try:
34+
storage.mount(vfs, "/sd")
35+
print("sd card mounted")
36+
except ValueError:
37+
print("no SD card")
38+
39+
# to update the RTC, change set_clock to True
40+
# otherwise RTC will remain set
41+
# it should only be needed after the initial set
42+
# if you've removed the coincell battery
43+
set_clock = False
44+
45+
if set_clock:
46+
# year, mon, date, hour, min, sec, wday, yday, isdst
47+
t = time.struct_time((2021, 10, 31, 00, 00, 00, 0, -1, -1))
48+
49+
print("Setting time to:", t)
50+
rtc.datetime = t
51+
print()
52+
53+
# variable to hold RTC datetime
54+
t = rtc.datetime
55+
56+
time.sleep(1)
57+
58+
# initial write to the SD card on startup
59+
try:
60+
with open("/sd/co2.txt", "a") as f:
61+
# writes the date
62+
f.write('The date is {} {}/{}/{}\n'.format(days[t.tm_wday], t.tm_mday, t.tm_mon, t.tm_year))
63+
# writes the start time
64+
f.write('Start time: {}:{}:{}\n'.format(t.tm_hour, t.tm_min, t.tm_sec))
65+
# headers for data, comma-delimited
66+
f.write('CO2,Time\n')
67+
# debug statement for REPL
68+
print("initial write to SD card complete, starting to log")
69+
except ValueError:
70+
print("initial write to SD card failed - check card")
71+
72+
while True:
73+
try:
74+
# variable for RTC datetime
75+
t = rtc.datetime
76+
# append SD card text file
77+
with open("/sd/co2.txt", "a") as f:
78+
# read co2 data from SCD40
79+
co2 = scd4x.CO2
80+
# write co2 data followed by the time, comma-delimited
81+
f.write('{},{}:{}:{}\n'.format(co2, t.tm_hour, t.tm_min, t.tm_sec))
82+
print("data written to sd card")
83+
# repeat every 30 seconds
84+
time.sleep(30)
85+
except ValueError:
86+
print("data error - cannot write to SD card")
87+
time.sleep(10)

0 commit comments

Comments
 (0)