Skip to content
Merged
Show file tree
Hide file tree
Changes from 10 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: perform one-shot measurements every 10 seconds, power down the sensor between reads, and display memory usage and timing |
Comment thread
Charly-sketch marked this conversation as resolved.
Outdated
28 changes: 28 additions & 0 deletions lib/wsen-hids/examples/comfort_monitor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
"""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

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(2)
29 changes: 29 additions & 0 deletions lib/wsen-hids/examples/data_logger.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
'''Read every 5s and print CSV-formatted output (timestamp, humidity, temperature) suitable for serial capture'''
from time import sleep, ticks_diff, ticks_ms

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

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

# Set filename and erase
flash.set_filename("data", "CSV")
flash.clear_flash()
sleep(0.5)
print("Flash erased.")
Comment thread
Charly-sketch marked this conversation as resolved.
Outdated

start_ms = ticks_ms()

print("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(5)
24 changes: 24 additions & 0 deletions lib/wsen-hids/examples/dew_point.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
"""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

gamma = (a * temperature_c / (b + temperature_c)) + log(humidity / 100.0)
Comment thread
Charly-sketch marked this conversation as resolved.
Outdated
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