add struct for timing and value comparisons
This commit is contained in:
parent
12f65702d0
commit
7c4f6bef9b
136
src/main.cpp
136
src/main.cpp
|
@ -2,7 +2,7 @@
|
|||
|
||||
// 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)
|
||||
#define SENSOR_PIR
|
||||
|
@ -21,27 +21,25 @@
|
|||
#define FW_NAME "sensoresp" //gets printed on topic/$fw/name
|
||||
#define FW_VERSION "1.0.0" //gets printed on topic/$fw/version
|
||||
|
||||
/*
|
||||
|
||||
struct sensordata
|
||||
{
|
||||
unsigned long lastDHT22time=0;
|
||||
unsigned long readdelay_DHT22=1000*30; //polling delay
|
||||
float minchange_DHT22=0.2;//send new value if difference to last sent value is greater than this
|
||||
unsigned long lastsend_DHT22=0;
|
||||
unsigned long senddelaymax_DHT22=1000*60*10; //maximum time until current value is send
|
||||
}
|
||||
*/
|
||||
unsigned long lastreadtime=0;
|
||||
unsigned long readdelay=1000*60; //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*10; //maximum time until current value is send
|
||||
};
|
||||
|
||||
|
||||
#ifdef SENSOR_DHT22
|
||||
#include <Adafruit_Sensor.h> //required for dht library
|
||||
#include <DHT.h>
|
||||
DHT dht(DHTPIN,DHT22,11); //default:11
|
||||
unsigned long lastDHT22time=0;
|
||||
unsigned long readdelay_DHT22=1000*30; //polling delay
|
||||
float minchange_DHT22=0.2;//send new value if difference to last sent value is greater than this
|
||||
unsigned long lastsend_DHT22=0;
|
||||
unsigned long senddelaymax_DHT22=1000*60*10; //maximum time until current value is send
|
||||
|
||||
struct sensordata dataDHT22_temperature; //struct values are changed in setup()
|
||||
struct sensordata dataDHT22_humidity; //struct values are changed in setup()
|
||||
|
||||
#endif
|
||||
|
||||
|
@ -49,19 +47,25 @@ struct sensordata
|
|||
#include <Wire.h>
|
||||
#include <BH1750.h>
|
||||
BH1750 lightMeter(0x23);
|
||||
sensordata dataBH1750;
|
||||
/*
|
||||
unsigned long lastBH1750time=0;
|
||||
unsigned long readdelay_BH1750=2000; //polling delay
|
||||
unsigned long lastsend_BH1750=0;
|
||||
unsigned long senddelaymax_BH1750=1000*60*10; //maximum time until current value is send
|
||||
*/
|
||||
#endif
|
||||
|
||||
|
||||
#ifdef SENSOR_PIR
|
||||
sensordata dataPIR;
|
||||
/*
|
||||
unsigned long lastPIRtime=0;
|
||||
unsigned long readdelay_PIR=100; //polling delay
|
||||
unsigned long lastsend_PIR=0;
|
||||
unsigned long senddelaymax_PIR=1000*60*10; //maximum time until current value is send
|
||||
bool motion=false;
|
||||
*/
|
||||
bool lastsentvalue_PIR=false;
|
||||
#endif
|
||||
|
||||
|
||||
|
@ -92,9 +96,13 @@ void setup() {
|
|||
Serial.println("Booting");
|
||||
|
||||
|
||||
|
||||
|
||||
#ifdef SENSOR_DHT22
|
||||
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
|
||||
|
||||
#ifdef SENSOR_BH1750
|
||||
|
@ -105,6 +113,7 @@ void setup() {
|
|||
} else {
|
||||
Serial.println(F("Error initialising BH1750"));
|
||||
}
|
||||
dataBH1750.minchange=1.0;
|
||||
#endif
|
||||
|
||||
|
||||
|
@ -130,7 +139,7 @@ void setup() {
|
|||
|
||||
|
||||
#ifdef SENSOR_PIR
|
||||
sensorNode.advertise("motion");
|
||||
sensorNode.advertise("lastsentvalue_PIR");
|
||||
#endif
|
||||
|
||||
|
||||
|
@ -146,26 +155,54 @@ void loop() {
|
|||
#ifdef SENSOR_DHT22
|
||||
void loop_DHT22()
|
||||
{
|
||||
if (millis() >= (lastDHT22time+readdelay_DHT22))
|
||||
{
|
||||
Serial.println("Sending");
|
||||
sensordata d=dataDHT22_temperature;
|
||||
bool _changed=false;
|
||||
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();
|
||||
|
||||
|
||||
float temperatureDHT = dht.readTemperature();
|
||||
if (!(isnan(temperatureDHT) == 1)){ //success
|
||||
Homie.getLogger() << "temperature " << ": " << temperatureDHT << endl;
|
||||
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
|
||||
Homie.getLogger() << "humidity " << ": " << humidityDHT << endl;
|
||||
sensorNode.setProperty("humidity").send(String(humidityDHT));
|
||||
d.lastsentvalue=humidityDHT;
|
||||
}
|
||||
|
||||
|
||||
lastDHT22time=millis();
|
||||
d.lastsent=millis();
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
@ -173,7 +210,8 @@ void loop_DHT22()
|
|||
#ifdef SENSOR_BH1750
|
||||
void loop_BH1750()
|
||||
{
|
||||
if (millis() >= (lastBH1750time+readdelay_BH1750))
|
||||
sensordata d=dataBH1750;
|
||||
if (millis() >= (d.lastreadtime+d.readdelay))
|
||||
{
|
||||
Serial.println("Sending");
|
||||
checkESPStatus();
|
||||
|
@ -182,7 +220,7 @@ void loop_BH1750()
|
|||
Homie.getLogger() << "light " << ": " << light << endl;
|
||||
sensorNode.setProperty("light").send(String(light));
|
||||
|
||||
lastBH1750time=millis();
|
||||
d.lastreadtime=millis();
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
@ -190,35 +228,25 @@ void loop_BH1750()
|
|||
#ifdef SENSOR_PIR
|
||||
void loop_PIR()
|
||||
{
|
||||
if (millis() >= (lastPIRtime+readdelay_PIR)){
|
||||
if (digitalRead(PIRPIN)){
|
||||
if (!motion) { //changed?
|
||||
Homie.getLogger() << "motion " << ": " << "true" << endl;
|
||||
sensorNode.setProperty("motion").send(String("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();
|
||||
sensordata d=dataPIR;
|
||||
bool _changed=false;
|
||||
if (millis() >= (d.lastreadtime+d.readdelay)) {
|
||||
if (digitalRead(PIRPIN) != lastsentvalue_PIR){
|
||||
_changed=true;
|
||||
}
|
||||
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)){
|
||||
Homie.getLogger() << "motion resend " << ": " << "true" << endl;
|
||||
sensorNode.setProperty("motion").send(String("true"));
|
||||
motion=true;
|
||||
Homie.getLogger() << "lastsentvalue_PIR resend " << ": " << "true" << endl;
|
||||
sensorNode.setProperty("lastsentvalue_PIR").send(String("true"));
|
||||
lastsentvalue_PIR=true;
|
||||
}else{
|
||||
Homie.getLogger() << "motion resend " << ": " << "false" << endl;
|
||||
sensorNode.setProperty("motion").send(String("false"));
|
||||
motion=false;
|
||||
Homie.getLogger() << "lastsentvalue_PIR resend " << ": " << "false" << endl;
|
||||
sensorNode.setProperty("lastsentvalue_PIR").send(String("false"));
|
||||
lastsentvalue_PIR=false;
|
||||
}
|
||||
lastsend_PIR=millis();
|
||||
d.lastsent=millis();
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
@ -228,11 +256,11 @@ void loop_PIR()
|
|||
void loopHandler() {
|
||||
|
||||
#ifdef SENSOR_DHT22
|
||||
|
||||
loop_DHT22();
|
||||
#endif
|
||||
|
||||
#ifdef SENSOR_BH1750
|
||||
|
||||
loop_BH1750();
|
||||
#endif
|
||||
|
||||
|
||||
|
|
Loading…
Reference in New Issue