Browse Source

take measurements once per minute

ir
Hendrik Langer 5 years ago
parent
commit
ddf1dc5c66
  1. 184
      src/main.cpp

184
src/main.cpp

@ -49,6 +49,18 @@ Adafruit_VEML6075 uv = Adafruit_VEML6075();
XD0OTA ota("esp32-weatherstation"); XD0OTA ota("esp32-weatherstation");
XD0MQTT mqtt; 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; uint32_t lastDisplayUpdate = 0;
bool bme280_active = false; bool bme280_active = false;
bool bme680_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) { if (bme280_active) {
#define SEALEVELPRESSURE_HPA (1013.25) bme280.takeForcedMeasurement();
Serial.print("Temperature = "); sensor_readings.temperature = bme280.readTemperature();
Serial.print(bme280.readTemperature()); sensor_readings.humidity = bme280.readHumidity();
Serial.println(" *C"); sensor_readings.pressure = bme280.readPressure();
}
Serial.print("Pressure = "); if (bme680_active) {
if (bme680.performReading()) {
Serial.print(bme280.readPressure() / 100.0F); sensor_readings.temperature = bme680.temperature;
Serial.println(" hPa"); sensor_readings.humidity = bme680.humidity;
sensor_readings.pressure = bme680.pressure;
Serial.print("Approx. Altitude = "); sensor_readings.voc = bme680.gas_resistance;
Serial.print(bme280.readAltitude(SEALEVELPRESSURE_HPA)); } else {
Serial.println(" m"); Serial.println("Failed to perform reading :(");
}
}
Serial.print("Humidity = "); if (uv_active) {
Serial.print(bme280.readHumidity()); sensor_readings.uvi = uv.readUVI();
Serial.println(" %"); 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) #define SEALEVELPRESSURE_HPA (1013.25)
Serial.print("Temperature = "); Serial.print("Temperature = ");
Serial.print(bme680.readTemperature()); Serial.print(sensor_readings.temperature);
Serial.println(" *C"); Serial.println(" *C");
Serial.print("Pressure = "); Serial.print("Pressure = ");
Serial.print(bme680.readPressure() / 100.0F); Serial.print(sensor_readings.pressure / 100.0F);
Serial.println(" hPa"); Serial.println(" hPa");
Serial.print("Approx. Altitude = ");
Serial.print(bme680.readAltitude(SEALEVELPRESSURE_HPA));
Serial.println(" m");
Serial.print("Humidity = "); Serial.print("Humidity = ");
Serial.print(bme680.readHumidity()); Serial.print(sensor_readings.humidity);
Serial.println(" %"); 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) { if (uv_active) {
Serial.print("UV Index reading: "); Serial.println(uv.readUVI()); Serial.print("UV Index reading: "); Serial.println(sensor_readings.uvi);
Serial.print("Raw UVA reading: "); Serial.println(uv.readUVA()); Serial.print("Raw UVA reading: "); Serial.println(sensor_readings.uva);
Serial.print("Raw UVB reading: "); Serial.println(uv.readUVB()); Serial.print("Raw UVB reading: "); Serial.println(sensor_readings.uvb);
Serial.println();
} }
/*
if (sds_active) { if (sds_active) {
PmResult pm = sds.readPm();
if (pm.isOk()) {
Serial.print("PM2.5 = "); Serial.print("PM2.5 = ");
Serial.print(pm.pm25); Serial.print(sensor_readings.pm25);
Serial.print(", PM10 = "); Serial.print(", PM10 = ");
Serial.println(pm.pm10); Serial.println(sensor_readings.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());
}
} }
*/
} }
void sendValues() { void sendValues() {
/* send values MQTT */ /* send values MQTT */
if (bme280_active) { if (bme280_active || bme680_active) {
String topic_temperature = String("thomas/sensor/") + ota.getMAC() + String("/temperature"); String topic_temperature = String("thomas/sensor/") + ota.getMAC() + String("/temperature");
String topic_humidity = String("thomas/sensor/") + ota.getMAC() + String("/humidity"); String topic_humidity = String("thomas/sensor/") + ota.getMAC() + String("/humidity");
String topic_pressure = String("thomas/sensor/") + ota.getMAC() + String("/pressure"); String topic_pressure = String("thomas/sensor/") + ota.getMAC() + String("/pressure");
char temperature[8]; sprintf(temperature, "%.2f", bme280.readTemperature()); char temperature[8]; sprintf(temperature, "%.2f", sensor_readings.temperature);
char humidity[7]; sprintf(humidity, "%.2f", bme280.readHumidity()); char humidity[7]; sprintf(humidity, "%.2f", sensor_readings.humidity);
char pressure[8]; sprintf(pressure, "%.2f", bme280.readPressure() / 100.0F); char pressure[8]; sprintf(pressure, "%.2f", sensor_readings.pressure / 100.0F);
mqtt.publish(topic_temperature.c_str(), temperature, strlen(temperature)); mqtt.publish(topic_temperature.c_str(), temperature, strlen(temperature));
mqtt.publish(topic_humidity.c_str(), humidity, strlen(humidity)); mqtt.publish(topic_humidity.c_str(), humidity, strlen(humidity));
mqtt.publish(topic_pressure.c_str(), pressure, strlen(pressure)); mqtt.publish(topic_pressure.c_str(), pressure, strlen(pressure));
} else if (bme680_active) { }
bme680.performReading(); if (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");
String topic_voc = String("thomas/sensor/") + ota.getMAC() + String("/voc"); String topic_voc = String("thomas/sensor/") + ota.getMAC() + String("/voc");
char temperature[8]; sprintf(temperature, "%.2f", bme680.readTemperature()); char voc[12]; sprintf(voc, "%.2f", sensor_readings.voc / 1000.0F);
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));
mqtt.publish(topic_voc.c_str(), voc, strlen(voc)); 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"); String topic_temperature = String("thomas/sensor/") + ota.getMAC() + String("/temperature");
float esp32_temperature = (temprature_sens_read() - 32) / 1.8; float esp32_temperature = (temprature_sens_read() - 32) / 1.8;
char temperature[8]; sprintf(temperature, "%.2f", esp32_temperature-29.40); 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_uvi = String("thomas/sensor/") + ota.getMAC() + String("/uvi");
String topic_uva = String("thomas/sensor/") + ota.getMAC() + String("/uva"); String topic_uva = String("thomas/sensor/") + ota.getMAC() + String("/uva");
String topic_uvb = String("thomas/sensor/") + ota.getMAC() + String("/uvb"); String topic_uvb = String("thomas/sensor/") + ota.getMAC() + String("/uvb");
char uvi[10]; sprintf(uvi, "%.2f", uv.readUVI()); char uvi[10]; sprintf(uvi, "%.2f", sensor_readings.uvi);
char uva[10]; sprintf(uva, "%.2f", uv.readUVA()); char uva[10]; sprintf(uva, "%.2f", sensor_readings.uva);
char uvb[10]; sprintf(uvb, "%.2f", uv.readUVB()); char uvb[10]; sprintf(uvb, "%.2f", sensor_readings.uvb);
mqtt.publish(topic_uvi.c_str(), uvi, strlen(uvi)); mqtt.publish(topic_uvi.c_str(), uvi, strlen(uvi));
mqtt.publish(topic_uva.c_str(), uva, strlen(uva)); mqtt.publish(topic_uva.c_str(), uva, strlen(uva));
mqtt.publish(topic_uvb.c_str(), uvb, strlen(uvb)); mqtt.publish(topic_uvb.c_str(), uvb, strlen(uvb));
} }
if (sds_active) { if (sds_active) {
PmResult pm = sds.readPm();
if (pm.isOk()) {
String topic_pm10 = String("thomas/sensor/") + ota.getMAC() + String("/pm10"); 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"); 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_pm10.c_str(), pm10, strlen(pm10));
mqtt.publish(topic_pm25.c_str(), pm25, strlen(pm25)); 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 */ /* Do an e-paper display refresh every 2 minutes */
if (millis() - lastDisplayUpdate >= 1*60*1000) { if (millis() - lastDisplayUpdate >= 1*60*1000) {
lastDisplayUpdate = millis(); lastDisplayUpdate = millis();
getSensorMeasurements();
display.setFullWindow(); display.setFullWindow();
display.setRotation(1); display.setRotation(1);
display.firstPage(); display.firstPage();
@ -357,39 +362,29 @@ void loop()
display.setFont(&FreeSans9pt7b); display.setFont(&FreeSans9pt7b);
display.println("ESP32-Wetterstation"); display.println("ESP32-Wetterstation");
display.drawFastHLine(0, 14, display.width(), GxEPD_BLACK); display.drawFastHLine(0, 14, display.width(), GxEPD_BLACK);
display.setFont(NULL); //display.setFont(NULL);
display.setCursor(0, 32); display.setCursor(0, 32);
if (bme280_active) { if (bme280_active || bme680_active) {
display.print("Temperatur: "); display.print("Temperatur: ");
display.print(bme280.readTemperature()); display.print(sensor_readings.temperature);
display.println(" *C"); display.println(" *C");
display.print("Luftfeuchte: "); display.print("Luftfeuchte: ");
display.print(bme280.readHumidity()); display.print(sensor_readings.humidity);
display.println(" %"); display.println(" %");
display.print("Luftdruck:"); display.print("Luftdruck:");
display.print(bme280.readPressure() / 100.0F); display.print(sensor_readings.pressure / 100.0F);
display.println(" hPa"); display.println(" hPa");
} else if (bme680_active) {
if (! bme680.performReading()) {
Serial.println("Failed to perform reading :(");
} }
display.print("Temperatur: "); if (bme680_active) {
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");
display.print("Gas: "); display.print("Gas: ");
display.print(bme680.gas_resistance / 1000.0); display.print(sensor_readings.voc / 1000.0);
display.println(" KOhms"); display.println(" KOhms");
} else { }
if (!bme280_active && !bme680_active) {
display.print("Temperatur: "); display.print("Temperatur: ");
float esp32_temperature = (temprature_sens_read() - 32) / 1.8; float esp32_temperature = (temprature_sens_read() - 32) / 1.8;
display.println(esp32_temperature-29.40); display.println(esp32_temperature-29.40);
display.println("kein BME280"); display.println("kein BME Sensor");
} }
char timeStr[9]; char timeStr[9];
@ -402,11 +397,10 @@ void loop()
while (display.nextPage()); while (display.nextPage());
display.powerOff(); display.powerOff();
}
printValues(); printValues();
sendValues(); sendValues();
}
if(wifiMulti.run() != WL_CONNECTED) { if(wifiMulti.run() != WL_CONNECTED) {
Serial.println("WiFi not connected!"); Serial.println("WiFi not connected!");

Loading…
Cancel
Save