Created
December 13, 2020 01:34
-
-
Save debsahu/037068aef72c5eb5915dfb8a2235cd2a to your computer and use it in GitHub Desktop.
BME/BMP 280 usermod for WLED
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "wled.h" | |
#include <Arduino.h> | |
#include <Wire.h> | |
#include <BME280I2C.h> //BME280 sensor | |
void UpdateBME280Data(); | |
#define Celsius // Show temperature mesaurement in Celcius otherwise is in Fahrenheit | |
BME280I2C bme; // Default : forced mode, standby time = 1000 ms | |
// Oversampling = pressure ×1, temperature ×1, humidity ×1, filter off, | |
#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 timer | |
long tempTimer = millis(); | |
long lastMeasure = 0; | |
float SensorPressure(NAN); | |
float SensorTemperature(NAN); | |
float SensorHumidity(NAN); | |
void userSetup() { | |
Wire.begin(SDA_PIN,SCL_PIN); | |
while(!bme.begin()) | |
{ | |
Serial.println("Could not find BME280I2C sensor!"); | |
delay(1000); | |
} | |
switch(bme.chipModel()) | |
{ | |
case BME280::ChipModel_BME280: | |
Serial.println("Found BME280 sensor! Success."); | |
break; | |
case BME280::ChipModel_BMP280: | |
Serial.println("Found BMP280 sensor! No Humidity available."); | |
break; | |
default: | |
Serial.println("Found UNKNOWN sensor! Error!"); | |
} | |
} | |
// gets called every time WiFi is (re-)connected. Initialize own network | |
// interfaces here | |
void userConnected() {} | |
void userLoop() { | |
// BME280 sensor MQTT publishing | |
tempTimer = millis(); | |
// Timer to publish new sensor data every 60 seconds | |
if (tempTimer - lastMeasure > 60000) | |
{ | |
lastMeasure = tempTimer; | |
// Check if MQTT Connected, otherwise it will crash the 8266 | |
if (mqtt != nullptr) | |
{ | |
UpdateBME280Data(); | |
float board_temperature = SensorTemperature; | |
float board_pressure = SensorPressure; | |
float board_humidity = SensorHumidity; | |
// Create string populated with user defined device topic from the UI, and the read temperature, humidity and pressure. Then publish to MQTT server. | |
String t = String(mqttDeviceTopic); | |
t += "/temperature"; | |
mqtt->publish(t.c_str(), 0, true, String(board_temperature).c_str()); | |
String p = String(mqttDeviceTopic); | |
p += "/pressure"; | |
mqtt->publish(p.c_str(), 0, true, String(board_pressure).c_str()); | |
String h = String(mqttDeviceTopic); | |
h += "/humidity"; | |
mqtt->publish(h.c_str(), 0, true, String(board_humidity).c_str()); | |
} | |
} | |
} | |
void UpdateBME280Data() { | |
float temp(NAN), hum(NAN), pres(NAN); | |
#ifdef Celsius | |
BME280::TempUnit tempUnit(BME280::TempUnit_Celsius); | |
#else | |
BME280::TempUnit tempUnit(BME280::TempUnit_Fahrenheit); | |
#endif | |
BME280::PresUnit presUnit(BME280::PresUnit_Pa); | |
bme.read(pres, temp, hum, tempUnit, presUnit); | |
SensorTemperature=temp; | |
SensorHumidity=hum; | |
SensorPressure=pres; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment