Skip to content

Commit 767483d

Browse files
committed
daplink_flash: Add sensor logging example with statistics.
1 parent 0c18740 commit 767483d

1 file changed

Lines changed: 103 additions & 0 deletions

File tree

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
"""Log sensor data to flash and compute statistics.
2+
3+
Writes 200 rows of simulated sensor data (temperature, humidity, pressure)
4+
to a CSV file on flash, then reads it back and computes min/max/average
5+
for each column.
6+
"""
7+
8+
from machine import I2C
9+
from time import sleep_ms
10+
from daplink_flash import DaplinkFlash
11+
12+
# --- Configuration ---
13+
NUM_ROWS = 200
14+
FILENAME = "SENSORS"
15+
EXT = "CSV"
16+
17+
# --- Init ---
18+
i2c = I2C(1)
19+
flash = DaplinkFlash(i2c)
20+
print("DAPLink Flash WHO_AM_I: 0x{:02X}".format(flash.device_id()))
21+
22+
# --- Generate and write data ---
23+
print("Writing {} rows to {}.{} ...".format(NUM_ROWS, FILENAME, EXT))
24+
flash.set_filename(FILENAME, EXT)
25+
flash.clear_flash()
26+
sleep_ms(500)
27+
28+
flash.write_line("row;temperature;humidity;pressure")
29+
30+
# Simple pseudo-random generator (linear congruential)
31+
seed = 12345
32+
for row in range(NUM_ROWS):
33+
seed = (seed * 1103515245 + 12345) & 0x7FFFFFFF
34+
temp = 20.0 + (seed % 1000) / 100.0
35+
seed = (seed * 1103515245 + 12345) & 0x7FFFFFFF
36+
hum = 30.0 + (seed % 4000) / 100.0
37+
seed = (seed * 1103515245 + 12345) & 0x7FFFFFFF
38+
pres = 990.0 + (seed % 5000) / 100.0
39+
40+
line = "{};{:.1f};{:.1f};{:.1f}".format(row, temp, hum, pres)
41+
flash.write(line + "\n")
42+
43+
print("Write complete.")
44+
sleep_ms(100)
45+
46+
# --- Read back and compute statistics ---
47+
print("Reading back...")
48+
content = flash.read()
49+
lines = content.decode().split("\n")
50+
51+
# Skip header and empty lines
52+
data_lines = [l for l in lines[1:] if l]
53+
print("Read {} data rows.".format(len(data_lines)))
54+
55+
temp_sum = 0.0
56+
hum_sum = 0.0
57+
pres_sum = 0.0
58+
temp_min = 999.0
59+
temp_max = -999.0
60+
hum_min = 999.0
61+
hum_max = -999.0
62+
pres_min = 9999.0
63+
pres_max = -9999.0
64+
count = 0
65+
66+
for line in data_lines:
67+
parts = line.split(";")
68+
if len(parts) != 4:
69+
continue
70+
t = float(parts[1])
71+
h = float(parts[2])
72+
p = float(parts[3])
73+
74+
temp_sum += t
75+
hum_sum += h
76+
pres_sum += p
77+
78+
if t < temp_min:
79+
temp_min = t
80+
if t > temp_max:
81+
temp_max = t
82+
if h < hum_min:
83+
hum_min = h
84+
if h > hum_max:
85+
hum_max = h
86+
if p < pres_min:
87+
pres_min = p
88+
if p > pres_max:
89+
pres_max = p
90+
91+
count += 1
92+
93+
print()
94+
print("=== Statistics ({} rows) ===".format(count))
95+
print(" Min Avg Max")
96+
print("Temp (C): {:7.1f} {:7.1f} {:7.1f}".format(
97+
temp_min, temp_sum / count, temp_max))
98+
print("Hum (%): {:7.1f} {:7.1f} {:7.1f}".format(
99+
hum_min, hum_sum / count, hum_max))
100+
print("Pres (hPa):{:7.1f} {:7.1f} {:7.1f}".format(
101+
pres_min, pres_sum / count, pres_max))
102+
print()
103+
print("File size: {} bytes".format(len(content)))

0 commit comments

Comments
 (0)