add bmp280
This commit is contained in:
parent
b3de99ea73
commit
d23014fb85
|
@ -0,0 +1,37 @@
|
|||
#ifndef SENSOR_BMP280_H
|
||||
#define SENSOR_BMP280_H
|
||||
|
||||
#include "sensordata.h"
|
||||
#include <Homie.h>
|
||||
|
||||
#include <Adafruit_BMP280.h>
|
||||
|
||||
|
||||
class Sensor_BMP280
|
||||
{
|
||||
|
||||
private:
|
||||
Adafruit_BMP280 *bmp280;
|
||||
HomieNode *sensorNode; //reference to HomieNode
|
||||
|
||||
struct sensordata data_temperature; //struct values are changed in setup()
|
||||
struct sensordata data_pressure; //struct values are changed in setup()
|
||||
|
||||
bool init_ok;
|
||||
|
||||
public:
|
||||
Sensor_BMP280();
|
||||
|
||||
void loop_BMP280_temperature();
|
||||
void loop_BMP280_pressure();
|
||||
|
||||
void init();
|
||||
void setSettings_Temperature(float minchange, unsigned long senddelaymax, unsigned long readdelay);
|
||||
void setSettings_Pressure(float minchange, unsigned long senddelaymax, unsigned long readdelay);
|
||||
void advertise(HomieNode& p_sensorNode);
|
||||
void sensorloop();
|
||||
|
||||
};
|
||||
|
||||
#endif
|
||||
|
|
@ -379,13 +379,15 @@ build_flags =
|
|||
-D SENSOR_DHT22_temperature_minchange=0.2
|
||||
-D SENSOR_DHT22_humidity_minchange=2.0
|
||||
|
||||
-D SENSOR_BMP180
|
||||
-D dataBMP180_temperature_minchange=0.2
|
||||
-D dataBMP180_pressure_minchange=0.5
|
||||
-D SENSOR_BMP280
|
||||
-D PIN_SDA=D2
|
||||
-D PIN_SCL=D1
|
||||
-D dataBMP280_temperature_minchange=0.2
|
||||
-D dataBMP280_pressure_minchange=0.5
|
||||
|
||||
|
||||
lib_deps =
|
||||
Adafruit BMP085 Library@1.1.0
|
||||
adafruit/Adafruit BMP280 Library@^2.6.8
|
||||
DHT sensor library@1.3.10
|
||||
ArduinoJson@6.16.1 #dependency of homie. using older version because of "ambiguous overload for operator|" error
|
||||
Homie@3.0.0
|
||||
|
|
43
src/main.cpp
43
src/main.cpp
|
@ -82,6 +82,34 @@ HomieNode sensorNode("sensors", "Sensors","sensors"); //id, name, type
|
|||
#endif
|
||||
#endif
|
||||
|
||||
|
||||
#ifdef SENSOR_BMP280
|
||||
#include "sensor_bmp280.h"
|
||||
Sensor_BMP280 sensor_bmp280;
|
||||
|
||||
#ifndef SENSOR_BMP280_temperature_minchange
|
||||
#define SENSOR_BMP280_temperature_minchange 0.2
|
||||
#endif
|
||||
#ifndef SENSOR_BMP280_temperature_senddelaymax
|
||||
#define SENSOR_BMP280_temperature_senddelaymax 5*60*1000
|
||||
#endif
|
||||
#ifndef SENSOR_BMP280_temperature_readdelay
|
||||
#define SENSOR_BMP280_temperature_readdelay 10000
|
||||
#endif
|
||||
|
||||
#ifndef SENSOR_BMP280_pressure_minchange
|
||||
#define SENSOR_BMP280_pressure_minchange 0.5
|
||||
#endif
|
||||
#ifndef SENSOR_BMP280_pressure_senddelaymax
|
||||
#define SENSOR_BMP280_pressure_senddelaymax 30*60*1000
|
||||
#endif
|
||||
#ifndef SENSOR_BMP280_pressure_readdelay
|
||||
#define SENSOR_BMP280_pressure_readdelay 10000
|
||||
#endif
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
#ifdef SENSOR_HTU21D
|
||||
#include "sensor_htu21d.h"
|
||||
Sensor_HTU21D sensor_htu21d;
|
||||
|
@ -454,6 +482,13 @@ void setup() {
|
|||
sensor_bmp180.setSettings_Pressure(SENSOR_BMP180_temperature_minchange,SENSOR_BMP180_temperature_senddelaymax,SENSOR_BMP180_temperature_readdelay);
|
||||
#endif
|
||||
|
||||
|
||||
#ifdef SENSOR_BMP280
|
||||
sensor_bmp280.init();
|
||||
sensor_bmp280.setSettings_Temperature(SENSOR_BMP280_temperature_minchange,SENSOR_BMP280_temperature_senddelaymax,SENSOR_BMP280_temperature_readdelay);
|
||||
sensor_bmp280.setSettings_Pressure(SENSOR_BMP280_temperature_minchange,SENSOR_BMP280_temperature_senddelaymax,SENSOR_BMP280_temperature_readdelay);
|
||||
#endif
|
||||
|
||||
#ifdef SENSOR_HTU21D
|
||||
sensor_htu21d.init();
|
||||
sensor_htu21d.setSettings_Temperature(SENSOR_HTU21D_temperature_minchange,SENSOR_HTU21D_temperature_senddelaymax,SENSOR_HTU21D_temperature_readdelay);
|
||||
|
@ -563,6 +598,10 @@ void setup() {
|
|||
sensor_bmp180.advertise(sensorNode);
|
||||
#endif
|
||||
|
||||
#ifdef SENSOR_BMP280
|
||||
sensor_bmp280.advertise(sensorNode);
|
||||
#endif
|
||||
|
||||
#ifdef SENSOR_HTU21D
|
||||
sensor_htu21d.advertise(sensorNode);
|
||||
#endif
|
||||
|
@ -648,6 +687,10 @@ void loopHandler() {
|
|||
sensor_bmp180.sensorloop();
|
||||
#endif
|
||||
|
||||
#ifdef SENSOR_BMP280
|
||||
sensor_bmp280.sensorloop();
|
||||
#endif
|
||||
|
||||
#ifdef SENSOR_HTU21D
|
||||
sensor_htu21d.sensorloop();
|
||||
#endif
|
||||
|
|
|
@ -0,0 +1,115 @@
|
|||
#ifdef SENSOR_BMP280
|
||||
//Connect SCL to D1, SDA to D2, GND and 3v3
|
||||
#include "sensor_bmp280.h"
|
||||
|
||||
|
||||
|
||||
Sensor_BMP280::Sensor_BMP280()
|
||||
{
|
||||
bmp280 = new Adafruit_BMP280();
|
||||
}
|
||||
|
||||
void Sensor_BMP280::init() //Things to be done during setup()
|
||||
{
|
||||
Serial.println("initializing bmp280");
|
||||
if (!bmp280->begin(0x76)){
|
||||
Serial.println("#ERROR: bmp280 init fail\n");
|
||||
}else{
|
||||
init_ok=true; //stays false if init failed, sensor will not be read in loop
|
||||
|
||||
bmp280->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. */
|
||||
}
|
||||
}
|
||||
|
||||
//Also called during setup()
|
||||
void Sensor_BMP280::setSettings_Temperature(float minchange, unsigned long senddelaymax, unsigned long readdelay)
|
||||
{
|
||||
data_temperature.minchange=minchange;
|
||||
data_temperature.senddelaymax=senddelaymax;
|
||||
data_temperature.readdelay=readdelay;
|
||||
}
|
||||
|
||||
//Also called during setup()
|
||||
void Sensor_BMP280::setSettings_Pressure(float minchange, unsigned long senddelaymax, unsigned long readdelay)
|
||||
{
|
||||
data_pressure.minchange=minchange;
|
||||
data_pressure.senddelaymax=senddelaymax;
|
||||
data_pressure.readdelay=readdelay;
|
||||
}
|
||||
|
||||
//Called during setup
|
||||
void Sensor_BMP280::advertise(HomieNode& p_sensorNode)
|
||||
{
|
||||
sensorNode = &p_sensorNode;
|
||||
sensorNode->advertise("temperature");
|
||||
sensorNode->advertise("pressure");
|
||||
}
|
||||
|
||||
void Sensor_BMP280::sensorloop()
|
||||
{
|
||||
if (init_ok) {
|
||||
loop_BMP280_temperature();
|
||||
loop_BMP280_pressure();
|
||||
}
|
||||
}
|
||||
|
||||
void Sensor_BMP280::loop_BMP280_temperature()
|
||||
{
|
||||
sensordata &d=data_temperature;
|
||||
bool _changed=false;
|
||||
|
||||
if (millis() >= (d.lastreadtime+d.readdelay)) {
|
||||
d.value = bmp280->readTemperature();
|
||||
if (fabs(d.lastsentvalue-d.value)>=d.minchange){
|
||||
_changed=true;
|
||||
}
|
||||
d.lastreadtime=millis();
|
||||
}
|
||||
|
||||
if (_changed || millis() >= (d.lastsent+d.senddelaymax)) {
|
||||
Serial.print("Sending bmp280_temperature. reason=");
|
||||
if (_changed) Serial.println("change"); else Serial.println("time");
|
||||
|
||||
|
||||
if (!(isnan(d.value) == 1)){ //success
|
||||
sensorNode->setProperty("temperature").send(String(d.value));
|
||||
Homie.getLogger() << "temperature " << ": " << d.value << endl;
|
||||
d.lastsentvalue=d.value;
|
||||
}
|
||||
|
||||
d.lastsent=millis();
|
||||
}
|
||||
}
|
||||
|
||||
void Sensor_BMP280::loop_BMP280_pressure()
|
||||
{
|
||||
sensordata &d=data_pressure;
|
||||
bool _changed=false;
|
||||
|
||||
if (millis() >= (d.lastreadtime+d.readdelay)) {
|
||||
d.value = bmp280->readPressure()/100.0;
|
||||
if (fabs(d.lastsentvalue-d.value)>=d.minchange){
|
||||
_changed=true;
|
||||
}
|
||||
d.lastreadtime=millis();
|
||||
}
|
||||
|
||||
if (_changed || millis() >= (d.lastsent+d.senddelaymax)) {
|
||||
Serial.print("Sending bmp280_pressure. reason=");
|
||||
if (_changed) Serial.println("change"); else Serial.println("time");
|
||||
|
||||
if (!(isnan(d.value) == 1)){ //success
|
||||
Homie.getLogger() << "pressure " << ": " << d.value << endl;
|
||||
sensorNode->setProperty("pressure").send(String(d.value));
|
||||
d.lastsentvalue=d.value;
|
||||
}
|
||||
d.lastsent=millis();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#endif
|
Loading…
Reference in New Issue