Skip to content

Commit 8e730ad

Browse files
authored
feat(bme280): Add dew point calculation using Magnus formula (#322)
* feat(bme280): Add dew point calculation using Magnus formula. * fix(bme280): Use single read() in dew_point and clamp RH to avoid log(0).
1 parent cb58b8e commit 8e730ad

3 files changed

Lines changed: 49 additions & 1 deletion

File tree

lib/bme280/README.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,16 @@ sensor.sea_level_pressure_hpa = 1020.0
186186

187187
---
188188

189+
### Dew Point
190+
191+
```python
192+
dp = sensor.dew_point()
193+
```
194+
195+
Returns the dew point temperature in **degrees Celsius**, computed from the current temperature and humidity using the Magnus formula.
196+
197+
---
198+
189199
## Data-Ready Status
190200

191201
```python
@@ -313,7 +323,7 @@ Performs a soft reset, re-reads calibration data, and re-applies default configu
313323
| Standby time ||||| ⚠️ Fixed 500ms ||
314324
| Altitude |||||||
315325
| Sea-level pressure |||||||
316-
| Dew point | ||||||
326+
| Dew point | ||||||
317327
| Soft reset |||||||
318328
| Full reset + recalibration |||||||
319329
| power_off / power_on ||| ⚠️ Via mode ||||

lib/bme280/bme280/device.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,3 +409,17 @@ def altitude(self, pressure_hpa=None):
409409
"""
410410
p = self.pressure_hpa() if pressure_hpa is None else pressure_hpa
411411
return 44330.0 * (1.0 - (p / self.sea_level_pressure_hpa) ** 0.1903)
412+
413+
def dew_point(self):
414+
"""Return dew point temperature in degrees Celsius.
415+
416+
Uses the Magnus formula (Alduchov & Eskridge, 1996) with the
417+
current temperature and relative humidity readings. Both values
418+
come from a single ``read()`` call to ensure consistency.
419+
"""
420+
from math import log
421+
422+
t, _, rh = self.read()
423+
rh = max(rh, 0.01)
424+
gamma = log(rh / 100.0) + 17.625 * t / (243.04 + t)
425+
return 243.04 * gamma / (17.625 - gamma)

tests/scenarios/bme280.yaml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -630,3 +630,27 @@ tests:
630630
result = abs(alt - 33.7) < 1.0
631631
expect_true: true
632632
mode: [mock]
633+
634+
# ----- Dew point -----
635+
636+
- name: "dew_point() returns plausible value"
637+
action: script
638+
script: |
639+
# Mock: ~25.08°C, ~50.57%RH → dew point ~14.11°C
640+
dp = dev.dew_point()
641+
result = abs(dp - 14.11) < 0.5
642+
expect_true: true
643+
mode: [mock]
644+
645+
- name: "dew_point() matches Magnus formula for mock readings"
646+
action: script
647+
script: |
648+
from math import log
649+
t, _, rh = dev.read()
650+
# Compute expected dew point from Magnus formula
651+
gamma = log(rh / 100.0) + 17.625 * t / (243.04 + t)
652+
expected = 243.04 * gamma / (17.625 - gamma)
653+
dp = dev.dew_point()
654+
result = abs(dp - expected) < 0.01
655+
expect_true: true
656+
mode: [mock]

0 commit comments

Comments
 (0)