WLED/usermods/BME280_v2/usermod_bme280.h

216 lines
7.0 KiB
C
Raw Permalink Normal View History

2020-12-25 21:17:30 +01:00
#pragma once
#include "wled.h"
#include <Arduino.h>
#include <Wire.h>
#include <BME280I2C.h> // BME280 sensor
#include <EnvironmentCalculations.h> // BME280 extended measurements
class UsermodBME280 : public Usermod
{
private:
// User-defined configuration
#define Celsius // Show temperature mesaurement in Celcius. Comment out for Fahrenheit
#define TemperatureDecimals 1 // Number of decimal places in published temperaure values
2021-04-12 10:31:59 +02:00
#define HumidityDecimals 2 // Number of decimal places in published humidity values
2020-12-25 21:17:30 +01:00
#define PressureDecimals 2 // Number of decimal places in published pressure values
#define TemperatureInterval 5 // Interval to measure temperature (and humidity, dew point if available) in seconds
#define PressureInterval 300 // Interval to measure pressure in seconds
2021-04-12 10:31:59 +02:00
#define PublishAlways 0 // Publish values even when they have not changed
2020-12-25 21:17:30 +01:00
// Sanity checks
#if !defined(TemperatureDecimals) || TemperatureDecimals < 0
#define TemperatureDecimals 0
#endif
#if !defined(HumidityDecimals) || HumidityDecimals < 0
#define HumidityDecimals 0
#endif
#if !defined(PressureDecimals) || PressureDecimals < 0
#define PressureDecimals 0
#endif
#if !defined(TemperatureInterval) || TemperatureInterval < 0
#define TemperatureInterval 1
#endif
#if !defined(PressureInterval) || PressureInterval < 0
#define PressureInterval TemperatureInterval
#endif
2021-04-12 10:31:59 +02:00
#if !defined(PublishAlways)
#define PublishAlways 0
#endif
2020-12-25 21:17:30 +01:00
#ifdef ARDUINO_ARCH_ESP32 // ESP32 boards
uint8_t SCL_PIN = 22;
uint8_t SDA_PIN = 21;
#else // ESP8266 boards
uint8_t SCL_PIN = 5;
uint8_t SDA_PIN = 4;
//uint8_t RST_PIN = 16; // Uncoment for Heltec WiFi-Kit-8
#endif
// BME280 sensor settings
BME280I2C::Settings settings{
BME280::OSR_X16, // Temperature oversampling x16
BME280::OSR_X16, // Humidity oversampling x16
BME280::OSR_X16, // Pressure oversampling x16
// Defaults
BME280::Mode_Forced,
BME280::StandbyTime_1000ms,
BME280::Filter_Off,
BME280::SpiEnable_False,
BME280I2C::I2CAddr_0x76 // I2C address. I2C specific. Default 0x76
};
BME280I2C bme{settings};
2021-04-12 10:31:59 +02:00
uint8_t sensorType;
2020-12-25 21:17:30 +01:00
// Measurement timers
long timer;
long lastTemperatureMeasure = 0;
long lastPressureMeasure = 0;
// Current sensor values
2021-04-12 10:31:59 +02:00
float sensorTemperature;
float sensorHumidity;
float sensorHeatIndex;
float sensorDewPoint;
float sensorPressure;
2020-12-25 21:17:30 +01:00
// Track previous sensor values
float lastTemperature;
float lastHumidity;
float lastHeatIndex;
float lastDewPoint;
float lastPressure;
// Store packet IDs of MQTT publications
uint16_t mqttTemperaturePub = 0;
uint16_t mqttPressurePub = 0;
void UpdateBME280Data(int SensorType)
{
float _temperature, _humidity, _pressure;
#ifdef Celsius
BME280::TempUnit tempUnit(BME280::TempUnit_Celsius);
EnvironmentCalculations::TempUnit envTempUnit(EnvironmentCalculations::TempUnit_Celsius);
#else
BME280::TempUnit tempUnit(BME280::TempUnit_Fahrenheit);
EnvironmentCalculations::TempUnit envTempUnit(EnvironmentCalculations::TempUnit_Fahrenheit);
#endif
BME280::PresUnit presUnit(BME280::PresUnit_hPa);
bme.read(_pressure, _temperature, _humidity, tempUnit, presUnit);
2021-04-12 10:31:59 +02:00
sensorTemperature = _temperature;
sensorHumidity = _humidity;
sensorPressure = _pressure;
if (sensorType == 1)
2020-12-25 21:17:30 +01:00
{
2021-04-12 10:31:59 +02:00
sensorHeatIndex = EnvironmentCalculations::HeatIndex(_temperature, _humidity, envTempUnit);
sensorDewPoint = EnvironmentCalculations::DewPoint(_temperature, _humidity, envTempUnit);
2020-12-25 21:17:30 +01:00
}
}
public:
void setup()
{
Wire.begin(SDA_PIN, SCL_PIN);
if (!bme.begin())
{
2021-04-12 10:31:59 +02:00
sensorType = 0;
2020-12-25 21:17:30 +01:00
Serial.println("Could not find BME280I2C sensor!");
}
else
{
switch (bme.chipModel())
{
case BME280::ChipModel_BME280:
2021-04-12 10:31:59 +02:00
sensorType = 1;
2020-12-25 21:17:30 +01:00
Serial.println("Found BME280 sensor! Success.");
break;
case BME280::ChipModel_BMP280:
2021-04-12 10:31:59 +02:00
sensorType = 2;
2020-12-25 21:17:30 +01:00
Serial.println("Found BMP280 sensor! No Humidity available.");
break;
default:
2021-04-12 10:31:59 +02:00
sensorType = 0;
2020-12-25 21:17:30 +01:00
Serial.println("Found UNKNOWN sensor! Error!");
}
}
}
void loop()
{
// BME280 sensor MQTT publishing
// Check if sensor present and MQTT Connected, otherwise it will crash the MCU
2021-04-12 10:31:59 +02:00
if (sensorType != 0 && mqtt != nullptr)
2020-12-25 21:17:30 +01:00
{
// Timer to fetch new temperature, humidity and pressure data at intervals
timer = millis();
if (timer - lastTemperatureMeasure >= TemperatureInterval * 1000 || mqttTemperaturePub == 0)
{
lastTemperatureMeasure = timer;
2021-04-12 10:31:59 +02:00
UpdateBME280Data(sensorType);
2020-12-25 21:17:30 +01:00
2021-04-12 10:31:59 +02:00
float temperature = roundf(sensorTemperature * pow(10, TemperatureDecimals)) / pow(10, TemperatureDecimals);
float humidity, heatIndex, dewPoint;
2020-12-25 21:17:30 +01:00
// If temperature has changed since last measure, create string populated with device topic
// from the UI and values read from sensor, then publish to broker
2021-04-12 10:31:59 +02:00
if (temperature != lastTemperature || PublishAlways)
2020-12-25 21:17:30 +01:00
{
String topic = String(mqttDeviceTopic) + "/temperature";
2021-04-12 10:31:59 +02:00
mqttTemperaturePub = mqtt->publish(topic.c_str(), 0, false, String(temperature, TemperatureDecimals).c_str());
2020-12-25 21:17:30 +01:00
}
2021-04-12 10:31:59 +02:00
lastTemperature = temperature; // Update last sensor temperature for next loop
2020-12-25 21:17:30 +01:00
2021-04-12 10:31:59 +02:00
if (sensorType == 1) // Only if sensor is a BME280
2020-12-25 21:17:30 +01:00
{
2021-04-12 10:31:59 +02:00
humidity = roundf(sensorHumidity * pow(10, HumidityDecimals)) / pow(10, HumidityDecimals);
heatIndex = roundf(sensorHeatIndex * pow(10, TemperatureDecimals)) / pow(10, TemperatureDecimals);
dewPoint = roundf(sensorDewPoint * pow(10, TemperatureDecimals)) / pow(10, TemperatureDecimals);
2020-12-25 21:17:30 +01:00
2021-04-12 10:31:59 +02:00
if (humidity != lastHumidity || PublishAlways)
2020-12-25 21:17:30 +01:00
{
String topic = String(mqttDeviceTopic) + "/humidity";
2021-04-12 10:31:59 +02:00
mqtt->publish(topic.c_str(), 0, false, String(humidity, HumidityDecimals).c_str());
2020-12-25 21:17:30 +01:00
}
2021-04-12 10:31:59 +02:00
if (heatIndex != lastHeatIndex || PublishAlways)
2020-12-25 21:17:30 +01:00
{
String topic = String(mqttDeviceTopic) + "/heat_index";
2021-04-12 10:31:59 +02:00
mqtt->publish(topic.c_str(), 0, false, String(heatIndex, TemperatureDecimals).c_str());
2020-12-25 21:17:30 +01:00
}
2021-04-12 10:31:59 +02:00
if (dewPoint != lastDewPoint || PublishAlways)
2020-12-25 21:17:30 +01:00
{
String topic = String(mqttDeviceTopic) + "/dew_point";
2021-04-12 10:31:59 +02:00
mqtt->publish(topic.c_str(), 0, false, String(dewPoint, TemperatureDecimals).c_str());
2020-12-25 21:17:30 +01:00
}
2021-04-12 10:31:59 +02:00
lastHumidity = humidity;
lastHeatIndex = heatIndex;
lastDewPoint = dewPoint;
2020-12-25 21:17:30 +01:00
}
}
if (timer - lastPressureMeasure >= PressureInterval * 1000 || mqttPressurePub == 0)
{
lastPressureMeasure = timer;
2021-04-12 10:31:59 +02:00
float pressure = roundf(sensorPressure * pow(10, PressureDecimals)) / pow(10, PressureDecimals);
2020-12-25 21:17:30 +01:00
2021-04-12 10:31:59 +02:00
if (pressure != lastPressure || PublishAlways)
2020-12-25 21:17:30 +01:00
{
String topic = String(mqttDeviceTopic) + "/pressure";
2021-04-12 10:31:59 +02:00
mqttPressurePub = mqtt->publish(topic.c_str(), 0, true, String(pressure, PressureDecimals).c_str());
2020-12-25 21:17:30 +01:00
}
2021-04-12 10:31:59 +02:00
lastPressure = pressure;
2020-12-25 21:17:30 +01:00
}
}
}
};