move bmp180 to class
This commit is contained in:
parent
24fe2af8b7
commit
ac81c74dbd
|
@ -0,0 +1,105 @@
|
|||
//Connect SCL to D1, SDA to D2, GND and 3v3
|
||||
#include "sensor_bmp180.h"
|
||||
|
||||
|
||||
|
||||
Sensor_BMP180::Sensor_BMP180()
|
||||
{
|
||||
Adafruit_BMP085 bmp180;
|
||||
}
|
||||
|
||||
void Sensor_BMP180::init() //Things to be done during setup()
|
||||
{
|
||||
Serial.println("initializing bmp180");
|
||||
if (!bmp180->begin()){
|
||||
Serial.println("#ERROR: BMP180 init fail\n");
|
||||
}else{
|
||||
init_ok=true; //stays false if init failed, sensor will not be read in loop
|
||||
}
|
||||
}
|
||||
|
||||
//Also called during setup()
|
||||
void Sensor_BMP180::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_BMP180::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_BMP180::advertise(HomieNode& p_sensorNode)
|
||||
{
|
||||
sensorNode = &p_sensorNode;
|
||||
sensorNode->advertise("temperature");
|
||||
sensorNode->advertise("pressure");
|
||||
}
|
||||
|
||||
void Sensor_BMP180::sensorloop()
|
||||
{
|
||||
if (init_ok) {
|
||||
loop_BMP180_temperature();
|
||||
loop_BMP180_pressure();
|
||||
}
|
||||
}
|
||||
|
||||
void Sensor_BMP180::loop_BMP180_temperature()
|
||||
{
|
||||
sensordata &d=data_temperature;
|
||||
bool _changed=false;
|
||||
|
||||
if (millis() >= (d.lastreadtime+d.readdelay)) {
|
||||
d.value = bmp180->readTemperature();
|
||||
if (fabs(d.lastsentvalue-d.value)>=d.minchange){
|
||||
_changed=true;
|
||||
}
|
||||
d.lastreadtime=millis();
|
||||
}
|
||||
|
||||
if (_changed || millis() >= (d.lastsent+d.senddelaymax)) {
|
||||
Serial.print("Sending BMP180_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_BMP180::loop_BMP180_pressure()
|
||||
{
|
||||
sensordata &d=data_pressure;
|
||||
bool _changed=false;
|
||||
|
||||
if (millis() >= (d.lastreadtime+d.readdelay)) {
|
||||
d.value = bmp180->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 BMP180_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();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,37 @@
|
|||
#ifndef SENSOR_BMP180_H
|
||||
#define SENSOR_BMP180_H
|
||||
|
||||
#include "sensordata.h"
|
||||
#include <Homie.h>
|
||||
|
||||
#include <Adafruit_BMP085.h>
|
||||
|
||||
|
||||
class Sensor_BMP180
|
||||
{
|
||||
|
||||
private:
|
||||
Adafruit_BMP085 *bmp180;
|
||||
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_BMP180();
|
||||
|
||||
void loop_BMP180_temperature();
|
||||
void loop_BMP180_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
|
||||
|
|
@ -1,26 +1,6 @@
|
|||
// Digital pin connected to the DHT sensor. // dht pins: 1=power, 2=data, 3=NC, 4=GND. 10k from data to power needed
|
||||
#include "sensor_dht22.h"
|
||||
|
||||
#ifndef SENSOR_DHT22_temperature_minchange
|
||||
#define SENSOR_DHT22_temperature_minchange 0.2
|
||||
#endif
|
||||
#ifndef SENSOR_DHT22_temperature_senddelaymax
|
||||
#define SENSOR_DHT22_temperature_senddelaymax 300000
|
||||
#endif
|
||||
#ifndef SENSOR_DHT22_temperature_readdelay
|
||||
#define SENSOR_DHT22_temperature_readdelay 10000
|
||||
#endif
|
||||
|
||||
#ifndef SENSOR_DHT22_humidity_minchange
|
||||
#define SENSOR_DHT22_humidity_minchange 2.0
|
||||
#endif
|
||||
#ifndef SENSOR_DHT22_humidity_senddelaymax
|
||||
#define SENSOR_DHT22_humidity_senddelaymax 300000
|
||||
#endif
|
||||
#ifndef SENSOR_DHT22_humidity_readdelay
|
||||
#define SENSOR_DHT22_humidity_readdelay 10000
|
||||
#endif
|
||||
|
||||
Sensor_DHT22::Sensor_DHT22(int pin)
|
||||
{
|
||||
dht = new DHT(pin,DHT22,11); //default:11
|
||||
|
|
|
@ -1,19 +1,14 @@
|
|||
#ifndef SENSOR_DHT22_H
|
||||
#define SENSOR_DHT22_H
|
||||
|
||||
#ifndef Adafruit_Sensor_H
|
||||
|
||||
#include "sensordata.h"
|
||||
#include <Homie.h>
|
||||
|
||||
#include <Adafruit_Sensor.h> //required for dht library
|
||||
#define Adafruit_Sensor_H
|
||||
#endif
|
||||
#include <DHT.h>
|
||||
|
||||
|
||||
#include "sensordata.h"
|
||||
|
||||
#ifndef HOMIE_H
|
||||
#include <Homie.h>
|
||||
#define HOMIE_H
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
|
|
|
@ -87,10 +87,16 @@ build_flags =
|
|||
-D SENSOR_DHT22_temperature_minchange=0.2
|
||||
-D SENSOR_DHT22_humidity_minchange=2.0
|
||||
|
||||
|
||||
-D SENSOR_BMP180
|
||||
-D SENSOR_BMP180_temperature_minchange=0.2
|
||||
-D SENSOR_BMP180_pressure_minchange=0.5
|
||||
|
||||
lib_deps =
|
||||
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
|
||||
Adafruit BMP085 Library@1.1.0
|
||||
|
||||
#Arbeitszimmer
|
||||
[env:sensoresp1]
|
||||
|
|
147
src/main.cpp
147
src/main.cpp
|
@ -25,34 +25,55 @@
|
|||
|
||||
|
||||
|
||||
/*
|
||||
#ifdef SENSOR_DHT22
|
||||
// Digital pin connected to the DHT sensor. // dht pins: 1=power, 2=data, 3=NC, 4=GND. 10k from data to power needed
|
||||
#include <Adafruit_Sensor.h> //required for dht library
|
||||
#include <DHT.h>
|
||||
DHT dht(DHTPIN,DHT22,11); //default:11
|
||||
|
||||
struct sensordata dataDHT22_temperature; //struct values are changed in setup()
|
||||
float value_temperatureDHT=0;
|
||||
struct sensordata dataDHT22_humidity; //struct values are changed in setup()
|
||||
float value_humidityDHT=0;
|
||||
#endif
|
||||
*/
|
||||
#ifdef SENSOR_DHT22
|
||||
#include "sensor_dht22.cpp"
|
||||
Sensor_DHT22 sensor_dht22(SENSOR_DHT22_PIN);
|
||||
|
||||
#ifndef SENSOR_DHT22_temperature_minchange
|
||||
#define SENSOR_DHT22_temperature_minchange 0.2
|
||||
#endif
|
||||
#ifndef SENSOR_DHT22_temperature_senddelaymax
|
||||
#define SENSOR_DHT22_temperature_senddelaymax 300000
|
||||
#endif
|
||||
#ifndef SENSOR_DHT22_temperature_readdelay
|
||||
#define SENSOR_DHT22_temperature_readdelay 10000
|
||||
#endif
|
||||
|
||||
#ifndef SENSOR_DHT22_humidity_minchange
|
||||
#define SENSOR_DHT22_humidity_minchange 2.0
|
||||
#endif
|
||||
#ifndef SENSOR_DHT22_humidity_senddelaymax
|
||||
#define SENSOR_DHT22_humidity_senddelaymax 300000
|
||||
#endif
|
||||
#ifndef SENSOR_DHT22_humidity_readdelay
|
||||
#define SENSOR_DHT22_humidity_readdelay 10000
|
||||
#endif
|
||||
#endif
|
||||
|
||||
|
||||
#ifdef SENSOR_BMP180
|
||||
//Connect SCL to D1, SDA to D2, GND and 3v3
|
||||
#include <Adafruit_BMP085.h>
|
||||
bool bmp180init_ok=false;
|
||||
Adafruit_BMP085 bmp180;
|
||||
struct sensordata dataBMP180_temperature; //struct values are changed in setup()
|
||||
float value_temperatureBMP=0;
|
||||
struct sensordata dataBMP180_pressure; //struct values are changed in setup()
|
||||
float value_pressureBMP=0;
|
||||
#include "sensor_bmp180.cpp"
|
||||
Sensor_BMP180 sensor_bmp180;
|
||||
|
||||
#ifndef SENSOR_BMP180_temperature_minchange
|
||||
#define SENSOR_BMP180_temperature_minchange 0.2
|
||||
#endif
|
||||
#ifndef SENSOR_BMP180_temperature_senddelaymax
|
||||
#define SENSOR_BMP180_temperature_senddelaymax 300000
|
||||
#endif
|
||||
#ifndef SENSOR_BMP180_temperature_readdelay
|
||||
#define SENSOR_BMP180_temperature_readdelay 10000
|
||||
#endif
|
||||
|
||||
#ifndef SENSOR_BMP180_pressure_minchange
|
||||
#define SENSOR_BMP180_pressure_minchange 2.0
|
||||
#endif
|
||||
#ifndef SENSOR_BMP180_pressure_senddelaymax
|
||||
#define SENSOR_BMP180_pressure_senddelaymax 300000
|
||||
#endif
|
||||
#ifndef SENSOR_BMP180_pressure_readdelay
|
||||
#define SENSOR_BMP180_pressure_readdelay 10000
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#ifdef SENSOR_HTU21D
|
||||
|
@ -314,18 +335,9 @@ void setup() {
|
|||
|
||||
|
||||
#ifdef SENSOR_BMP180
|
||||
Serial.println("initializing bmp180");
|
||||
if (!bmp180.begin()){
|
||||
Serial.println("#ERROR: BMP180 init fail\n");
|
||||
}else{
|
||||
bmp180init_ok=true; //stays false if init failed, sensor will not be read in loop
|
||||
}
|
||||
#ifdef dataBMP180_temperature_minchange
|
||||
dataBMP180_temperature.minchange=dataBMP180_temperature_minchange;
|
||||
#endif
|
||||
#ifdef dataBMP180_pressure_minchange
|
||||
dataBMP180_pressure.minchange=dataBMP180_pressure_minchange;
|
||||
#endif
|
||||
sensor_bmp180.init();
|
||||
sensor_bmp180.setSettings_Temperature(SENSOR_BMP180_temperature_minchange,SENSOR_BMP180_temperature_senddelaymax,SENSOR_BMP180_temperature_readdelay);
|
||||
sensor_bmp180.setSettings_Pressure(SENSOR_BMP180_temperature_minchange,SENSOR_BMP180_temperature_senddelaymax,SENSOR_BMP180_temperature_readdelay);
|
||||
#endif
|
||||
|
||||
#ifdef SENSOR_HTU21D
|
||||
|
@ -541,6 +553,10 @@ void setup() {
|
|||
sensor_dht22.advertise(sensorNode);
|
||||
#endif
|
||||
|
||||
#ifdef SENSOR_BMP180
|
||||
sensor_bmp180.advertise(sensorNode);
|
||||
#endif
|
||||
|
||||
#ifdef SENSOR_HS1101
|
||||
#ifdef SENSOR_DHT22
|
||||
sensorNode.advertise("humidity_hs1101");
|
||||
|
@ -573,10 +589,6 @@ void setup() {
|
|||
sensorNode.advertise("radar");
|
||||
#endif
|
||||
|
||||
#ifdef SENSOR_BMP180
|
||||
sensorNode.advertise("temperature");
|
||||
sensorNode.advertise("pressure");
|
||||
#endif
|
||||
|
||||
#ifdef SENSOR_HTU21D
|
||||
sensorNode.advertise("temperature_htu");
|
||||
|
@ -632,62 +644,6 @@ void loop() {
|
|||
|
||||
|
||||
|
||||
#ifdef SENSOR_BMP180
|
||||
void loop_BMP180_temperature()
|
||||
{
|
||||
sensordata &d=dataBMP180_temperature;
|
||||
bool _changed=false;
|
||||
|
||||
if (millis() >= (d.lastreadtime+d.readdelay)) {
|
||||
value_temperatureBMP = bmp180.readTemperature();
|
||||
if (fabs(d.lastsentvalue-value_temperatureBMP)>=d.minchange){
|
||||
_changed=true;
|
||||
}
|
||||
d.lastreadtime=millis();
|
||||
}
|
||||
|
||||
if (_changed || millis() >= (d.lastsent+d.senddelaymax)) {
|
||||
Serial.print("Sending BMP180_temperature. reason=");
|
||||
if (_changed) Serial.println("change"); else Serial.println("time");
|
||||
checkESPStatus();
|
||||
|
||||
if (!(isnan(value_temperatureBMP) == 1)){ //success
|
||||
sensorNode.setProperty("temperature").send(String(value_temperatureBMP));
|
||||
Homie.getLogger() << "temperature " << ": " << value_temperatureBMP << endl;
|
||||
d.lastsentvalue=value_temperatureBMP;
|
||||
}
|
||||
|
||||
d.lastsent=millis();
|
||||
}
|
||||
}
|
||||
|
||||
void loop_BMP180_pressure()
|
||||
{
|
||||
sensordata &d=dataBMP180_pressure;
|
||||
bool _changed=false;
|
||||
|
||||
if (millis() >= (d.lastreadtime+d.readdelay)) {
|
||||
value_pressureBMP = bmp180.readPressure()/100.0;
|
||||
if (fabs(d.lastsentvalue-value_pressureBMP)>=d.minchange){
|
||||
_changed=true;
|
||||
}
|
||||
d.lastreadtime=millis();
|
||||
}
|
||||
|
||||
if (_changed || millis() >= (d.lastsent+d.senddelaymax)) {
|
||||
Serial.print("Sending BMP180_pressure. reason=");
|
||||
if (_changed) Serial.println("change"); else Serial.println("time");
|
||||
checkESPStatus();
|
||||
|
||||
if (!(isnan(value_pressureBMP) == 1)){ //success
|
||||
Homie.getLogger() << "pressure " << ": " << value_pressureBMP << endl;
|
||||
sensorNode.setProperty("pressure").send(String(value_pressureBMP));
|
||||
d.lastsentvalue=value_pressureBMP;
|
||||
}
|
||||
d.lastsent=millis();
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef SENSOR_HTU21D
|
||||
void loop_HTU21D_temperature()
|
||||
|
@ -1287,10 +1243,7 @@ void loopHandler() {
|
|||
#endif
|
||||
|
||||
#ifdef SENSOR_BMP180
|
||||
if (bmp180init_ok) {
|
||||
loop_BMP180_temperature();
|
||||
loop_BMP180_pressure();
|
||||
}
|
||||
sensor_bmp180.sensorloop();
|
||||
#endif
|
||||
|
||||
#ifdef SENSOR_HTU21D
|
||||
|
|
Loading…
Reference in New Issue