Skip to content

Commit 68ca906

Browse files
committed
Adding error checking
Adding checks on SD card to catch write/mount errors
1 parent 24ba5a0 commit 68ca906

1 file changed

Lines changed: 32 additions & 20 deletions

File tree

  • Disconnected_CO2_Data_Logger

Disconnected_CO2_Data_Logger/code.py

Lines changed: 32 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,11 @@
3030
cs = digitalio.DigitalInOut(SD_CS)
3131
sdcard = adafruit_sdcard.SDCard(spi, cs)
3232
vfs = storage.VfsFat(sdcard)
33-
storage.mount(vfs, "/sd")
33+
try:
34+
storage.mount(vfs, "/sd")
35+
print("sd card mounted")
36+
except ValueError:
37+
print("no SD card")
3438

3539
# to update the RTC, change set_clock to True
3640
# otherwise RTC will remain set
@@ -52,24 +56,32 @@
5256
time.sleep(1)
5357

5458
# initial write to the SD card on startup
55-
with open("/sd/co2.txt", "a") as f:
56-
# writes the date
57-
f.write('The date is {} {}/{}/{}\n'.format(days[t.tm_wday], t.tm_mday, t.tm_mon, t.tm_year))
58-
# writes the start time
59-
f.write('Start time: {}:{}:{}\n'.format(t.tm_hour, t.tm_min, t.tm_sec))
60-
# headers for data, comma-delimited
61-
f.write('CO2,Time\n')
62-
# debug statement for REPL
63-
print("initial write to SD card complete, starting to log")
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")
6471

6572
while True:
66-
# variable for RTC datetime
67-
t = rtc.datetime
68-
# append SD card text file
69-
with open("/sd/co2.txt", "a") as f:
70-
# read co2 data from SCD40
71-
co2 = scd4x.CO2
72-
# write co2 data followed by the time, comma-delimited
73-
f.write('{},{}:{}:{}\n'.format(co2, t.tm_hour, t.tm_min, t.tm_sec))
74-
# repeat every 30 seconds
75-
time.sleep(30)
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)