|
1 | 1 | """Continuous weather monitoring with altitude estimation. |
2 | 2 |
|
3 | 3 | Reads temperature, pressure, and humidity every 5 seconds in normal mode, |
4 | | -computes approximate altitude from pressure using the barometric formula, |
| 4 | +computes altitude using the driver's built-in barometric formula, |
5 | 5 | and logs each measurement to the DAPLink flash as CSV. |
6 | 6 | """ |
7 | 7 |
|
|
13 | 13 | from daplink_flash import DaplinkFlash |
14 | 14 | from machine import I2C |
15 | 15 |
|
16 | | -# Sea-level reference pressure in hPa (adjust to local conditions) |
17 | | -SEA_LEVEL_PRESSURE = 1013.25 |
18 | | - |
19 | 16 | i2c = I2C(1) |
20 | 17 |
|
21 | 18 | sensor = BME280(i2c) |
22 | 19 | bridge = DaplinkBridge(i2c) |
23 | 20 | flash = DaplinkFlash(bridge) |
24 | 21 |
|
| 22 | +# Adjust sea-level pressure to local conditions for accurate altitude |
| 23 | +# sensor.sea_level_pressure_hpa = 1020.0 |
| 24 | + |
25 | 25 | # Configure for weather monitoring: high pressure resolution, moderate temp/hum |
26 | 26 | sensor.set_oversampling(temperature=OSRS_X2, pressure=OSRS_X16, humidity=OSRS_X2) |
27 | 27 | sensor.set_iir_filter(FILTER_16) |
|
35 | 35 |
|
36 | 36 | flash.write_line("temperature;pressure;humidity;altitude") |
37 | 37 |
|
38 | | - |
39 | | -def altitude_m(pressure_hpa): |
40 | | - """Estimate altitude in meters from pressure using the barometric formula.""" |
41 | | - return 44330.0 * (1.0 - (pressure_hpa / SEA_LEVEL_PRESSURE) ** 0.1903) |
42 | | - |
43 | | - |
44 | 38 | while True: |
45 | 39 | temperature, pressure, humidity = sensor.read() |
46 | | - alt = altitude_m(pressure) |
| 40 | + alt = sensor.altitude(pressure_hpa=pressure) |
47 | 41 |
|
48 | 42 | print( |
49 | 43 | "T: {:.1f} C P: {:.1f} hPa H: {:.1f} %RH Alt: {:.0f} m".format( |
|
0 commit comments