diff --git a/WeatherPredictor/WeatherPredictor.ino b/WeatherPredictor/WeatherPredictor.ino index b4504b1..3ab0942 100644 --- a/WeatherPredictor/WeatherPredictor.ino +++ b/WeatherPredictor/WeatherPredictor.ino @@ -2,6 +2,7 @@ #include #include "config.h" #include "display_ui.h" +#include "sensors.h" static void i2cScan() { Serial.println(F("I2C scan:")); @@ -25,7 +26,14 @@ void setup() { displayBegin(); displaySplash("Weather", "Predictor v0.1"); + + if (!sensorsBegin()) Serial.println(F("BMP180 not found!")); } void loop() { + float abs_ = readAbsPressureHpa(); + float msl = toSeaLevelHpa(abs_, DEFAULT_ALTITUDE_M); + Serial.printf("abs=%.1f hPa msl=%.1f hPa t=%.1f C\n", + abs_, msl, readTemperatureC()); + delay(2000); } diff --git a/WeatherPredictor/sensors.cpp b/WeatherPredictor/sensors.cpp new file mode 100644 index 0000000..0561a1b --- /dev/null +++ b/WeatherPredictor/sensors.cpp @@ -0,0 +1,22 @@ +#include +#include +#include "sensors.h" + +static Adafruit_BMP085 bmp; + +bool sensorsBegin() { + return bmp.begin(); // uses Wire (already started on D6/D1), addr 0x77 +} + +float readAbsPressureHpa() { + return bmp.readPressure() / 100.0f; // Pa -> hPa +} + +float readTemperatureC() { + return bmp.readTemperature(); +} + +// Standard barometric reduction to sea level. +float toSeaLevelHpa(float absHpa, float altitudeM) { + return absHpa / powf(1.0f - (altitudeM / 44330.0f), 5.255f); +} diff --git a/WeatherPredictor/sensors.h b/WeatherPredictor/sensors.h new file mode 100644 index 0000000..36e4952 --- /dev/null +++ b/WeatherPredictor/sensors.h @@ -0,0 +1,7 @@ +#pragma once +#include + +bool sensorsBegin(); +float readAbsPressureHpa(); // absolute (station) pressure, hPa +float readTemperatureC(); // degrees C +float toSeaLevelHpa(float absHpa, float altitudeM); // mean-sea-level pressure, hPa