Skip to content

Commit 79c609a

Browse files
examples: Replace test scripts with practical examples for wsen-hids. (#239)
* refactor(wsen-hids): remove test from examples * feat(wsen-hids): add simple single read example * style(wsen-hids): add description for humidity_temp example * feat(wsen-hids): add a comfort monitoring example * feat(wsen-hids): add a data logger example * feat(wsen-hids): add dew point example * style(wsen-hids): remove french comment * feat(wsen-hids): add heater to reduce humidity example * feat(wsen-hids): add low power sampling example * docs(wsen-hids): add new examples to readme. * fix(wsen-hids): fix power down issue * fix(wsen-hids): Add sleep_ms ans forgotten titile line in datalogger example - Added a warning for the date erasing - Replace sleep by sleep_ms - writen head line for csv file * fix(wsen-hids): Added sleep_ms in comfort monitor example * fix(wsen-hids): add sleep_ms to heater demo example Also put read() after heater disable * style(wsen-hids): put docstring on multiple line in the example * fix(wsen-hids): added minimum humidity to ensure math + added lint style space * fix(wsen-hids): Address review feedback on examples. --------- Co-authored-by: Sébastien NEDJAR <sebastien@nedjar.com>
1 parent 194ab8e commit 79c609a

8 files changed

Lines changed: 207 additions & 495 deletions

File tree

lib/wsen-hids/README.md

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -293,16 +293,19 @@ No calibration is required for basic usage.
293293

294294
---
295295

296-
# Examples
297-
298-
Example scripts are located in:
299-
300-
```
301-
examples/
302-
```
303-
304-
Examples include:
305-
306-
* basic one-shot measurements
307-
* continuous measurement mode
308-
* driver validation tests
296+
## Examples
297+
298+
The `examples/` directory provides practical scripts demonstrating how to use the WSEN-HIDS sensor in different scenarios.
299+
300+
### Overview
301+
302+
| Example | Description |
303+
|--------|------------|
304+
| `one_shot_mode.py` | Basic one-shot measurements: trigger a measurement and read humidity and temperature on demand |
305+
| `continuous_mode.py` | Continuous measurement mode: sensor runs continuously and values are read periodically |
306+
| `humidity_temperature.py` | Beginner-friendly example: perform a single read and print humidity and temperature |
307+
| `comfort_monitor.py` | Read every 2 seconds and display a comfort indicator (`Dry`, `Comfortable`, `Humid`) based on humidity |
308+
| `data_logger.py` | Log data every 5 seconds in CSV format (`timestamp, humidity, temperature`) for serial capture |
309+
| `dew_point.py` | Compute and display dew point using temperature and humidity (Magnus formula) |
310+
| `heater_demo.py` | Demonstrate the built-in heater: compare readings before and after enabling it |
311+
| `low_power_sampling.py` | Low-power sampling: one-shot every 10 s with `power_off()` between reads. Requires firmware >= v0.1.0 (#238) |
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
"""Loop that reads humidity + temperature every 2s and prints a comfort indicator ("Dry", "Comfortable", "Humid")
2+
based on humidity thresholds (< 30%, 30-60%, > 60%)"""
3+
4+
from time import sleep_ms
5+
6+
from machine import I2C
7+
from wsen_hids import WSEN_HIDS
8+
9+
i2c = I2C(1)
10+
sensor = WSEN_HIDS(i2c)
11+
12+
13+
def comfort_label(humidity):
14+
if humidity < 30:
15+
return "Dry"
16+
elif humidity <= 60:
17+
return "Comfortable"
18+
else:
19+
return "Humid"
20+
21+
while True:
22+
humidity, temperature = sensor.read_one_shot()
23+
comfort = comfort_label(humidity)
24+
25+
print("Humidity: {:.2f} %RH".format(humidity))
26+
print("Temperature: {:.2f} °C".format(temperature))
27+
print("Comfort: {}".format(comfort))
28+
print()
29+
30+
sleep_ms(2000)
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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)
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
"""Compute and display the dew point temperature from humidity + temperature using the Magnus formula.
2+
Useful to understand when condensation might occur."""
3+
from math import log
4+
5+
from machine import I2C
6+
from wsen_hids import WSEN_HIDS
7+
8+
i2c = I2C(1)
9+
sensor = WSEN_HIDS(i2c)
10+
11+
12+
def dew_point_celsius(temperature_c, humidity):
13+
# Magnus formula
14+
a = 17.62
15+
b = 243.12 # °C
16+
17+
# Clamp humidity to a small positive value to avoid log(0) when humidity is 0.0
18+
safe_humidity = max(humidity, 0.01)
19+
gamma = (a * temperature_c / (b + temperature_c)) + log(safe_humidity / 100.0)
20+
dp = (b * gamma) / (a - gamma)
21+
return dp
22+
23+
humidity, temperature = sensor.read_one_shot()
24+
dew_point = dew_point_celsius(temperature, humidity)
25+
26+
print("Humidity: {:.2f} %RH".format(humidity))
27+
print("Temperature: {:.2f} °C".format(temperature))
28+
print("Dew point: {:.2f} °C".format(dew_point))

0 commit comments

Comments
 (0)