From ddf1dc5c66d9a803d2265d23f70ae564ed43135f Mon Sep 17 00:00:00 2001 From: Hendrik Langer Date: Mon, 29 Jul 2019 19:37:00 +0200 Subject: [PATCH] take measurements once per minute --- src/main.cpp | 192 +++++++++++++++++++++++++-------------------------- 1 file changed, 93 insertions(+), 99 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 614d114..32bda72 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -49,6 +49,18 @@ Adafruit_VEML6075 uv = Adafruit_VEML6075(); XD0OTA ota("esp32-weatherstation"); XD0MQTT mqtt; +struct __attribute__((packed)) { + float temperature = 0.0; // °C + float humidity = 0.0; // %H + float pressure = 0.0; // Pa + uint32_t voc = 0; // Ohm + float pm10 = 0.0; // µg/m³ + float pm25 = 0.0; // µg/m³ + float uvi = 0.0; + float uva = 0.0; + float uvb = 0.0; +} sensor_readings; + uint32_t lastDisplayUpdate = 0; bool bme280_active = false; bool bme680_active = false; @@ -104,102 +116,100 @@ void getTime(char* ptr, size_t maxsize, const char* format) { } -void printValues() { +void getSensorMeasurements() { if (bme280_active) { - #define SEALEVELPRESSURE_HPA (1013.25) - Serial.print("Temperature = "); - Serial.print(bme280.readTemperature()); - Serial.println(" *C"); - - Serial.print("Pressure = "); - - Serial.print(bme280.readPressure() / 100.0F); - Serial.println(" hPa"); - - Serial.print("Approx. Altitude = "); - Serial.print(bme280.readAltitude(SEALEVELPRESSURE_HPA)); - Serial.println(" m"); + bme280.takeForcedMeasurement(); + sensor_readings.temperature = bme280.readTemperature(); + sensor_readings.humidity = bme280.readHumidity(); + sensor_readings.pressure = 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.voc = bme680.gas_resistance; + } else { + Serial.println("Failed to perform reading :("); + } + } - Serial.print("Humidity = "); - Serial.print(bme280.readHumidity()); - Serial.println(" %"); + if (uv_active) { + sensor_readings.uvi = uv.readUVI(); + sensor_readings.uva = uv.readUVA(); + sensor_readings.uvb = uv.readUVB(); + } - Serial.println(); + if (sds_active) { + PmResult pm = sds.readPm(); + if (pm.isOk()) { + sensor_readings.pm10 = pm.pm10; + sensor_readings.pm25 = pm.pm25; + } } +} - if (bme680_active) { + +void printValues() { + if (bme280_active || bme680_active) { #define SEALEVELPRESSURE_HPA (1013.25) Serial.print("Temperature = "); - Serial.print(bme680.readTemperature()); + Serial.print(sensor_readings.temperature); Serial.println(" *C"); Serial.print("Pressure = "); - Serial.print(bme680.readPressure() / 100.0F); + Serial.print(sensor_readings.pressure / 100.0F); Serial.println(" hPa"); - Serial.print("Approx. Altitude = "); - Serial.print(bme680.readAltitude(SEALEVELPRESSURE_HPA)); - Serial.println(" m"); - Serial.print("Humidity = "); - Serial.print(bme680.readHumidity()); + Serial.print(sensor_readings.humidity); Serial.println(" %"); + } - Serial.println(); + if (bme680_active) { + Serial.print("VOC = "); + Serial.print(sensor_readings.voc / 1000.0F); + Serial.println(" hPa"); } + Serial.println(); + if (uv_active) { - Serial.print("UV Index reading: "); Serial.println(uv.readUVI()); - Serial.print("Raw UVA reading: "); Serial.println(uv.readUVA()); - Serial.print("Raw UVB reading: "); Serial.println(uv.readUVB()); + Serial.print("UV Index reading: "); Serial.println(sensor_readings.uvi); + Serial.print("Raw UVA reading: "); Serial.println(sensor_readings.uva); + Serial.print("Raw UVB reading: "); Serial.println(sensor_readings.uvb); + Serial.println(); } -/* - if (sds_active) { - PmResult pm = sds.readPm(); - if (pm.isOk()) { - Serial.print("PM2.5 = "); - Serial.print(pm.pm25); - Serial.print(", PM10 = "); - Serial.println(pm.pm10); - // if you want to just print the measured values, you can use toString() method as well - Serial.println(pm.toString()); - } else { - Serial.print("Could not read values from sensor, reason: "); - Serial.println(pm.statusToString()); - } + if (sds_active) { + Serial.print("PM2.5 = "); + Serial.print(sensor_readings.pm25); + Serial.print(", PM10 = "); + Serial.println(sensor_readings.pm10); } -*/ + } void sendValues() { /* send values MQTT */ - if (bme280_active) { + if (bme280_active || bme680_active) { String topic_temperature = String("thomas/sensor/") + ota.getMAC() + String("/temperature"); String topic_humidity = String("thomas/sensor/") + ota.getMAC() + String("/humidity"); String topic_pressure = String("thomas/sensor/") + ota.getMAC() + String("/pressure"); - char temperature[8]; sprintf(temperature, "%.2f", bme280.readTemperature()); - char humidity[7]; sprintf(humidity, "%.2f", bme280.readHumidity()); - char pressure[8]; sprintf(pressure, "%.2f", bme280.readPressure() / 100.0F); + char temperature[8]; sprintf(temperature, "%.2f", sensor_readings.temperature); + char humidity[7]; sprintf(humidity, "%.2f", sensor_readings.humidity); + char pressure[8]; sprintf(pressure, "%.2f", sensor_readings.pressure / 100.0F); mqtt.publish(topic_temperature.c_str(), temperature, strlen(temperature)); mqtt.publish(topic_humidity.c_str(), humidity, strlen(humidity)); mqtt.publish(topic_pressure.c_str(), pressure, strlen(pressure)); - } else if (bme680_active) { - bme680.performReading(); - String topic_temperature = String("thomas/sensor/") + ota.getMAC() + String("/temperature"); - String topic_humidity = String("thomas/sensor/") + ota.getMAC() + String("/humidity"); - String topic_pressure = String("thomas/sensor/") + ota.getMAC() + String("/pressure"); + } + if (bme680_active) { String topic_voc = String("thomas/sensor/") + ota.getMAC() + String("/voc"); - char temperature[8]; sprintf(temperature, "%.2f", bme680.readTemperature()); - char humidity[7]; sprintf(humidity, "%.2f", bme680.readHumidity()); - char pressure[8]; sprintf(pressure, "%.2f", bme680.readPressure() / 100.0F); - char voc[12]; sprintf(voc, "%.2f", bme680.gas_resistance / 1000.0F); - mqtt.publish(topic_temperature.c_str(), temperature, strlen(temperature)); - mqtt.publish(topic_humidity.c_str(), humidity, strlen(humidity)); - mqtt.publish(topic_pressure.c_str(), pressure, strlen(pressure)); + char voc[12]; sprintf(voc, "%.2f", sensor_readings.voc / 1000.0F); mqtt.publish(topic_voc.c_str(), voc, strlen(voc)); - } else { + } + if (!bme280_active && !bme680_active) { String topic_temperature = String("thomas/sensor/") + ota.getMAC() + String("/temperature"); float esp32_temperature = (temprature_sens_read() - 32) / 1.8; char temperature[8]; sprintf(temperature, "%.2f", esp32_temperature-29.40); @@ -210,29 +220,21 @@ void sendValues() { String topic_uvi = String("thomas/sensor/") + ota.getMAC() + String("/uvi"); String topic_uva = String("thomas/sensor/") + ota.getMAC() + String("/uva"); String topic_uvb = String("thomas/sensor/") + ota.getMAC() + String("/uvb"); - char uvi[10]; sprintf(uvi, "%.2f", uv.readUVI()); - char uva[10]; sprintf(uva, "%.2f", uv.readUVA()); - char uvb[10]; sprintf(uvb, "%.2f", uv.readUVB()); + char uvi[10]; sprintf(uvi, "%.2f", sensor_readings.uvi); + char uva[10]; sprintf(uva, "%.2f", sensor_readings.uva); + char uvb[10]; sprintf(uvb, "%.2f", sensor_readings.uvb); mqtt.publish(topic_uvi.c_str(), uvi, strlen(uvi)); mqtt.publish(topic_uva.c_str(), uva, strlen(uva)); mqtt.publish(topic_uvb.c_str(), uvb, strlen(uvb)); } if (sds_active) { - PmResult pm = sds.readPm(); - if (pm.isOk()) { String topic_pm10 = String("thomas/sensor/") + ota.getMAC() + String("/pm10"); - char pm10[10]; sprintf(pm10, "%.2f", pm.pm10); + char pm10[10]; sprintf(pm10, "%.2f", sensor_readings.pm10); String topic_pm25 = String("thomas/sensor/") + ota.getMAC() + String("/pm25"); - char pm25[10]; sprintf(pm25, "%.2f", pm.pm25); + char pm25[10]; sprintf(pm25, "%.2f", sensor_readings.pm25); mqtt.publish(topic_pm10.c_str(), pm10, strlen(pm10)); mqtt.publish(topic_pm25.c_str(), pm25, strlen(pm25)); - - // if you want to just print the measured values, you can use toString() method as well - Serial.println(pm.toString()); - } - - } } @@ -345,6 +347,9 @@ void loop() /* Do an e-paper display refresh every 2 minutes */ if (millis() - lastDisplayUpdate >= 1*60*1000) { lastDisplayUpdate = millis(); + + getSensorMeasurements(); + display.setFullWindow(); display.setRotation(1); display.firstPage(); @@ -357,39 +362,29 @@ void loop() display.setFont(&FreeSans9pt7b); display.println("ESP32-Wetterstation"); display.drawFastHLine(0, 14, display.width(), GxEPD_BLACK); - display.setFont(NULL); + //display.setFont(NULL); display.setCursor(0, 32); - if (bme280_active) { + if (bme280_active || bme680_active) { display.print("Temperatur: "); - display.print(bme280.readTemperature()); + display.print(sensor_readings.temperature); display.println(" *C"); display.print("Luftfeuchte: "); - display.print(bme280.readHumidity()); + display.print(sensor_readings.humidity); display.println(" %"); display.print("Luftdruck:"); - display.print(bme280.readPressure() / 100.0F); + display.print(sensor_readings.pressure / 100.0F); display.println(" hPa"); - } else if (bme680_active) { - if (! bme680.performReading()) { - Serial.println("Failed to perform reading :("); } - display.print("Temperatur: "); - display.print(bme680.readTemperature()); - display.println(" *C"); - display.print("Luftfeuchte: "); - display.print(bme680.readHumidity()); - display.println(" %"); - display.print("Luftdruck: "); - display.print(bme680.readPressure() / 100.0F); - display.println(" hPa"); + if (bme680_active) { display.print("Gas: "); - display.print(bme680.gas_resistance / 1000.0); + display.print(sensor_readings.voc / 1000.0); display.println(" KOhms"); - } else { + } + if (!bme280_active && !bme680_active) { display.print("Temperatur: "); float esp32_temperature = (temprature_sens_read() - 32) / 1.8; display.println(esp32_temperature-29.40); - display.println("kein BME280"); + display.println("kein BME Sensor"); } char timeStr[9]; @@ -402,11 +397,10 @@ void loop() while (display.nextPage()); display.powerOff(); - } - - printValues(); - sendValues(); + printValues(); + sendValues(); + } if(wifiMulti.run() != WL_CONNECTED) { Serial.println("WiFi not connected!");