start putting sensors into seperate files

This commit is contained in:
interfisch 2021-10-31 14:07:09 +01:00
parent 1e87d26088
commit 57dce3b696
4 changed files with 192 additions and 97 deletions

View File

@ -81,9 +81,13 @@ monitor_speed = 115200
build_flags =
-D SENSOR_DHT22
-D DHTPIN=D7
-D dataDHT22_temperature_minchange=0.2
-D dataDHT22_humidity_minchange=2.0
-D SENSOR_DHT22_PIN=D7
-D SENSOR_DHT22_temperature_minchange=0.2
-D SENSOR_DHT22_temperature_senddelaymax=300000
-D SENSOR_DHT22_temperature_readdelay=10000
-D SENSOR_DHT22_humidity_minchange=2.0
-D SENSOR_DHT22_humidity_senddelaymax=300000
-D SENSOR_DHT22_humidity_readdelay=10000
-D SENSOR_BMP180
-D dataBMP180_temperature_minchange=0.2

View File

@ -11,24 +11,24 @@
#include "Arduino.h"
#ifndef HOMIE_H
#include <Homie.h>
#define HOMIE_H
#endif
#define FW_NAME "sensoresp" //gets printed on topic/$fw/name
#define FW_VERSION "1.0.0" //gets printed on topic/$fw/version
#define STATUSNODE
struct sensordata
{
unsigned long lastreadtime=0;
unsigned long readdelay=1000*10; //polling delay
float minchange=0; //send new value if difference to last sent value is greater than this
float lastsentvalue=0;
unsigned long lastsent=0;
unsigned long senddelaymax=1000*60*5; //maximum time until current value is send
};
#ifndef SENSORDATA_H
#include "sensordata.h"
#define SENSORDATA_H
#endif
/*
#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
@ -40,6 +40,11 @@ struct sensordata
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);
#endif
#ifdef SENSOR_BMP180
@ -305,14 +310,9 @@ void setup() {
#ifdef SENSOR_DHT22
Serial.println("initializing dht");
dht.begin();
#ifdef dataDHT22_temperature_minchange
dataDHT22_temperature.minchange=dataDHT22_temperature_minchange;
#endif
#ifdef dataDHT22_humidity_minchange
dataDHT22_humidity.minchange=dataDHT22_humidity_minchange;
#endif
sensor_dht22.init();
sensor_dht22.setSettings_Temperature(SENSOR_DHT22_temperature_minchange,SENSOR_DHT22_temperature_senddelaymax,SENSOR_DHT22_temperature_readdelay);
sensor_dht22.setSettings_Humidity(SENSOR_DHT22_humidity_minchange,SENSOR_DHT22_humidity_senddelaymax,SENSOR_DHT22_humidity_readdelay);
#endif
@ -535,17 +535,11 @@ void setup() {
#endif
#ifdef SENSOR_DHT22
#ifndef SENSOR_BMP180
sensorNode.advertise("temperature");
#else
sensorNode.advertise("temperature_dht");
#endif
sensorNode.advertise("humidity");
sensor_dht22.advertise(sensorNode);
#endif
#ifdef SENSOR_HS1101
#if defined(SENSOR_DHT22)
#ifdef SENSOR_DHT22
sensorNode.advertise("humidity_hs1101");
#else
sensorNode.advertise("humidity");
@ -631,71 +625,6 @@ void loop() {
}
#ifdef SENSOR_DHT22
void loop_DHT22_temperature()
{
sensordata &d=dataDHT22_temperature;
bool _changed=false;
if (millis() >= (d.lastreadtime+d.readdelay)) {
value_temperatureDHT = dht.readTemperature();
if (fabs(d.lastsentvalue-value_temperatureDHT)>=d.minchange){
_changed=true;
}
d.lastreadtime=millis();
}
if (_changed || millis() >= (d.lastsent+d.senddelaymax)) {
Serial.print("Sending DHT22_temperature. reason=");
if (_changed) Serial.println("change"); else Serial.println("time");
checkESPStatus();
if (!(isnan(value_temperatureDHT) == 1)){ //success
#ifndef SENSOR_BMP180
sensorNode.setProperty("temperature").send(String(value_temperatureDHT));
Homie.getLogger() << "temperature " << ": " << value_temperatureDHT << endl;
#else
sensorNode.setProperty("temperature_dht").send(String(value_temperatureDHT));
Homie.getLogger() << "temperature_dht " << ": " << value_temperatureDHT << endl;
#endif
d.lastsentvalue=value_temperatureDHT;
}
d.lastsent=millis();
}
}
void loop_DHT22_humidity()
{
sensordata &d=dataDHT22_humidity;
bool _changed=false;
if (millis() >= (d.lastreadtime+d.readdelay)) {
value_humidityDHT = dht.readHumidity();
if (fabs(d.lastsentvalue-value_humidityDHT)>=d.minchange){
_changed=true;
}
d.lastreadtime=millis();
}
if (_changed || millis() >= (d.lastsent+d.senddelaymax)) {
Serial.print("Sending DHT22_humidity. reason=");
if (_changed) Serial.println("change"); else Serial.println("time");
checkESPStatus();
if (!(isnan(value_humidityDHT) == 1)){ //success
Homie.getLogger() << "humidity " << ": " << value_humidityDHT << endl;
sensorNode.setProperty("humidity").send(String(value_humidityDHT));
d.lastsentvalue=value_humidityDHT;
}
d.lastsent=millis();
}
}
#endif
#ifdef SENSOR_BMP180
void loop_BMP180_temperature()
@ -1332,10 +1261,10 @@ void loop_raingauge()
#endif
void loopHandler() {
checkESPStatus();
#ifdef SENSOR_DHT22
loop_DHT22_temperature();
loop_DHT22_humidity();
sensor_dht22.loop();
#endif
#ifdef SENSOR_BMP180

151
src/sensor_dht22.cpp Normal file
View File

@ -0,0 +1,151 @@
// Digital pin connected to the DHT sensor. // dht pins: 1=power, 2=data, 3=NC, 4=GND. 10k from data to power needed
#ifndef Adafruit_Sensor_H
#include <Adafruit_Sensor.h> //required for dht library
#define Adafruit_Sensor_H
#endif
#include <DHT.h>
#ifndef SENSORDATA_H
#include "sensordata.h"
#define SENSORDATA_H
#endif
#ifndef HOMIE_H
#include <Homie.h>
#define HOMIE_H
#endif
class Sensor_DHT22
{
private:
DHT *dht;
HomieNode *sensorNode; //reference to HomieNode
struct sensordata dataDHT22_temperature; //struct values are changed in setup()
struct sensordata dataDHT22_humidity; //struct values are changed in setup()
void loop_DHT22_temperature();
void loop_DHT22_humidity();
public:
Sensor_DHT22(int pin);
void init();
void setSettings_Temperature(float minchange, unsigned long senddelaymax, unsigned long readdelay);
void setSettings_Humidity(float minchange, unsigned long senddelaymax, unsigned long readdelay);
void advertise(HomieNode& p_sensorNode);
void loop();
};
Sensor_DHT22::Sensor_DHT22(int pin)
{
dht = new DHT(pin,DHT22,11); //default:11
}
void Sensor_DHT22::init() //Things to be done during setup()
{
Serial.println("initializing dht");
dht->begin();
}
//Also called during setup()
void Sensor_DHT22::setSettings_Temperature(float minchange, unsigned long senddelaymax, unsigned long readdelay)
{
dataDHT22_temperature.minchange=minchange;
dataDHT22_temperature.senddelaymax=senddelaymax;
dataDHT22_temperature.readdelay=readdelay;
}
//Also called during setup()
void Sensor_DHT22::setSettings_Humidity(float minchange, unsigned long senddelaymax, unsigned long readdelay)
{
dataDHT22_humidity.minchange=minchange;
dataDHT22_humidity.senddelaymax=senddelaymax;
dataDHT22_humidity.readdelay=readdelay;
}
//Called during setup
void Sensor_DHT22::advertise(HomieNode& p_sensorNode)
{
sensorNode = &p_sensorNode;
#ifndef SENSOR_BMP180
sensorNode->advertise("temperature");
#else
sensorNode->advertise("temperature_dht");
#endif
sensorNode->advertise("humidity");
}
void Sensor_DHT22::loop()
{
loop_DHT22_temperature();
loop_DHT22_humidity();
}
void Sensor_DHT22::loop_DHT22_temperature()
{
sensordata &d=dataDHT22_temperature;
bool _changed=false;
if (millis() >= (d.lastreadtime+d.readdelay)) {
d.value = dht->readTemperature();
if (fabs(d.lastsentvalue-d.value)>=d.minchange){
_changed=true;
}
d.lastreadtime=millis();
}
if (_changed || millis() >= (d.lastsent+d.senddelaymax)) {
Serial.print("Sending DHT22_temperature. reason=");
if (_changed) Serial.println("change"); else Serial.println("time");
if (!(isnan(d.value) == 1)){ //success
#ifndef SENSOR_BMP180
sensorNode->setProperty("temperature").send(String(d.value));
Homie.getLogger() << "temperature " << ": " << d.vlaue << endl;
#else
sensorNode->setProperty("temperature_dht").send(String(d.value));
Homie.getLogger() << "temperature_dht " << ": " << d.value << endl;
#endif
d.lastsentvalue=d.value;
}
d.lastsent=millis();
}
}
void Sensor_DHT22::loop_DHT22_humidity()
{
sensordata &d=dataDHT22_humidity;
bool _changed=false;
if (millis() >= (d.lastreadtime+d.readdelay)) {
d.value = dht->readHumidity();
if (fabs(d.lastsentvalue-d.value)>=d.minchange){
_changed=true;
}
d.lastreadtime=millis();
}
if (_changed || millis() >= (d.lastsent+d.senddelaymax)) {
Serial.print("Sending DHT22_humidity. reason=");
if (_changed) Serial.println("change"); else Serial.println("time");
if (!(isnan(d.value) == 1)){ //success
Homie.getLogger() << "humidity " << ": " << d.value << endl;
sensorNode->setProperty("humidity").send(String(d.value));
d.lastsentvalue=d.value;
}
d.lastsent=millis();
}
}

11
src/sensordata.h Normal file
View File

@ -0,0 +1,11 @@
struct sensordata
{
unsigned long lastreadtime=0;
unsigned long readdelay=1000*10; //polling delay
float minchange=0; //send new value if difference to last sent value is greater than this
float lastsentvalue=0;
float value=0;
unsigned long lastsent=0;
unsigned long senddelaymax=1000*60*5; //maximum time until current value is send
};