Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 16 additions & 13 deletions lib/wsen-hids/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -293,16 +293,19 @@ No calibration is required for basic usage.

---

# Examples

Example scripts are located in:

```
examples/
```

Examples include:

* basic one-shot measurements
* continuous measurement mode
* driver validation tests
## Examples

The `examples/` directory provides practical scripts demonstrating how to use the WSEN-HIDS sensor in different scenarios.

### Overview

| Example | Description |
|--------|------------|
| `one_shot_mode.py` | Basic one-shot measurements: trigger a measurement and read humidity and temperature on demand |
| `continuous_mode.py` | Continuous measurement mode: sensor runs continuously and values are read periodically |
| `humidity_temperature.py` | Beginner-friendly example: perform a single read and print humidity and temperature |
| `comfort_monitor.py` | Read every 2 seconds and display a comfort indicator (`Dry`, `Comfortable`, `Humid`) based on humidity |
| `data_logger.py` | Log data every 5 seconds in CSV format (`timestamp, humidity, temperature`) for serial capture |
| `dew_point.py` | Compute and display dew point using temperature and humidity (Magnus formula) |
| `heater_demo.py` | Demonstrate the built-in heater: compare readings before and after enabling it |
| `low_power_sampling.py` | Low-power sampling: one-shot every 10 s with `power_off()` between reads. Requires firmware >= v0.1.0 (#238) |
30 changes: 30 additions & 0 deletions lib/wsen-hids/examples/comfort_monitor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
"""Loop that reads humidity + temperature every 2s and prints a comfort indicator ("Dry", "Comfortable", "Humid")
based on humidity thresholds (< 30%, 30-60%, > 60%)"""

from time import sleep_ms

from machine import I2C
from wsen_hids import WSEN_HIDS

i2c = I2C(1)
sensor = WSEN_HIDS(i2c)

Comment thread
Charly-sketch marked this conversation as resolved.

def comfort_label(humidity):
if humidity < 30:
return "Dry"
elif humidity <= 60:
return "Comfortable"
else:
return "Humid"

while True:
humidity, temperature = sensor.read_one_shot()
comfort = comfort_label(humidity)

print("Humidity: {:.2f} %RH".format(humidity))
print("Temperature: {:.2f} °C".format(temperature))
print("Comfort: {}".format(comfort))
print()

sleep_ms(2000)
42 changes: 42 additions & 0 deletions lib/wsen-hids/examples/data_logger.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
"""Log humidity and temperature to DAPLink flash and serial console.

Reads every 5 seconds and writes CSV-formatted output (timestamp,
humidity, temperature). Data is stored on the DAPLink flash as
DATA.CSV and also printed to the serial console.

WARNING: When ERASE_FLASH_ON_START is True, the entire DAPLink flash
is erased before logging. Make sure you have backed up any important
data before enabling erasure.
"""
from time import sleep_ms, ticks_diff, ticks_ms

from daplink_flash import DaplinkFlash
from machine import I2C
from wsen_hids import WSEN_HIDS

# Set to True to erase the DAPLink flash on startup (DESTRUCTIVE).
ERASE_FLASH_ON_START = False

i2c = I2C(1)
sensor = WSEN_HIDS(i2c)
flash = DaplinkFlash(i2c)

flash.set_filename("DATA", "CSV")
if ERASE_FLASH_ON_START:
flash.clear_flash()
sleep_ms(500)
print("Flash erased.")

start_ms = ticks_ms()

print("timestamp,humidity,temperature")
flash.write_line("timestamp,humidity,temperature")

while True:
humidity, temperature = sensor.read_one_shot()
elapsed_s = ticks_diff(ticks_ms(), start_ms) // 1000

print("{},{:.2f},{:.2f}".format(elapsed_s, humidity, temperature))
flash.write_line("{},{:.2f},{:.2f}".format(elapsed_s, humidity, temperature))

sleep_ms(5000)
28 changes: 28 additions & 0 deletions lib/wsen-hids/examples/dew_point.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
"""Compute and display the dew point temperature from humidity + temperature using the Magnus formula.
Useful to understand when condensation might occur."""
from math import log

from machine import I2C
from wsen_hids import WSEN_HIDS

i2c = I2C(1)
sensor = WSEN_HIDS(i2c)


def dew_point_celsius(temperature_c, humidity):
# Magnus formula
Comment thread
Charly-sketch marked this conversation as resolved.
a = 17.62
b = 243.12 # °C

# Clamp humidity to a small positive value to avoid log(0) when humidity is 0.0
safe_humidity = max(humidity, 0.01)
gamma = (a * temperature_c / (b + temperature_c)) + log(safe_humidity / 100.0)
dp = (b * gamma) / (a - gamma)
return dp

humidity, temperature = sensor.read_one_shot()
dew_point = dew_point_celsius(temperature, humidity)

print("Humidity: {:.2f} %RH".format(humidity))
print("Temperature: {:.2f} °C".format(temperature))
print("Dew point: {:.2f} °C".format(dew_point))
Loading
Loading