From 583a26a6a4adb29336c7e5a6dbd8a7fdb582c668 Mon Sep 17 00:00:00 2001 From: Hendrik Langer Date: Wed, 7 Aug 2019 15:41:40 +0200 Subject: [PATCH] pressure compensation to sea level --- src/main.cpp | 37 +++++++++++++++++++++++++++++-------- 1 file changed, 29 insertions(+), 8 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 5ee5ab7..2e0d7e9 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -59,7 +59,8 @@ XD0MQTT mqtt; struct __attribute__((packed)) sensor_readings_t { float temperature = NAN; // °C float humidity = NAN; // %H - float pressure = NAN; // Pa + float pressure = NAN; // hPa + float pressure_raw = NAN; // Pa uint32_t voc = 0; // Ohm float pm10 = NAN; // µg/m³ float pm25 = NAN; // µg/m³ @@ -85,6 +86,8 @@ bool uv_active = false; bool light_active = false; bool sds_active = false; +float station_height = 0; + void helloWorld() { const char HelloWorld[] = "IchbinsBens!"; @@ -139,13 +142,13 @@ void getSensorMeasurements() { bme280.takeForcedMeasurement(); sensor_readings.temperature = bme280.readTemperature(); sensor_readings.humidity = bme280.readHumidity(); - sensor_readings.pressure = bme280.readPressure(); + sensor_readings.pressure_raw = bme280.readPressure(); } if (bme680_active) { if (bme680.performReading()) { sensor_readings.temperature = bme680.temperature; sensor_readings.humidity = bme680.humidity; - sensor_readings.pressure = bme680.pressure; + sensor_readings.pressure_raw = bme680.pressure; sensor_readings.voc = bme680.gas_resistance; } else { Serial.println("Failed to perform reading :("); @@ -161,7 +164,14 @@ void getSensorMeasurements() { sensor_readings.temperature_min = sensor_readings.temperature; } - history_pressure.addValue(sensor_readings.pressure / 100.0F); + // https://de.wikipedia.org/wiki/Barometrische_H%C3%B6henformel#Reduktion_auf_Meeresh%C3%B6he + // https://carnotcycle.wordpress.com/2012/08/04/how-to-convert-relative-humidity-to-absolute-humidity/ + float absolute_humidity = (6.112*exp((17.67*sensor_readings.temperature)/(sensor_readings.temperature+243.5))*(sensor_readings.humidity/100)*18.02)/((273.15+sensor_readings.temperature)*1000*0.08314); + float pressure_compensation_factor = exp((9.80665/(287.05*(sensor_readings.temperature+273.15+0.12*((absolute_humidity*461.5*(sensor_readings.temperature+273.15)/100)/100)+0.0065*(station_height/2))))*station_height); + + sensor_readings.pressure = (sensor_readings.pressure_raw / 100.0F) * pressure_compensation_factor; + + history_pressure.addValue(sensor_readings.pressure); if (uv_active) { sensor_readings.uvi = uv.readUVI(); @@ -288,7 +298,7 @@ void displayValues() { display.print("Pressure"); display.setFont(&FreeSansBold9pt7b); display.setCursor(135,y_offset+40); - display.printf("%.1f", sensor_readings.pressure / 100.0F); + display.printf("%.1f", sensor_readings.pressure); display.setFont(NULL); //display.print(" hPa"); float pressure_diff = history_pressure.getElement(0) - history_pressure.getFirst(); @@ -430,8 +440,11 @@ void printValues() { Serial.println(" *C"); Serial.print("Pressure = "); + Serial.print(sensor_readings.pressure); + Serial.println(" hPa"); - Serial.print(sensor_readings.pressure / 100.0F); + Serial.print("Pressure [RAW] = "); + Serial.print(sensor_readings.pressure_raw / 100.0F); Serial.println(" hPa"); Serial.print("Humidity = "); @@ -442,7 +455,7 @@ void printValues() { if (bme680_active) { Serial.print("VOC = "); Serial.print(sensor_readings.voc / 1000.0F); - Serial.println(" hPa"); + Serial.println(" kOhm"); } Serial.println(); @@ -477,7 +490,7 @@ void sendValues() { delay(10); mqtt.publish(topic_humidity.c_str(), sensor_readings.humidity, "%.2f"); delay(10); - mqtt.publish(topic_pressure.c_str(), sensor_readings.pressure / 100.0F, "%.2f"); + mqtt.publish(topic_pressure.c_str(), sensor_readings.pressure, "%.2f"); delay(10); } if (bme680_active) { @@ -652,6 +665,14 @@ void setup() const char* fw_version_str = String(FW_VERSION).c_str(); mqtt.publish(topic_version.c_str(), fw_version_str, strlen(fw_version_str)); + if (WiFi.SSID() == "LNet") { + station_height = 135; + } else if (WiFi.SSID() == "Galaktisches Imperium") { + station_height = 30; + } else if (WiFi.SSID() == "nether.net") { + station_height = 111; + } + ESP_LOGD(TAG, "setup done"); }