//#define DEBUG // DHT22 #define SENSOR_DHT22 #define DHTPIN D7 // Digital pin connected to the DHT sensor // PIR Sensors HC-SR501 (modified to put out shortest pulse time) #define SENSOR_PIR #define PIRPIN D6 //pir sensor needs 5v. output level is 3.3v //BH1750 Lux Sensor #define SENSOR_BH1750 //GPIO2 is blue led on wemos_d1 #include "Arduino.h" #include #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 } */ #ifdef SENSOR_DHT22 #include //required for dht library #include 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 #endif #ifdef SENSOR_BH1750 #include #include BH1750 lightMeter(0x23); 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 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; #endif // data/homie/config.json hochladen mit platformio run --target uploadfs // config contains homie device name, mqtt ip and wifi credentials /* float humidity; //[%RH] DHT float temperature; //[deg C] DHT float light; //[Lux] BH1750 bool movement //true bei pir output hight, false wenn low HC12?501? */ HomieNode sensorNode("sensors", "Sensors","sensors"); //id, name, type char tempstring[16]; //for dtostrf void loopHandler(); void checkESPStatus(); void setup() { Serial.begin(115200); Serial.println(); 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 #endif #ifdef SENSOR_BH1750 Serial.println("initializing bh1750"); Wire.begin(); //initialize i2c. SDA=D2, SCL=D1 if (lightMeter.begin(BH1750::CONTINUOUS_HIGH_RES_MODE)) { Serial.println(F("BH1750 Advanced begin")); } else { Serial.println(F("Error initialising BH1750")); } #endif #ifdef SENSOR_PIR pinMode(PIRPIN, INPUT_PULLUP); #endif Serial.println("connecting.."); Homie_setFirmware(FW_NAME, FW_VERSION); Homie_setBrand(FW_NAME); Homie.setLoopFunction(loopHandler); #ifdef SENSOR_DHT22 sensorNode.advertise("temperature"); sensorNode.advertise("humidity"); #endif #ifdef SENSOR_BH1750 sensorNode.advertise("light"); #endif #ifdef SENSOR_PIR sensorNode.advertise("motion"); #endif Homie.setup(); Serial.println("connected"); //wird nicht ausgegeben. keine ahnung warum. } void loop() { Homie.loop(); } #ifdef SENSOR_DHT22 void loop_DHT22() { if (millis() >= (lastDHT22time+readdelay_DHT22)) { Serial.println("Sending"); checkESPStatus(); float temperatureDHT = dht.readTemperature(); if (!(isnan(temperatureDHT) == 1)){ //success Homie.getLogger() << "temperature " << ": " << temperatureDHT << endl; sensorNode.setProperty("temperature").send(String(temperatureDHT)); } float humidityDHT = dht.readHumidity(); if (!(isnan(humidityDHT) == 1)){ //success Homie.getLogger() << "humidity " << ": " << humidityDHT << endl; sensorNode.setProperty("humidity").send(String(humidityDHT)); } lastDHT22time=millis(); } } #endif #ifdef SENSOR_BH1750 void loop_BH1750() { if (millis() >= (lastBH1750time+readdelay_BH1750)) { Serial.println("Sending"); checkESPStatus(); float light = lightMeter.readLightLevel(); // [lux] Homie.getLogger() << "light " << ": " << light << endl; sensorNode.setProperty("light").send(String(light)); lastBH1750time=millis(); } } #endif #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(); } lastPIRtime=millis(); } if (millis() >= (lastsend_PIR+senddelaymax_PIR)) { //send current value after some long time if (digitalRead(PIRPIN)){ Homie.getLogger() << "motion resend " << ": " << "true" << endl; sensorNode.setProperty("motion").send(String("true")); motion=true; }else{ Homie.getLogger() << "motion resend " << ": " << "false" << endl; sensorNode.setProperty("motion").send(String("false")); motion=false; } lastsend_PIR=millis(); } } #endif void loopHandler() { #ifdef SENSOR_DHT22 #endif #ifdef SENSOR_BH1750 #endif #ifdef SENSOR_PIR loop_PIR(); #endif } void checkESPStatus() { if (WiFi.status() != WL_CONNECTED) //restart if wifi signal loss { ESP.reset(); } }