From 47dd714a92229fa5f1aaa65245804ff5852931f9 Mon Sep 17 00:00:00 2001 From: Nicu Hodos Date: Sat, 13 Jan 2024 15:40:00 +0100 Subject: [PATCH] switch to BME280 --- include/bme.h | 50 +++++++++++++++++++++++++++++++++++++++ include/bmp.h | 59 ----------------------------------------------- include/display.h | 22 ++++++++++++++---- include/ha.h | 8 +++++++ include/mqtt.h | 14 ++++++----- platformio.ini | 2 +- src/esp_clock.cpp | 26 +++++++++++---------- 7 files changed, 99 insertions(+), 82 deletions(-) create mode 100644 include/bme.h delete mode 100644 include/bmp.h diff --git a/include/bme.h b/include/bme.h new file mode 100644 index 0000000..8360e11 --- /dev/null +++ b/include/bme.h @@ -0,0 +1,50 @@ +#pragma once + +#include + +namespace Bme { + + Adafruit_BME280 bme; // I2C Interface + + struct { + float temp; + float humidity; + float pressure; + float altitude; + float readTemp() { + char buf[10]; + sprintf(buf, "%.1f", bme.readTemperature() - 2); + temp = atof(buf); + return temp; + } + float readHumidity() { + char buf[10]; + sprintf(buf, "%.1f", bme.readHumidity() - 2); + humidity = atof(buf); + return humidity; + } + void readAll() { + readTemp(); + readHumidity(); + pressure = bme.readPressure() / 100; + altitude = bme.readAltitude(1006); + } + } data; + + void setup() { + Serial.println(F("BME280 setup")); + + if (!bme.begin(BME280_ADDRESS_ALTERNATE, &Wire)) { + Serial.println(F("Could not find a valid BME280 sensor, check wiring!")); + return; + } + + /* Default settings from datasheet. */ + bme.setSampling(Adafruit_BME280::MODE_NORMAL, /* Operating Mode. */ + Adafruit_BME280::SAMPLING_X2, // temperature + Adafruit_BME280::SAMPLING_X16, // pressure + Adafruit_BME280::SAMPLING_X2, // humidity + Adafruit_BME280::FILTER_X16, + Adafruit_BME280::STANDBY_MS_500); /* Standby time. */ + } +} \ No newline at end of file diff --git a/include/bmp.h b/include/bmp.h deleted file mode 100644 index 95c4ade..0000000 --- a/include/bmp.h +++ /dev/null @@ -1,59 +0,0 @@ -#pragma once - -#include - -namespace Bmp { - - Adafruit_BMP280 bmp; // I2C Interface - - struct { - float temp; - float pressure; - float altitude; - float readTemp() { - char buf[10]; - sprintf(buf, "%.1f", bmp.readTemperature() - 2); - temp = atof(buf); - return temp; - } - void readAll() { - readTemp(); - pressure = bmp.readPressure() / 100; - altitude = bmp.readAltitude(1006); - } - } data; - - void setup() { - Serial.begin(9600); - Serial.println(F("BMP280 setup")); - - if (!bmp.begin(0x76)) { - Serial.println(F("Could not find a valid BMP280 sensor, check wiring!")); - while (1); - } - - /* Default settings from datasheet. */ - bmp.setSampling(Adafruit_BMP280::MODE_NORMAL, /* Operating Mode. */ - Adafruit_BMP280::SAMPLING_X2, /* Temp. oversampling */ - Adafruit_BMP280::SAMPLING_X16, /* Pressure oversampling */ - Adafruit_BMP280::FILTER_X16, /* Filtering. */ - Adafruit_BMP280::STANDBY_MS_500); /* Standby time. */ - } - - void loop() { - Serial.print(F("Temperature = ")); - Serial.print(bmp.readTemperature()); - Serial.println(" *C"); - - Serial.print(F("Pressure = ")); - Serial.print(bmp.readPressure() / 100); //displaying the Pressure in hPa, you can change the unit - Serial.println(" hPa"); - - Serial.print(F("Approx altitude = ")); - Serial.print(bmp.readAltitude(1019.66)); //The "1019.66" is the pressure(hPa) at sea level in day in your region - Serial.println(" m"); //If you don't know it, modify it until you get your current altitude - - Serial.println(); - delay(2000); - } -} \ No newline at end of file diff --git a/include/display.h b/include/display.h index 21218cd..4f673ba 100644 --- a/include/display.h +++ b/include/display.h @@ -5,6 +5,7 @@ #include #include #include "ntp_time.h" +#include "bme.h" #define DISPLAY_ADDRESS 0x70 #define BRIGHTNESS_MIN 0 @@ -17,6 +18,8 @@ namespace Display { void displayTime(); + void displayTemp(); + void displayHumidity(); void drawTime(); void drawColon(bool); Task tDisplayTime(500, TASK_FOREVER, displayTime, &ts, false, []() { @@ -25,6 +28,12 @@ namespace Display { }, []() { drawColon(false); }); + Task tDisplaySensor(5*TASK_SECOND, 4, displayTemp, &ts, false, []() { + tDisplayTime.disable(); + return true; + }, []() { + tDisplayTime.enableDelayed(tDisplaySensor.getInterval()); + }); bool hourFormat24 = false; void (*hourFormatChangedCallback)(); @@ -104,11 +113,16 @@ namespace Display { colonOn = !colonOn; } - void displayTemp(float value) { - tDisplayTime.disable(); - clockDisplay.printFloat(value, 1); + void displayTemp() { + clockDisplay.printFloat(Bme::data.temp, 1); clockDisplay.writeDisplay(); - tDisplayTime.enableDelayed(SECONDS(10)); + tDisplaySensor.setCallback(&displayHumidity); + } + + void displayHumidity() { + clockDisplay.printFloat(Bme::data.humidity, 1); + clockDisplay.writeDisplay(); + tDisplaySensor.setCallback(&displayTemp); } void displayValue(uint8_t value) { diff --git a/include/ha.h b/include/ha.h index 7667155..e567c13 100644 --- a/include/ha.h +++ b/include/ha.h @@ -156,6 +156,14 @@ namespace Ha { } }; + struct HumiditySensor : Sensor { + HumiditySensor(const char* name, const char* id) : Sensor(name, id) { + deviceClass = "humidity"; + unitMeasure = "%"; + valueTemplate = "{{ value_json.humidity }}"; + } + }; + struct PressureSensor : Sensor { PressureSensor(const char* name, const char* id) : Sensor(name, id) { deviceClass = "pressure"; diff --git a/include/mqtt.h b/include/mqtt.h index 76cdc6e..c3cf829 100644 --- a/include/mqtt.h +++ b/include/mqtt.h @@ -3,7 +3,7 @@ #include #include #include "ha.h" -#include "bmp.h" +#include "bme.h" #include "display.h" #define MQTT_HOST IPAddress(192, 168, 5, 11) @@ -12,7 +12,7 @@ namespace Mqtt { void publishInit(); - void publishBmp280(); + void publishBme280(); void connect(); void disconnect(); Task tReConnect(5 * TASK_MINUTE, TASK_FOREVER, connect, &ts); @@ -37,6 +37,7 @@ namespace Mqtt { Ha::Sensor* sensors[] = { new Ha::TemperatureSensor{"Temperature", "temperature"}, + new Ha::HumiditySensor{"Humidity", "humidity"}, new Ha::PressureSensor{"Pressure", "pressure"}, new Ha::AltitudeSensor{"Altitude", "altitude"} }; @@ -101,11 +102,12 @@ namespace Mqtt { ts.deleteTask(tPublishInit); } - void publishBmp280() { + void publishBme280() { StaticJsonDocument<255> jsonDoc; - jsonDoc["temperature"] = Bmp::data.temp; - jsonDoc["pressure"] = Bmp::data.pressure; - jsonDoc["altitude"] = Bmp::data.altitude; + jsonDoc["temperature"] = Bme::data.temp; + jsonDoc["humidity"] = Bme::data.humidity; + jsonDoc["pressure"] = Bme::data.pressure; + jsonDoc["altitude"] = Bme::data.altitude; char message[255]; serializeJson(jsonDoc, message); publish(Ha::Sensor::stateTopic, message); diff --git a/platformio.ini b/platformio.ini index a6d8a43..13defb9 100644 --- a/platformio.ini +++ b/platformio.ini @@ -21,7 +21,7 @@ lib_deps = marvinroger/AsyncMqttClient@^0.9.0 arkhipenko/TaskScheduler@^3.7.0 adafruit/Adafruit Unified Sensor @ ^1.1.4 - adafruit/Adafruit BMP280 Library@^2.5.0 + adafruit/Adafruit BME280 Library@^2.2.4 bblanchon/ArduinoJson@6.16.1 build_flags = -D WIFI_ALWAYS_ON=1 diff --git a/src/esp_clock.cpp b/src/esp_clock.cpp index 39e7106..e2aa8a5 100644 --- a/src/esp_clock.cpp +++ b/src/esp_clock.cpp @@ -14,27 +14,29 @@ Task tWifiConnected(TASK_IMMEDIATE, TASK_ONCE, onWifiConnected, &ts); #include "wifi.h" #include "mqtt.h" #include "display.h" -#include "bmp.h" +#include "bme.h" #include "ntp_time.h" #include "ota.h" #define BUTTON D3 Task tButton(TASK_IMMEDIATE, TASK_ONCE, []() { - Display::displayTemp(Bmp::data.readTemp()); + Bme::data.readAll(); + Display::tDisplaySensor.restart(); }, &ts); Task tLed(TASK_IMMEDIATE, TASK_ONCE, []() { Mqtt::ledMqtt->publishState(); }, &ts); -Task tReadBmp(TASK_MINUTE, TASK_FOREVER, []() { +Task tReadBme(TASK_MINUTE, TASK_FOREVER, []() { static float lastTemp = 0; - float temp = Bmp::data.temp; - Bmp::data.readAll(); - if (temp == Bmp::data.temp) return; - Mqtt::publishBmp280(); - if (abs(lastTemp - Bmp::data.temp) > 0.2) { - lastTemp = Bmp::data.temp; - Display::displayTemp(Bmp::data.temp); + float temp = Bme::data.temp; + float humidity = Bme::data.humidity; + Bme::data.readAll(); + if ((temp == Bme::data.temp) && (humidity == Bme::data.humidity)) return; + Mqtt::publishBme280(); + if (abs(lastTemp - Bme::data.temp) > 0.2) { + lastTemp = Bme::data.temp; + Display::tDisplaySensor.restart(); } }, &ts); @@ -49,7 +51,7 @@ void setup() { Ota::setup(); Ntp::setup(); Mqtt::setup(); - Bmp::setup(); + Bme::setup(); Wifi::setup(); @@ -57,7 +59,7 @@ void setup() { attachInterrupt(digitalPinToInterrupt(BUTTON), onButtonPressed, FALLING); attachInterrupt(digitalPinToInterrupt(LED_BUILTIN), onLed, CHANGE); - tReadBmp.enable(); + tReadBme.enable(); } void loop() {