|
| 1 | +"""Log humidity and temperature to DAPLink flash and serial console. |
| 2 | +
|
| 3 | +Reads every 5 seconds and writes CSV-formatted output (timestamp, |
| 4 | +humidity, temperature). Data is stored on the DAPLink flash as |
| 5 | +DATA.CSV and also printed to the serial console. |
| 6 | +
|
| 7 | +WARNING: When ERASE_FLASH_ON_START is True, the entire DAPLink flash |
| 8 | +is erased before logging. Make sure you have backed up any important |
| 9 | +data before enabling erasure. |
| 10 | +""" |
| 11 | +from time import sleep_ms, ticks_diff, ticks_ms |
| 12 | + |
| 13 | +from daplink_flash import DaplinkFlash |
| 14 | +from machine import I2C |
| 15 | +from wsen_hids import WSEN_HIDS |
| 16 | + |
| 17 | +# Set to True to erase the DAPLink flash on startup (DESTRUCTIVE). |
| 18 | +ERASE_FLASH_ON_START = False |
| 19 | + |
| 20 | +i2c = I2C(1) |
| 21 | +sensor = WSEN_HIDS(i2c) |
| 22 | +flash = DaplinkFlash(i2c) |
| 23 | + |
| 24 | +flash.set_filename("DATA", "CSV") |
| 25 | +if ERASE_FLASH_ON_START: |
| 26 | + flash.clear_flash() |
| 27 | + sleep_ms(500) |
| 28 | + print("Flash erased.") |
| 29 | + |
| 30 | +start_ms = ticks_ms() |
| 31 | + |
| 32 | +print("timestamp,humidity,temperature") |
| 33 | +flash.write_line("timestamp,humidity,temperature") |
| 34 | + |
| 35 | +while True: |
| 36 | + humidity, temperature = sensor.read_one_shot() |
| 37 | + elapsed_s = ticks_diff(ticks_ms(), start_ms) // 1000 |
| 38 | + |
| 39 | + print("{},{:.2f},{:.2f}".format(elapsed_s, humidity, temperature)) |
| 40 | + flash.write_line("{},{:.2f},{:.2f}".format(elapsed_s, humidity, temperature)) |
| 41 | + |
| 42 | + sleep_ms(5000) |
0 commit comments