|
30 | 30 | cs = digitalio.DigitalInOut(SD_CS) |
31 | 31 | sdcard = adafruit_sdcard.SDCard(spi, cs) |
32 | 32 | 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") |
34 | 38 |
|
35 | 39 | # to update the RTC, change set_clock to True |
36 | 40 | # otherwise RTC will remain set |
|
52 | 56 | time.sleep(1) |
53 | 57 |
|
54 | 58 | # 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") |
64 | 71 |
|
65 | 72 | 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