sensoresp/include/sensor_bmp180.cpp

106 lines
2.7 KiB
C++
Raw Normal View History

2021-11-02 19:13:38 +00:00
//Connect SCL to D1, SDA to D2, GND and 3v3
#include "sensor_bmp180.h"
Sensor_BMP180::Sensor_BMP180()
{
bmp180 = new Adafruit_BMP085();
2021-11-02 19:13:38 +00:00
}
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();
}
}