add struct for timing and value comparisons

This commit is contained in:
interfisch 2020-10-02 20:28:14 +02:00
parent 12f65702d0
commit 7c4f6bef9b
1 changed files with 82 additions and 54 deletions

View File

@ -2,7 +2,7 @@
// DHT22 // DHT22
#define SENSOR_DHT22 #define SENSOR_DHT22
#define DHTPIN D7 // Digital pin connected to the DHT sensor #define DHTPIN D7 // Digital pin connected to the DHT sensor. // dht pins: 1=power, 2=data, 3=NC, 4=GND. 10k from data to power needed
// PIR Sensors HC-SR501 (modified to put out shortest pulse time) // PIR Sensors HC-SR501 (modified to put out shortest pulse time)
#define SENSOR_PIR #define SENSOR_PIR
@ -21,27 +21,25 @@
#define FW_NAME "sensoresp" //gets printed on topic/$fw/name #define FW_NAME "sensoresp" //gets printed on topic/$fw/name
#define FW_VERSION "1.0.0" //gets printed on topic/$fw/version #define FW_VERSION "1.0.0" //gets printed on topic/$fw/version
/*
struct sensordata struct sensordata
{ {
unsigned long lastDHT22time=0; unsigned long lastreadtime=0;
unsigned long readdelay_DHT22=1000*30; //polling delay unsigned long readdelay=1000*60; //polling delay
float minchange_DHT22=0.2;//send new value if difference to last sent value is greater than this float minchange=0; //send new value if difference to last sent value is greater than this
unsigned long lastsend_DHT22=0; float lastsentvalue=0;
unsigned long senddelaymax_DHT22=1000*60*10; //maximum time until current value is send unsigned long lastsent=0;
} unsigned long senddelaymax=1000*60*10; //maximum time until current value is send
*/ };
#ifdef SENSOR_DHT22 #ifdef SENSOR_DHT22
#include <Adafruit_Sensor.h> //required for dht library #include <Adafruit_Sensor.h> //required for dht library
#include <DHT.h> #include <DHT.h>
DHT dht(DHTPIN,DHT22,11); //default:11 DHT dht(DHTPIN,DHT22,11); //default:11
unsigned long lastDHT22time=0;
unsigned long readdelay_DHT22=1000*30; //polling delay struct sensordata dataDHT22_temperature; //struct values are changed in setup()
float minchange_DHT22=0.2;//send new value if difference to last sent value is greater than this struct sensordata dataDHT22_humidity; //struct values are changed in setup()
unsigned long lastsend_DHT22=0;
unsigned long senddelaymax_DHT22=1000*60*10; //maximum time until current value is send
#endif #endif
@ -49,19 +47,25 @@ struct sensordata
#include <Wire.h> #include <Wire.h>
#include <BH1750.h> #include <BH1750.h>
BH1750 lightMeter(0x23); BH1750 lightMeter(0x23);
sensordata dataBH1750;
/*
unsigned long lastBH1750time=0; unsigned long lastBH1750time=0;
unsigned long readdelay_BH1750=2000; //polling delay unsigned long readdelay_BH1750=2000; //polling delay
unsigned long lastsend_BH1750=0; unsigned long lastsend_BH1750=0;
unsigned long senddelaymax_BH1750=1000*60*10; //maximum time until current value is send unsigned long senddelaymax_BH1750=1000*60*10; //maximum time until current value is send
*/
#endif #endif
#ifdef SENSOR_PIR #ifdef SENSOR_PIR
sensordata dataPIR;
/*
unsigned long lastPIRtime=0; unsigned long lastPIRtime=0;
unsigned long readdelay_PIR=100; //polling delay unsigned long readdelay_PIR=100; //polling delay
unsigned long lastsend_PIR=0; unsigned long lastsend_PIR=0;
unsigned long senddelaymax_PIR=1000*60*10; //maximum time until current value is send unsigned long senddelaymax_PIR=1000*60*10; //maximum time until current value is send
bool motion=false; */
bool lastsentvalue_PIR=false;
#endif #endif
@ -92,9 +96,13 @@ void setup() {
Serial.println("Booting"); Serial.println("Booting");
#ifdef SENSOR_DHT22 #ifdef SENSOR_DHT22
Serial.println("initializing dht"); Serial.println("initializing dht");
dht.begin(); // dht pins: 1=power, 2=data, 3=NC, 4=GND. 10k from data to power needed dht.begin();
dataDHT22_temperature.minchange=0.1;
dataDHT22_humidity.minchange=1.0;
#endif #endif
#ifdef SENSOR_BH1750 #ifdef SENSOR_BH1750
@ -105,6 +113,7 @@ void setup() {
} else { } else {
Serial.println(F("Error initialising BH1750")); Serial.println(F("Error initialising BH1750"));
} }
dataBH1750.minchange=1.0;
#endif #endif
@ -130,7 +139,7 @@ void setup() {
#ifdef SENSOR_PIR #ifdef SENSOR_PIR
sensorNode.advertise("motion"); sensorNode.advertise("lastsentvalue_PIR");
#endif #endif
@ -146,26 +155,54 @@ void loop() {
#ifdef SENSOR_DHT22 #ifdef SENSOR_DHT22
void loop_DHT22() void loop_DHT22()
{ {
if (millis() >= (lastDHT22time+readdelay_DHT22)) sensordata d=dataDHT22_temperature;
{ bool _changed=false;
Serial.println("Sending"); float temperatureDHT = dht.readTemperature();
float humidityDHT = dht.readHumidity();
if (millis() >= (d.lastreadtime+d.readdelay)) {
temperatureDHT = dht.readTemperature();
if (abs(d.lastsentvalue-temperatureDHT)>=d.minchange){
_changed=true;
}
d.lastreadtime=millis();
}
if (_changed || millis() >= (d.lastsent+d.senddelaymax)) {
Serial.println("Sending DHT22_temperature");
checkESPStatus(); checkESPStatus();
float temperatureDHT = dht.readTemperature();
if (!(isnan(temperatureDHT) == 1)){ //success if (!(isnan(temperatureDHT) == 1)){ //success
Homie.getLogger() << "temperature " << ": " << temperatureDHT << endl; Homie.getLogger() << "temperature " << ": " << temperatureDHT << endl;
sensorNode.setProperty("temperature").send(String(temperatureDHT)); sensorNode.setProperty("temperature").send(String(temperatureDHT));
d.lastsentvalue=temperatureDHT;
} }
float humidityDHT = dht.readHumidity(); d.lastsent=millis();
}
//exchange variables
d=dataDHT22_humidity;
_changed=false;
if (millis() >= (d.lastreadtime+d.readdelay)) {
humidityDHT = dht.readHumidity();
if (abs(d.lastsentvalue-humidityDHT)>=d.minchange){
_changed=true;
}
dataPIR.lastreadtime=millis();
}
if (_changed || millis() >= (d.lastsent+d.senddelaymax)) {
Serial.println("Sending DHT22_temperature");
checkESPStatus();
if (!(isnan(humidityDHT) == 1)){ //success if (!(isnan(humidityDHT) == 1)){ //success
Homie.getLogger() << "humidity " << ": " << humidityDHT << endl; Homie.getLogger() << "humidity " << ": " << humidityDHT << endl;
sensorNode.setProperty("humidity").send(String(humidityDHT)); sensorNode.setProperty("humidity").send(String(humidityDHT));
d.lastsentvalue=humidityDHT;
} }
d.lastsent=millis();
lastDHT22time=millis();
} }
} }
#endif #endif
@ -173,7 +210,8 @@ void loop_DHT22()
#ifdef SENSOR_BH1750 #ifdef SENSOR_BH1750
void loop_BH1750() void loop_BH1750()
{ {
if (millis() >= (lastBH1750time+readdelay_BH1750)) sensordata d=dataBH1750;
if (millis() >= (d.lastreadtime+d.readdelay))
{ {
Serial.println("Sending"); Serial.println("Sending");
checkESPStatus(); checkESPStatus();
@ -182,7 +220,7 @@ void loop_BH1750()
Homie.getLogger() << "light " << ": " << light << endl; Homie.getLogger() << "light " << ": " << light << endl;
sensorNode.setProperty("light").send(String(light)); sensorNode.setProperty("light").send(String(light));
lastBH1750time=millis(); d.lastreadtime=millis();
} }
} }
#endif #endif
@ -190,35 +228,25 @@ void loop_BH1750()
#ifdef SENSOR_PIR #ifdef SENSOR_PIR
void loop_PIR() void loop_PIR()
{ {
if (millis() >= (lastPIRtime+readdelay_PIR)){ sensordata d=dataPIR;
if (digitalRead(PIRPIN)){ bool _changed=false;
if (!motion) { //changed? if (millis() >= (d.lastreadtime+d.readdelay)) {
Homie.getLogger() << "motion " << ": " << "true" << endl; if (digitalRead(PIRPIN) != lastsentvalue_PIR){
sensorNode.setProperty("motion").send(String("true")); _changed=true;
}
motion=true;
lastsend_PIR=millis();
}else{
if (motion) { //changed?
Homie.getLogger() << "motion " << ": " << "false" << endl;
sensorNode.setProperty("motion").send(String("false"));
}
motion=false;
lastsend_PIR=millis();
} }
lastPIRtime=millis(); d.lastreadtime=millis();
} }
if (millis() >= (lastsend_PIR+senddelaymax_PIR)) { //send current value after some long time if (_changed || millis() >= (d.lastsent+d.senddelaymax)) { //send current value after some long time
if (digitalRead(PIRPIN)){ if (digitalRead(PIRPIN)){
Homie.getLogger() << "motion resend " << ": " << "true" << endl; Homie.getLogger() << "lastsentvalue_PIR resend " << ": " << "true" << endl;
sensorNode.setProperty("motion").send(String("true")); sensorNode.setProperty("lastsentvalue_PIR").send(String("true"));
motion=true; lastsentvalue_PIR=true;
}else{ }else{
Homie.getLogger() << "motion resend " << ": " << "false" << endl; Homie.getLogger() << "lastsentvalue_PIR resend " << ": " << "false" << endl;
sensorNode.setProperty("motion").send(String("false")); sensorNode.setProperty("lastsentvalue_PIR").send(String("false"));
motion=false; lastsentvalue_PIR=false;
} }
lastsend_PIR=millis(); d.lastsent=millis();
} }
} }
#endif #endif
@ -228,11 +256,11 @@ void loop_PIR()
void loopHandler() { void loopHandler() {
#ifdef SENSOR_DHT22 #ifdef SENSOR_DHT22
loop_DHT22();
#endif #endif
#ifdef SENSOR_BH1750 #ifdef SENSOR_BH1750
loop_BH1750();
#endif #endif