-
Notifications
You must be signed in to change notification settings - Fork 1
examples: Replace test script with practical examples for wsen-pads. #228
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
4107380
chore(wsen-pads): remove test script
Charly-sketch 19b86ba
style(wsen-pads): remove unused Pin import
Charly-sketch 2ccf1da
feat(wsen-pads): add pressure_trend example
Charly-sketch 1d31ac1
feat(wsen-pads): add floor_detector example
Charly-sketch 1b17040
refactor(wsen-pads): improve pressure_trend example
Charly-sketch 4982d5c
feat(wsen-pads): add threshold_alert example
Charly-sketch 7073684
feat(wsen-pads): add temp_pressure_display example
Charly-sketch bc8f858
feat(wsen-pads): add altitude_calibration example
Charly-sketch 8364a90
style(wsen-pads): fix linter errors in examples
Charly-sketch 08b9938
docs(wsen-pads): add new examples
Charly-sketch ae5b552
feat(wsen-pads): add weather_station example
Charly-sketch df71174
docs(wsen-pads): add weather_station example to README
Charly-sketch c242289
style(wsen-pads): remove unused constant in floor_detector
Charly-sketch 44022e3
refactor(wsen-pads): use a single read in temp_pressure_display
Charly-sketch 8dd7c79
fix(wsen-pads): rename threshold_alert typo
Charly-sketch 6ebc61c
fix(wsen-pads): fix weather_station example
Charly-sketch f83b080
style(wsen-pads): fix weather_station example formatting
Charly-sketch fcf7629
test(wsen-pads): add coverage for init errors and status helpers
Charly-sketch be7caf5
docs(wsen-pads): fix README typo
Charly-sketch da452d8
chore: gitignore node modules
Charly-sketch 1278ac1
chore: gitignore up to date
Charly-sketch b55dd55
Merge branch 'main' into wsen-pads-extand-example
Charly-sketch 2de4b1b
style(wsen-pads): fix lint errors
Charly-sketch a68d764
fix(wsen-pads): fix constant temp_pressure_display
Charly-sketch 1e64953
fix(wsen-pads): fix constant threshold_alert
Charly-sketch 1408362
fix(wsen-pads): Fix f-string in weather_station and floor height cons…
nedseb File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| from time import sleep | ||
|
|
||
| from machine import I2C | ||
| from wsen_pads import WSEN_PADS | ||
|
|
||
| # Set your known altitude (in meters) for calibration | ||
| KNOWN_ALTITUDE = 12 # Example: your location altitude | ||
|
|
||
| i2c = I2C(1) | ||
| sensor = WSEN_PADS(i2c) | ||
|
|
||
|
|
||
| pressure = sensor.pressure_hpa() | ||
|
|
||
| # Compute sea-level pressure based on known altitude | ||
| # Formula derived from barometric equation | ||
| sea_level_pressure = pressure / (1 - (KNOWN_ALTITUDE / 44330.0)) ** 5.255 | ||
|
|
||
| print("Calibration:") | ||
| print(" Measured pressure: {:.1f} hPa".format(pressure)) | ||
| print(" Known altitude: {:.1f} m".format(KNOWN_ALTITUDE)) | ||
| print(" Sea-level pressure: {:.1f} hPa".format(sea_level_pressure)) | ||
| print("-" * 50) | ||
|
|
||
| while True: | ||
| pressure = sensor.pressure_hpa() | ||
|
|
||
| # Compute altitude using calibrated sea-level pressure | ||
| altitude = 44330.0 * (1 - (pressure / sea_level_pressure) ** (1 / 5.255)) | ||
|
|
||
| print("Altitude: {:6.1f} m | Pressure: {:6.1f} hPa".format(altitude, pressure)) | ||
|
|
||
| sleep(1) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| """Measure altitude at startup as baseline, then loop and detect floor changes (each ~3m altitude difference = 1 floor). Print "Floor +1", "Floor -1", etc.""" | ||
|
|
||
| from time import sleep | ||
|
|
||
| from machine import I2C | ||
| from wsen_pads import WSEN_PADS | ||
| from wsen_pads.const import ODR_10_HZ | ||
|
|
||
| i2c = I2C(1) | ||
| sensor = WSEN_PADS(i2c) | ||
| sensor.set_continuous(odr=ODR_10_HZ) | ||
|
|
||
| SAMPLES_FOR_BASELINE = 20 | ||
| SAMPLES_PER_MEASURE = 10 | ||
| METERS_PER_FLOOR = 3.0 | ||
|
|
||
| baseline_pressure_list = [] | ||
|
|
||
| print("Measuring baseline pressure...") | ||
| for _ in range(SAMPLES_FOR_BASELINE): | ||
| pressure = sensor.pressure_hpa() | ||
| baseline_pressure_list.append(pressure) | ||
|
|
||
| sleep(0.5) | ||
|
|
||
| baseline_pressure = sum(baseline_pressure_list) / len(baseline_pressure_list) | ||
| print("Baseline pressure:", baseline_pressure, "hPa") | ||
|
|
||
| while True: | ||
| pressure_samples = [] | ||
|
|
||
| print("Measuring...") | ||
| for _ in range(SAMPLES_PER_MEASURE): | ||
| pressure = sensor.pressure_hpa() | ||
| pressure_samples.append(pressure) | ||
|
|
||
| sleep(0.5) | ||
|
|
||
| avg_pressure = sum(pressure_samples) / len(pressure_samples) | ||
|
|
||
| # Calculate altitude difference from baseline using barometric formula | ||
| altitude_diff = 44330 * (1 - (avg_pressure / baseline_pressure) ** (1/5.255)) | ||
|
|
||
| floor_change = round(altitude_diff / METERS_PER_FLOOR) | ||
|
|
||
| print("Avg Pressure:", avg_pressure, "hPa Altitude Diff:", altitude_diff, "m Floor Change:", floor_change) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| """Read pressure every 10s, keep the last 10 values in a list, print whether pressure is rising, falling, or stable (useful for simple weather prediction)""" | ||
|
|
||
| from time import sleep | ||
|
|
||
| from machine import I2C | ||
| from wsen_pads import WSEN_PADS | ||
| from wsen_pads.const import ODR_10_HZ | ||
|
|
||
| i2c = I2C(1) | ||
| sensor = WSEN_PADS(i2c) | ||
|
|
||
| sensor.set_continuous(odr=ODR_10_HZ) | ||
|
|
||
| pressure_history = [] | ||
| MAX_VALUES = 10 | ||
| THRESHOLD = 0.5 # sensitivity (hPa) | ||
|
|
||
| def get_trend(values): | ||
| if len(values) < 2: | ||
| return "N/A" | ||
|
|
||
| half = len(values) // 2 | ||
| first_half_avg = sum(values[:half]) / len(values[:half]) | ||
| second_half_avg = sum(values[half:]) / len(values[half:]) | ||
|
|
||
| diff = second_half_avg - first_half_avg | ||
|
|
||
| if abs(diff) < THRESHOLD: | ||
| return "stable" | ||
| elif diff > 0: | ||
| return "rising" | ||
| else: | ||
| return "falling" | ||
|
|
||
| while True: | ||
| pressure = sensor.pressure_hpa() | ||
|
|
||
| # store value | ||
| pressure_history.append(pressure) | ||
|
|
||
| # keep only last 10 values | ||
| if len(pressure_history) > MAX_VALUES: | ||
| pressure_history.pop(0) | ||
|
|
||
| trend = get_trend(pressure_history) | ||
|
|
||
| print("P:", pressure, "hPa, Pressure is", trend) | ||
|
|
||
| sleep(10) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| """ | ||
| Read temperature and pressure, format a nice display with units and a simple bar graph using # characters to visualize pressure (e.g. 1013.2 hPa [##########-----]) | ||
| """ | ||
| from time import sleep | ||
|
|
||
| from machine import I2C | ||
| from wsen_pads import WSEN_PADS | ||
|
|
||
| TEMP_MIN = 15.0 | ||
| TEMP_MAX = 30.0 | ||
| PRESS_MIN = 960.0 | ||
| PRESS_MAX = 1060.0 | ||
|
|
||
|
|
||
| i2c = I2C(1) | ||
| sensor = WSEN_PADS(i2c) | ||
|
|
||
| def bar_graph(value, vmin, vmax, width=20): | ||
| # Clamp value | ||
| if value < vmin: | ||
| value = vmin | ||
| elif value > vmax: | ||
| value = vmax | ||
|
|
||
| ratio = (value - vmin) / (vmax - vmin) | ||
| filled = int(ratio * width) | ||
|
|
||
| return "[" + "#" * filled + "-" * (width - filled) + "]" | ||
|
|
||
| while True: | ||
| pressure, temp = sensor.read() | ||
| temp_bar = bar_graph(temp, TEMP_MIN, TEMP_MAX) | ||
| press_bar = bar_graph(pressure, PRESS_MIN, PRESS_MAX) | ||
|
|
||
| line = "T:{:5.1f}°C {} | P:{:6.1f}hPa {}".format( | ||
| temp, temp_bar, pressure, press_bar | ||
| ) | ||
|
|
||
| print("\r" + line, end="") | ||
|
|
||
| sleep(1) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.