diff --git a/src/main.cpp b/src/main.cpp index 2bdeaa1..7a1cfd1 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -44,7 +44,9 @@ XD0OTA ota("esp32-weatherstation"); XD0MQTT mqtt; uint32_t lastDisplayUpdate = 0; - +bool bme_active = false; +bool uv_active = false; +bool sds_active = false; void helloWorld() { @@ -96,6 +98,7 @@ void getTime(char* ptr, size_t maxsize, const char* format) { void printValues() { + if (bme_active) { #define SEALEVELPRESSURE_HPA (1013.25) Serial.print("Temperature = "); Serial.print(bme.readTemperature()); @@ -115,6 +118,58 @@ void printValues() { Serial.println(" %"); 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()); + } + + 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()); + } + } + +} + +void sendValues() { + /* send values MQTT */ + if (bme_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", bme.readTemperature()); + char humidity[7]; sprintf(humidity, "%.2f", bme.readHumidity()); + char pressure[8]; sprintf(pressure, "%.2f", bme.readPressure() / 100.0F); + mqtt.publish(topic_temperature.c_str(), temperature, sizeof(temperature)); + mqtt.publish(topic_humidity.c_str(), humidity, sizeof(humidity)); + mqtt.publish(topic_pressure.c_str(), pressure, sizeof(pressure)); +} + + if (uv_active) { + 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()); + mqtt.publish(topic_uvi.c_str(), uvi, sizeof(uvi)); + mqtt.publish(topic_uvi.c_str(), uvi, sizeof(uva)); + mqtt.publish(topic_uvi.c_str(), uvi, sizeof(uvb)); + } + } /** @@ -139,7 +194,9 @@ void setup() #define BME_SDA 21 #define BME_SCL 22 Wire.begin(BME_SDA, BME_SCL); - if (!bme.begin()) { + if (bme.begin()) { + bme_active = true; + } else { ESP_LOGE(TAG, "Could not find a valid BME280 sensor, check wiring!"); } /* @@ -151,11 +208,14 @@ void setup() bme.setGasHeater(320, 150); // 320*C for 150 ms */ - if (! uv.begin()) { + if (uv.begin()) { + uv_active = true; + } else { Serial.println("Failed to communicate with VEML6075 sensor, check wiring?"); } sds.begin(); + sds_active = true; // ToDo display.clearScreen(); display.refresh(); @@ -218,15 +278,19 @@ void loop() display.println("ESP32-Wetterstation"); display.drawFastHLine(0, 14, display.width(), GxEPD_BLACK); display.setCursor(0, 32); - display.print("Temperatur: "); - display.print(bme.readTemperature()); - display.println(" *C"); - display.print("Luftfeuchte: "); - display.print(bme.readHumidity()); - display.println(" %"); - display.print("Luftdruck:"); - display.print(bme.readPressure() / 100.0F); - display.println(" hPa"); + if (bme_active) { + display.print("Temperatur: "); + display.print(bme.readTemperature()); + display.println(" *C"); + display.print("Luftfeuchte: "); + display.print(bme.readHumidity()); + display.println(" %"); + display.print("Luftdruck:"); + display.print(bme.readPressure() / 100.0F); + display.println(" hPa"); + } else { + display.println("kein BME280"); + } char timeStr[9]; getTime(timeStr, sizeof(timeStr), "%H:%M:%S"); @@ -238,57 +302,11 @@ void loop() while (display.nextPage()); display.powerOff(); - /* send values MQTT */ - 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", bme.readTemperature()); - char humidity[7]; sprintf(humidity, "%.2f", bme.readHumidity()); - char pressure[8]; sprintf(pressure, "%.2f", bme.readPressure() / 100.0F); - mqtt.publish(topic_temperature.c_str(), temperature, sizeof(temperature)); - mqtt.publish(topic_humidity.c_str(), humidity, sizeof(humidity)); - mqtt.publish(topic_pressure.c_str(), pressure, sizeof(pressure)); - } -/* - unsigned long endTime = bme.beginReading(); - if (! bme.performReading()) { - ESP_LOGE(TAG, "BME680: Failed to perform reading :("); - return; - } - - Serial.print("Temperature = "); Serial.print(bme.temperature); Serial.println(" *C"); - display.print("Temperature: "); display.print(bme.temperature); display.println(" *C"); - - Serial.print("Pressure = "); Serial.print(bme.pressure / 100.0); Serial.println(" hPa"); - display.print("Pressure: "); display.print(bme.pressure / 100); display.println(" hPa"); - - Serial.print("Humidity = "); Serial.print(bme.humidity); Serial.println(" %"); - display.print("Humidity: "); display.print(bme.humidity); display.println(" %"); - - Serial.print("Gas = "); Serial.print(bme.gas_resistance / 1000.0); Serial.println(" KOhms"); - display.print("Gas: "); display.print(bme.gas_resistance / 1000.0); display.println(" KOhms"); -*/ - - 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()); } - printValues(); + sendValues(); - 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()); if(wifiMulti.run() != WL_CONNECTED) { Serial.println("WiFi not connected!");