From 1c61a25d2cd6a53111b8faf0a77a5feea532c839 Mon Sep 17 00:00:00 2001 From: Hendrik Langer Date: Mon, 29 Jul 2019 18:26:13 +0200 Subject: [PATCH] add bme680 --- platformio.ini | 1 + src/main.cpp | 113 +++++++++++++++++++++++++++++++++++++------------ 2 files changed, 88 insertions(+), 26 deletions(-) diff --git a/platformio.ini b/platformio.ini index d90b873..586388f 100644 --- a/platformio.ini +++ b/platformio.ini @@ -36,6 +36,7 @@ lib_deps = ; https://github.com/wemos/LOLIN_EPD_Library Adafruit GFX Library Adafruit BME280 Library + Adafruit BME680 Library Adafruit Unified Sensor Adafruit BusIO Adafruit VEML6075 Library diff --git a/src/main.cpp b/src/main.cpp index 9cd6056..cdcd403 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -20,6 +20,7 @@ #include #include "Adafruit_BME280.h" +#include "Adafruit_BME680.h" #include "Adafruit_VEML6075.h" @@ -39,7 +40,8 @@ static const char* TAG = "MAIN"; WiFiMulti wifiMulti; GxEPD2_BW display(GxEPD2_213_B72(/*CS=SS*/ TFT_CS, /*DC=*/ TFT_DC, /*RST=*/ TFT_RST, /*BUSY=*/ -1)); // GDEH0213B72 -Adafruit_BME280 bme; // I2C (also available: hardware SPI +Adafruit_BME280 bme280; // I2C (also available: hardware SPI +Adafruit_BME680 bme680; // I2C (also available: hardware SPI //HardwareSerial Serial2(2); SdsDustSensor sds(Serial2); Adafruit_VEML6075 uv = Adafruit_VEML6075(); @@ -48,7 +50,8 @@ XD0OTA ota("esp32-weatherstation"); XD0MQTT mqtt; uint32_t lastDisplayUpdate = 0; -bool bme_active = false; +bool bme280_active = false; +bool bme680_active = false; bool uv_active = false; bool sds_active = false; @@ -102,23 +105,45 @@ void getTime(char* ptr, size_t maxsize, const char* format) { void printValues() { - if (bme_active) { + if (bme280_active) { #define SEALEVELPRESSURE_HPA (1013.25) Serial.print("Temperature = "); - Serial.print(bme.readTemperature()); + Serial.print(bme280.readTemperature()); Serial.println(" *C"); Serial.print("Pressure = "); - Serial.print(bme.readPressure() / 100.0F); + Serial.print(bme280.readPressure() / 100.0F); Serial.println(" hPa"); Serial.print("Approx. Altitude = "); - Serial.print(bme.readAltitude(SEALEVELPRESSURE_HPA)); + Serial.print(bme280.readAltitude(SEALEVELPRESSURE_HPA)); Serial.println(" m"); Serial.print("Humidity = "); - Serial.print(bme.readHumidity()); + Serial.print(bme280.readHumidity()); + Serial.println(" %"); + + Serial.println(); + } + + if (bme680_active) { + #define SEALEVELPRESSURE_HPA (1013.25) + Serial.print("Temperature = "); + Serial.print(bme680.readTemperature()); + Serial.println(" *C"); + + Serial.print("Pressure = "); + + Serial.print(bme680.readPressure() / 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.println(" %"); Serial.println(); @@ -150,16 +175,30 @@ void printValues() { void sendValues() { /* send values MQTT */ - if (bme_active) { + if (bme280_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); + 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); 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"); + 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)); + mqtt.publish(topic_voc.c_str(), voc, strlen(voc)); } else { String topic_temperature = String("thomas/sensor/") + ota.getMAC() + String("/temperature"); float esp32_temperature = (temprature_sens_read() - 32) / 1.8; @@ -220,19 +259,25 @@ void setup() #define BME_SDA 21 #define BME_SCL 22 Wire.begin(BME_SDA, BME_SCL); - if (bme.begin()) { - bme_active = true; + if (bme280.begin()) { + bme280_active = true; } else { ESP_LOGE(TAG, "Could not find a valid BME280 sensor, check wiring!"); } -/* - // Set up oversampling and filter initialization - bme.setTemperatureOversampling(BME680_OS_8X); - bme.setHumidityOversampling(BME680_OS_2X); - bme.setPressureOversampling(BME680_OS_4X); - bme.setIIRFilterSize(BME680_FILTER_SIZE_3); - bme.setGasHeater(320, 150); // 320*C for 150 ms -*/ + if (bme680.begin()) { + bme680_active = true; + } else { + ESP_LOGE(TAG, "Could not find a valid BME680 sensor, check wiring!"); + } + + if (bme680_active) { + // Set up oversampling and filter initialization + bme680.setTemperatureOversampling(BME680_OS_8X); + bme680.setHumidityOversampling(BME680_OS_2X); + bme680.setPressureOversampling(BME680_OS_4X); + bme680.setIIRFilterSize(BME680_FILTER_SIZE_3); + bme680.setGasHeater(320, 150); // 320*C for 150 ms + } if (uv.begin()) { uv_active = true; @@ -307,22 +352,38 @@ void loop() { display.fillScreen(GxEPD_WHITE); display.setTextColor(GxEPD_BLACK); - display.setFont(&FreeSans9pt7b); display.setTextWrap(false); display.setCursor(0, 11); + display.setFont(&FreeSans9pt7b); display.println("ESP32-Wetterstation"); display.drawFastHLine(0, 14, display.width(), GxEPD_BLACK); display.setCursor(0, 32); - if (bme_active) { + if (bme280_active) { display.print("Temperatur: "); - display.print(bme.readTemperature()); + display.print(bme280.readTemperature()); display.println(" *C"); display.print("Luftfeuchte: "); - display.print(bme.readHumidity()); + display.print(bme280.readHumidity()); display.println(" %"); display.print("Luftdruck:"); - display.print(bme.readPressure() / 100.0F); + display.print(bme280.readPressure() / 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"); + display.print("Gas: "); + display.print(bme680.gas_resistance / 1000.0); + display.println(" KOhms"); } else { display.print("Temperatur: "); float esp32_temperature = (temprature_sens_read() - 32) / 1.8;