fix struct
This commit is contained in:
parent
7c4f6bef9b
commit
0edc6dd475
125
src/main.cpp
125
src/main.cpp
|
@ -25,11 +25,11 @@
|
|||
struct sensordata
|
||||
{
|
||||
unsigned long lastreadtime=0;
|
||||
unsigned long readdelay=1000*60; //polling delay
|
||||
unsigned long readdelay=1000*30; //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
|
||||
unsigned long senddelaymax=1000*60*5; //maximum time until current value is send
|
||||
};
|
||||
|
||||
|
||||
|
@ -39,7 +39,9 @@ struct sensordata
|
|||
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
|
||||
|
||||
|
@ -48,24 +50,13 @@ struct sensordata
|
|||
#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
|
||||
*/
|
||||
float value_lightBH1750=0;
|
||||
#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 lastsentvalue_PIR=false;
|
||||
bool value_PIR=false;
|
||||
#endif
|
||||
|
||||
|
||||
|
@ -113,12 +104,16 @@ void setup() {
|
|||
} else {
|
||||
Serial.println(F("Error initialising BH1750"));
|
||||
}
|
||||
dataBH1750.minchange=1.0;
|
||||
dataBH1750.minchange=10.0;
|
||||
dataBH1750.readdelay=1000*10;
|
||||
dataBH1750.senddelaymax=1000*60*2;
|
||||
#endif
|
||||
|
||||
|
||||
#ifdef SENSOR_PIR
|
||||
pinMode(PIRPIN, INPUT_PULLUP);
|
||||
dataPIR.readdelay=100;
|
||||
dataPIR.senddelaymax=1000*60*10;
|
||||
#endif
|
||||
|
||||
Serial.println("connecting..");
|
||||
|
@ -135,11 +130,12 @@ void setup() {
|
|||
|
||||
#ifdef SENSOR_BH1750
|
||||
sensorNode.advertise("light");
|
||||
lightMeter.readLightLevel(); //make first reading, could be 0
|
||||
#endif
|
||||
|
||||
|
||||
#ifdef SENSOR_PIR
|
||||
sensorNode.advertise("lastsentvalue_PIR");
|
||||
sensorNode.advertise("motion");
|
||||
#endif
|
||||
|
||||
|
||||
|
@ -153,54 +149,57 @@ void loop() {
|
|||
|
||||
|
||||
#ifdef SENSOR_DHT22
|
||||
void loop_DHT22()
|
||||
void loop_DHT22_temperature()
|
||||
{
|
||||
sensordata d=dataDHT22_temperature;
|
||||
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){
|
||||
value_temperatureDHT = dht.readTemperature();
|
||||
if (abs(d.lastsentvalue-value_temperatureDHT)>=d.minchange){
|
||||
_changed=true;
|
||||
}
|
||||
d.lastreadtime=millis();
|
||||
}
|
||||
|
||||
if (_changed || millis() >= (d.lastsent+d.senddelaymax)) {
|
||||
Serial.println("Sending DHT22_temperature");
|
||||
Serial.print("Sending DHT22_temperature. reason=");
|
||||
if (_changed) Serial.println("change"); else Serial.println("time");
|
||||
checkESPStatus();
|
||||
|
||||
if (!(isnan(temperatureDHT) == 1)){ //success
|
||||
Homie.getLogger() << "temperature " << ": " << temperatureDHT << endl;
|
||||
sensorNode.setProperty("temperature").send(String(temperatureDHT));
|
||||
d.lastsentvalue=temperatureDHT;
|
||||
if (!(isnan(value_temperatureDHT) == 1)){ //success
|
||||
Homie.getLogger() << "temperature " << ": " << value_temperatureDHT << endl;
|
||||
sensorNode.setProperty("temperature").send(String(value_temperatureDHT));
|
||||
d.lastsentvalue=value_temperatureDHT;
|
||||
}
|
||||
|
||||
d.lastsent=millis();
|
||||
}
|
||||
}
|
||||
|
||||
void loop_DHT22_humidity()
|
||||
{
|
||||
sensordata &d=dataDHT22_humidity;
|
||||
bool _changed=false;
|
||||
|
||||
//exchange variables
|
||||
d=dataDHT22_humidity;
|
||||
_changed=false;
|
||||
|
||||
if (millis() >= (d.lastreadtime+d.readdelay)) {
|
||||
humidityDHT = dht.readHumidity();
|
||||
if (abs(d.lastsentvalue-humidityDHT)>=d.minchange){
|
||||
value_humidityDHT = dht.readHumidity();
|
||||
if (abs(d.lastsentvalue-value_humidityDHT)>=d.minchange){
|
||||
_changed=true;
|
||||
}
|
||||
dataPIR.lastreadtime=millis();
|
||||
d.lastreadtime=millis();
|
||||
}
|
||||
|
||||
if (_changed || millis() >= (d.lastsent+d.senddelaymax)) {
|
||||
Serial.println("Sending DHT22_temperature");
|
||||
Serial.print("Sending DHT22_humidity. reason=");
|
||||
if (_changed) Serial.println("change"); else Serial.println("time");
|
||||
checkESPStatus();
|
||||
|
||||
if (!(isnan(humidityDHT) == 1)){ //success
|
||||
Homie.getLogger() << "humidity " << ": " << humidityDHT << endl;
|
||||
sensorNode.setProperty("humidity").send(String(humidityDHT));
|
||||
d.lastsentvalue=humidityDHT;
|
||||
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();
|
||||
}
|
||||
|
@ -210,17 +209,27 @@ void loop_DHT22()
|
|||
#ifdef SENSOR_BH1750
|
||||
void loop_BH1750()
|
||||
{
|
||||
sensordata d=dataBH1750;
|
||||
if (millis() >= (d.lastreadtime+d.readdelay))
|
||||
{
|
||||
Serial.println("Sending");
|
||||
sensordata &d=dataBH1750;
|
||||
|
||||
bool _changed=false;
|
||||
if (millis() >= (d.lastreadtime+d.readdelay)) {
|
||||
value_lightBH1750 = lightMeter.readLightLevel(); // [lux]
|
||||
if (abs(d.lastsentvalue-value_lightBH1750)>=d.minchange){
|
||||
_changed=true;
|
||||
}
|
||||
d.lastreadtime=millis();
|
||||
}
|
||||
|
||||
if (_changed || millis() >= (d.lastsent+d.senddelaymax)) {
|
||||
Serial.print("Sending BH1750. reason=");
|
||||
if (_changed) Serial.println("change"); else Serial.println("time");
|
||||
checkESPStatus();
|
||||
|
||||
float light = lightMeter.readLightLevel(); // [lux]
|
||||
Homie.getLogger() << "light " << ": " << light << endl;
|
||||
sensorNode.setProperty("light").send(String(light));
|
||||
Homie.getLogger() << "light " << ": " << value_lightBH1750 << endl;
|
||||
sensorNode.setProperty("light").send(String(value_lightBH1750));
|
||||
d.lastsentvalue=value_lightBH1750;
|
||||
|
||||
d.lastreadtime=millis();
|
||||
d.lastsent=millis();
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
@ -228,23 +237,26 @@ void loop_BH1750()
|
|||
#ifdef SENSOR_PIR
|
||||
void loop_PIR()
|
||||
{
|
||||
sensordata d=dataPIR;
|
||||
sensordata &d=dataPIR;
|
||||
bool _changed=false;
|
||||
if (millis() >= (d.lastreadtime+d.readdelay)) {
|
||||
if (digitalRead(PIRPIN) != lastsentvalue_PIR){
|
||||
if (digitalRead(PIRPIN) != value_PIR){
|
||||
_changed=true;
|
||||
}
|
||||
d.lastreadtime=millis();
|
||||
}
|
||||
if (_changed || millis() >= (d.lastsent+d.senddelaymax)) { //send current value after some long time
|
||||
Serial.print("Sending motion. reason=");
|
||||
if (_changed) Serial.println("change"); else Serial.println("time");
|
||||
|
||||
if (digitalRead(PIRPIN)){
|
||||
Homie.getLogger() << "lastsentvalue_PIR resend " << ": " << "true" << endl;
|
||||
sensorNode.setProperty("lastsentvalue_PIR").send(String("true"));
|
||||
lastsentvalue_PIR=true;
|
||||
Homie.getLogger() << "motion " << ": " << "true" << endl;
|
||||
sensorNode.setProperty("motion").send(String("true"));
|
||||
value_PIR=true;
|
||||
}else{
|
||||
Homie.getLogger() << "lastsentvalue_PIR resend " << ": " << "false" << endl;
|
||||
sensorNode.setProperty("lastsentvalue_PIR").send(String("false"));
|
||||
lastsentvalue_PIR=false;
|
||||
Homie.getLogger() << "motion " << ": " << "false" << endl;
|
||||
sensorNode.setProperty("motion").send(String("false"));
|
||||
value_PIR=false;
|
||||
}
|
||||
d.lastsent=millis();
|
||||
}
|
||||
|
@ -256,7 +268,8 @@ void loop_PIR()
|
|||
void loopHandler() {
|
||||
|
||||
#ifdef SENSOR_DHT22
|
||||
loop_DHT22();
|
||||
loop_DHT22_temperature();
|
||||
loop_DHT22_humidity();
|
||||
#endif
|
||||
|
||||
#ifdef SENSOR_BH1750
|
||||
|
|
Loading…
Reference in New Issue