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
|
struct sensordata
|
||||||
{
|
{
|
||||||
unsigned long lastreadtime=0;
|
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 minchange=0; //send new value if difference to last sent value is greater than this
|
||||||
float lastsentvalue=0;
|
float lastsentvalue=0;
|
||||||
unsigned long lastsent=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
|
DHT dht(DHTPIN,DHT22,11); //default:11
|
||||||
|
|
||||||
struct sensordata dataDHT22_temperature; //struct values are changed in setup()
|
struct sensordata dataDHT22_temperature; //struct values are changed in setup()
|
||||||
|
float value_temperatureDHT=0;
|
||||||
struct sensordata dataDHT22_humidity; //struct values are changed in setup()
|
struct sensordata dataDHT22_humidity; //struct values are changed in setup()
|
||||||
|
float value_humidityDHT=0;
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
@ -48,24 +50,13 @@ struct sensordata
|
||||||
#include <BH1750.h>
|
#include <BH1750.h>
|
||||||
BH1750 lightMeter(0x23);
|
BH1750 lightMeter(0x23);
|
||||||
sensordata dataBH1750;
|
sensordata dataBH1750;
|
||||||
/*
|
float value_lightBH1750=0;
|
||||||
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
|
#endif
|
||||||
|
|
||||||
|
|
||||||
#ifdef SENSOR_PIR
|
#ifdef SENSOR_PIR
|
||||||
sensordata dataPIR;
|
sensordata dataPIR;
|
||||||
/*
|
bool value_PIR=false;
|
||||||
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;
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
@ -113,12 +104,16 @@ void setup() {
|
||||||
} else {
|
} else {
|
||||||
Serial.println(F("Error initialising BH1750"));
|
Serial.println(F("Error initialising BH1750"));
|
||||||
}
|
}
|
||||||
dataBH1750.minchange=1.0;
|
dataBH1750.minchange=10.0;
|
||||||
|
dataBH1750.readdelay=1000*10;
|
||||||
|
dataBH1750.senddelaymax=1000*60*2;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
#ifdef SENSOR_PIR
|
#ifdef SENSOR_PIR
|
||||||
pinMode(PIRPIN, INPUT_PULLUP);
|
pinMode(PIRPIN, INPUT_PULLUP);
|
||||||
|
dataPIR.readdelay=100;
|
||||||
|
dataPIR.senddelaymax=1000*60*10;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
Serial.println("connecting..");
|
Serial.println("connecting..");
|
||||||
|
@ -135,11 +130,12 @@ void setup() {
|
||||||
|
|
||||||
#ifdef SENSOR_BH1750
|
#ifdef SENSOR_BH1750
|
||||||
sensorNode.advertise("light");
|
sensorNode.advertise("light");
|
||||||
|
lightMeter.readLightLevel(); //make first reading, could be 0
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
#ifdef SENSOR_PIR
|
#ifdef SENSOR_PIR
|
||||||
sensorNode.advertise("lastsentvalue_PIR");
|
sensorNode.advertise("motion");
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
@ -153,54 +149,57 @@ void loop() {
|
||||||
|
|
||||||
|
|
||||||
#ifdef SENSOR_DHT22
|
#ifdef SENSOR_DHT22
|
||||||
void loop_DHT22()
|
void loop_DHT22_temperature()
|
||||||
{
|
{
|
||||||
sensordata d=dataDHT22_temperature;
|
sensordata &d=dataDHT22_temperature;
|
||||||
bool _changed=false;
|
bool _changed=false;
|
||||||
float temperatureDHT = dht.readTemperature();
|
|
||||||
float humidityDHT = dht.readHumidity();
|
|
||||||
|
|
||||||
if (millis() >= (d.lastreadtime+d.readdelay)) {
|
if (millis() >= (d.lastreadtime+d.readdelay)) {
|
||||||
temperatureDHT = dht.readTemperature();
|
value_temperatureDHT = dht.readTemperature();
|
||||||
if (abs(d.lastsentvalue-temperatureDHT)>=d.minchange){
|
if (abs(d.lastsentvalue-value_temperatureDHT)>=d.minchange){
|
||||||
_changed=true;
|
_changed=true;
|
||||||
}
|
}
|
||||||
d.lastreadtime=millis();
|
d.lastreadtime=millis();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (_changed || millis() >= (d.lastsent+d.senddelaymax)) {
|
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();
|
checkESPStatus();
|
||||||
|
|
||||||
if (!(isnan(temperatureDHT) == 1)){ //success
|
if (!(isnan(value_temperatureDHT) == 1)){ //success
|
||||||
Homie.getLogger() << "temperature " << ": " << temperatureDHT << endl;
|
Homie.getLogger() << "temperature " << ": " << value_temperatureDHT << endl;
|
||||||
sensorNode.setProperty("temperature").send(String(temperatureDHT));
|
sensorNode.setProperty("temperature").send(String(value_temperatureDHT));
|
||||||
d.lastsentvalue=temperatureDHT;
|
d.lastsentvalue=value_temperatureDHT;
|
||||||
}
|
}
|
||||||
|
|
||||||
d.lastsent=millis();
|
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)) {
|
if (millis() >= (d.lastreadtime+d.readdelay)) {
|
||||||
humidityDHT = dht.readHumidity();
|
value_humidityDHT = dht.readHumidity();
|
||||||
if (abs(d.lastsentvalue-humidityDHT)>=d.minchange){
|
if (abs(d.lastsentvalue-value_humidityDHT)>=d.minchange){
|
||||||
_changed=true;
|
_changed=true;
|
||||||
}
|
}
|
||||||
dataPIR.lastreadtime=millis();
|
d.lastreadtime=millis();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (_changed || millis() >= (d.lastsent+d.senddelaymax)) {
|
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();
|
checkESPStatus();
|
||||||
|
|
||||||
if (!(isnan(humidityDHT) == 1)){ //success
|
if (!(isnan(value_humidityDHT) == 1)){ //success
|
||||||
Homie.getLogger() << "humidity " << ": " << humidityDHT << endl;
|
Homie.getLogger() << "humidity " << ": " << value_humidityDHT << endl;
|
||||||
sensorNode.setProperty("humidity").send(String(humidityDHT));
|
sensorNode.setProperty("humidity").send(String(value_humidityDHT));
|
||||||
d.lastsentvalue=humidityDHT;
|
d.lastsentvalue=value_humidityDHT;
|
||||||
}
|
}
|
||||||
d.lastsent=millis();
|
d.lastsent=millis();
|
||||||
}
|
}
|
||||||
|
@ -210,17 +209,27 @@ void loop_DHT22()
|
||||||
#ifdef SENSOR_BH1750
|
#ifdef SENSOR_BH1750
|
||||||
void loop_BH1750()
|
void loop_BH1750()
|
||||||
{
|
{
|
||||||
sensordata d=dataBH1750;
|
sensordata &d=dataBH1750;
|
||||||
if (millis() >= (d.lastreadtime+d.readdelay))
|
|
||||||
{
|
bool _changed=false;
|
||||||
Serial.println("Sending");
|
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();
|
checkESPStatus();
|
||||||
|
|
||||||
float light = lightMeter.readLightLevel(); // [lux]
|
Homie.getLogger() << "light " << ": " << value_lightBH1750 << endl;
|
||||||
Homie.getLogger() << "light " << ": " << light << endl;
|
sensorNode.setProperty("light").send(String(value_lightBH1750));
|
||||||
sensorNode.setProperty("light").send(String(light));
|
d.lastsentvalue=value_lightBH1750;
|
||||||
|
|
||||||
d.lastreadtime=millis();
|
d.lastsent=millis();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
@ -228,23 +237,26 @@ void loop_BH1750()
|
||||||
#ifdef SENSOR_PIR
|
#ifdef SENSOR_PIR
|
||||||
void loop_PIR()
|
void loop_PIR()
|
||||||
{
|
{
|
||||||
sensordata d=dataPIR;
|
sensordata &d=dataPIR;
|
||||||
bool _changed=false;
|
bool _changed=false;
|
||||||
if (millis() >= (d.lastreadtime+d.readdelay)) {
|
if (millis() >= (d.lastreadtime+d.readdelay)) {
|
||||||
if (digitalRead(PIRPIN) != lastsentvalue_PIR){
|
if (digitalRead(PIRPIN) != value_PIR){
|
||||||
_changed=true;
|
_changed=true;
|
||||||
}
|
}
|
||||||
d.lastreadtime=millis();
|
d.lastreadtime=millis();
|
||||||
}
|
}
|
||||||
if (_changed || millis() >= (d.lastsent+d.senddelaymax)) { //send current value after some long time
|
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)){
|
if (digitalRead(PIRPIN)){
|
||||||
Homie.getLogger() << "lastsentvalue_PIR resend " << ": " << "true" << endl;
|
Homie.getLogger() << "motion " << ": " << "true" << endl;
|
||||||
sensorNode.setProperty("lastsentvalue_PIR").send(String("true"));
|
sensorNode.setProperty("motion").send(String("true"));
|
||||||
lastsentvalue_PIR=true;
|
value_PIR=true;
|
||||||
}else{
|
}else{
|
||||||
Homie.getLogger() << "lastsentvalue_PIR resend " << ": " << "false" << endl;
|
Homie.getLogger() << "motion " << ": " << "false" << endl;
|
||||||
sensorNode.setProperty("lastsentvalue_PIR").send(String("false"));
|
sensorNode.setProperty("motion").send(String("false"));
|
||||||
lastsentvalue_PIR=false;
|
value_PIR=false;
|
||||||
}
|
}
|
||||||
d.lastsent=millis();
|
d.lastsent=millis();
|
||||||
}
|
}
|
||||||
|
@ -256,7 +268,8 @@ void loop_PIR()
|
||||||
void loopHandler() {
|
void loopHandler() {
|
||||||
|
|
||||||
#ifdef SENSOR_DHT22
|
#ifdef SENSOR_DHT22
|
||||||
loop_DHT22();
|
loop_DHT22_temperature();
|
||||||
|
loop_DHT22_humidity();
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifdef SENSOR_BH1750
|
#ifdef SENSOR_BH1750
|
||||||
|
|
Loading…
Reference in New Issue