|
|
|
@ -1,74 +1,116 @@
@@ -1,74 +1,116 @@
|
|
|
|
|
//#define DEBUG
|
|
|
|
|
|
|
|
|
|
// DHT22
|
|
|
|
|
#define SENSOR_DHT22 |
|
|
|
|
#define DHTPIN D7 // Digital pin connected to the DHT sensor
|
|
|
|
|
|
|
|
|
|
#include "Arduino.h" |
|
|
|
|
// 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
|
|
|
|
|
|
|
|
|
|
#include <Adafruit_Sensor.h> //required for dht library |
|
|
|
|
#include <DHT.h> |
|
|
|
|
#include <Wire.h> |
|
|
|
|
#include <BH1750.h> |
|
|
|
|
//BH1750 Lux Sensor
|
|
|
|
|
#define SENSOR_BH1750 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//GPIO2 is blue led on wemos_d1
|
|
|
|
|
|
|
|
|
|
// data/homie/config.json hochladen mit platformio run --target uploadfs
|
|
|
|
|
|
|
|
|
|
#include "Arduino.h" |
|
|
|
|
|
|
|
|
|
#include <Homie.h> |
|
|
|
|
#define FW_NAME "sensoresp" //gets printed on topic/$fw/name
|
|
|
|
|
#define FW_VERSION "1.0.0" //gets printed on topic/$fw/version
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
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?
|
|
|
|
|
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 <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
|
|
|
|
|
|
|
|
|
|
#endif |
|
|
|
|
|
|
|
|
|
//GPIO2 is blue led
|
|
|
|
|
#define DHTPIN D7 // Digital pin connected to the DHT sensor
|
|
|
|
|
//DHT dht(13,DHT22,11); //default:11
|
|
|
|
|
DHT dht(DHTPIN,DHT22,11); //default:11
|
|
|
|
|
#ifdef SENSOR_BH1750 |
|
|
|
|
#include <Wire.h> |
|
|
|
|
#include <BH1750.h> |
|
|
|
|
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 |
|
|
|
|
|
|
|
|
|
#define PIRPIN D6 //pir sensor needs 5v. output level is 3.3v
|
|
|
|
|
|
|
|
|
|
BH1750 lightMeter(0x23); |
|
|
|
|
#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 |
|
|
|
|
|
|
|
|
|
//timings
|
|
|
|
|
unsigned long lastsensorreadtime=0; |
|
|
|
|
unsigned long sensorupdatedelay=60000; //delay for reading and transmitting
|
|
|
|
|
unsigned long lastPIRtime=0; |
|
|
|
|
unsigned long PIRdelay=100; //polling delay
|
|
|
|
|
unsigned long lastPIRSendValueTime=0; |
|
|
|
|
unsigned long PIRMaxDelay=1000*60*10; //maximum time until current value is send
|
|
|
|
|
bool motion=false; |
|
|
|
|
|
|
|
|
|
// data/homie/config.json hochladen mit platformio run --target uploadfs
|
|
|
|
|
// config contains homie device name, mqtt ip and wifi credentials
|
|
|
|
|
|
|
|
|
|
#include <Homie.h> |
|
|
|
|
#define FW_NAME "sensoresp3" |
|
|
|
|
#define FW_VERSION "1.0.0" |
|
|
|
|
/*
|
|
|
|
|
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"); |
|
|
|
|
|
|
|
|
|
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("Welcome"); |
|
|
|
|
Serial.println("Booting"); |
|
|
|
|
|
|
|
|
|
pinMode(PIRPIN, INPUT_PULLUP); |
|
|
|
|
|
|
|
|
|
Serial.println("initializing dht"); |
|
|
|
|
dht.begin(); // dht pins: 1=power, 2=data, 3=NC, 4=GND. 10k from data to power needed
|
|
|
|
|
#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 |
|
|
|
|
|
|
|
|
|
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")); |
|
|
|
|
} |
|
|
|
|
#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.."); |
|
|
|
|
|
|
|
|
@ -77,42 +119,96 @@ void setup() {
@@ -77,42 +119,96 @@ void setup() {
|
|
|
|
|
Homie.setLoopFunction(loopHandler); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
sensorNode.advertise("temperature"); |
|
|
|
|
sensorNode.advertise("humidity"); |
|
|
|
|
sensorNode.advertise("light"); |
|
|
|
|
sensorNode.advertise("motion"); |
|
|
|
|
#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();
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void loopHandler() { |
|
|
|
|
|
|
|
|
|
if (millis() >= (lastPIRtime+PIRdelay)){
|
|
|
|
|
#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; |
|
|
|
|
lastPIRSendValueTime=millis(); |
|
|
|
|
lastsend_PIR=millis(); |
|
|
|
|
}else{ |
|
|
|
|
if (motion) { //changed?
|
|
|
|
|
Homie.getLogger() << "motion " << ": " << "false" << endl; |
|
|
|
|
sensorNode.setProperty("motion").send(String("false")); |
|
|
|
|
} |
|
|
|
|
motion=false; |
|
|
|
|
lastPIRSendValueTime=millis(); |
|
|
|
|
lastsend_PIR=millis(); |
|
|
|
|
} |
|
|
|
|
lastPIRtime=millis(); |
|
|
|
|
} |
|
|
|
|
if (millis() >= (lastPIRSendValueTime+PIRMaxDelay)) { //send current value after some long time
|
|
|
|
|
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")); |
|
|
|
@ -122,39 +218,40 @@ void loopHandler() {
@@ -122,39 +218,40 @@ void loopHandler() {
|
|
|
|
|
sensorNode.setProperty("motion").send(String("false")); |
|
|
|
|
motion=false; |
|
|
|
|
} |
|
|
|
|
lastPIRSendValueTime=millis(); |
|
|
|
|
lastsend_PIR=millis(); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
#endif |
|
|
|
|
|
|
|
|
|
if (millis() >= (lastsensorreadtime+sensorupdatedelay)) |
|
|
|
|
{ |
|
|
|
|
Serial.println("Sending"); |
|
|
|
|
|
|
|
|
|
if (WiFi.status() != WL_CONNECTED) //restart if wifi signal loss
|
|
|
|
|
{ |
|
|
|
|
ESP.reset(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void loopHandler() { |
|
|
|
|
|
|
|
|
|
#ifdef SENSOR_DHT22 |
|
|
|
|
|
|
|
|
|
#endif |
|
|
|
|
|
|
|
|
|
#ifdef SENSOR_BH1750 |
|
|
|
|
|
|
|
|
|
#endif |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef SENSOR_PIR |
|
|
|
|
loop_PIR(); |
|
|
|
|
#endif |
|
|
|
|
|
|
|
|
|
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)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
float light = lightMeter.readLightLevel(); // [lux]
|
|
|
|
|
Homie.getLogger() << "light " << ": " << light << endl; |
|
|
|
|
sensorNode.setProperty("light").send(String(light)); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
lastsensorreadtime=millis(); |
|
|
|
|
|
|
|
|
|
void checkESPStatus() |
|
|
|
|
{ |
|
|
|
|
if (WiFi.status() != WL_CONNECTED) //restart if wifi signal loss
|
|
|
|
|
{ |
|
|
|
|
ESP.reset(); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|