2020-08-15 17:45:02 +00:00
|
|
|
//#define DEBUG
|
|
|
|
|
|
|
|
|
|
|
|
#include "Arduino.h"
|
|
|
|
|
2020-09-26 17:48:49 +00:00
|
|
|
#include <Adafruit_Sensor.h> //required for dht library
|
|
|
|
#include <DHT.h>
|
2020-08-15 17:45:02 +00:00
|
|
|
#include <Wire.h>
|
2020-08-16 09:09:12 +00:00
|
|
|
#include <BH1750.h>
|
|
|
|
|
|
|
|
// data/homie/config.json hochladen mit platformio run --target uploadfs
|
2020-08-15 17:45:02 +00:00
|
|
|
|
|
|
|
/*
|
2020-08-16 09:09:12 +00:00
|
|
|
float humidity; //[%RH] DHT
|
|
|
|
float temperature; //[deg C] DHT
|
|
|
|
float light; //[Lux] BH1750
|
2020-10-01 20:19:11 +00:00
|
|
|
bool movement //true bei pir output hight, false wenn low HC12?501?
|
2020-08-15 17:45:02 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//GPIO2 is blue led
|
2020-08-16 09:09:12 +00:00
|
|
|
#define DHTPIN D7 // Digital pin connected to the DHT sensor
|
|
|
|
//DHT dht(13,DHT22,11); //default:11
|
|
|
|
DHT dht(DHTPIN,DHT22,11); //default:11
|
|
|
|
|
|
|
|
#define PIRPIN D6 //pir sensor needs 5v. output level is 3.3v
|
2020-08-15 17:45:02 +00:00
|
|
|
|
2020-08-16 09:09:12 +00:00
|
|
|
BH1750 lightMeter(0x23);
|
2020-08-15 17:45:02 +00:00
|
|
|
|
|
|
|
//timings
|
|
|
|
unsigned long lastsensorreadtime=0;
|
|
|
|
unsigned long sensorupdatedelay=60000; //delay for reading and transmitting
|
|
|
|
unsigned long lastPIRtime=0;
|
2020-08-16 09:09:12 +00:00
|
|
|
unsigned long PIRdelay=100; //polling delay
|
2020-10-01 20:19:11 +00:00
|
|
|
unsigned long lastPIRSendValueTime=0;
|
|
|
|
unsigned long PIRMaxDelay=1000*60*10; //maximum time until current value is send
|
2020-08-15 17:45:02 +00:00
|
|
|
bool motion=false;
|
|
|
|
|
|
|
|
|
|
|
|
#include <Homie.h>
|
2020-08-16 09:09:12 +00:00
|
|
|
#define FW_NAME "sensoresp3"
|
2020-08-15 17:45:02 +00:00
|
|
|
#define FW_VERSION "1.0.0"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
HomieNode sensorNode("sensors", "Sensors","sensors");
|
|
|
|
|
|
|
|
|
|
|
|
char tempstring[16]; //for dtostrf
|
|
|
|
|
|
|
|
|
|
|
|
void loopHandler();
|
|
|
|
|
|
|
|
void setup() {
|
|
|
|
Serial.begin(115200);
|
|
|
|
Serial.println();
|
|
|
|
Serial.println("Welcome");
|
|
|
|
|
2020-09-26 17:48:49 +00:00
|
|
|
pinMode(PIRPIN, INPUT_PULLUP);
|
2020-08-16 09:09:12 +00:00
|
|
|
|
2020-08-15 17:45:02 +00:00
|
|
|
Serial.println("initializing dht");
|
2020-08-16 09:09:12 +00:00
|
|
|
dht.begin(); // dht pins: 1=power, 2=data, 3=NC, 4=GND. 10k from data to power needed
|
|
|
|
|
|
|
|
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"));
|
2020-08-15 17:45:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Serial.println("connecting..");
|
|
|
|
|
|
|
|
Homie_setFirmware(FW_NAME, FW_VERSION);
|
|
|
|
Homie_setBrand(FW_NAME);
|
|
|
|
Homie.setLoopFunction(loopHandler);
|
|
|
|
|
|
|
|
|
2020-08-16 09:09:12 +00:00
|
|
|
sensorNode.advertise("temperature");
|
2020-08-15 17:45:02 +00:00
|
|
|
sensorNode.advertise("humidity");
|
|
|
|
sensorNode.advertise("light");
|
|
|
|
sensorNode.advertise("motion");
|
|
|
|
|
|
|
|
|
|
|
|
Homie.setup();
|
|
|
|
Serial.println("connected"); //wird nicht ausgegeben. keine ahnung warum.
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void loop() {
|
|
|
|
Homie.loop();
|
|
|
|
}
|
|
|
|
|
|
|
|
void loopHandler() {
|
|
|
|
|
2020-10-01 20:19:11 +00:00
|
|
|
if (millis() >= (lastPIRtime+PIRdelay)){
|
2020-08-15 17:45:02 +00:00
|
|
|
if (digitalRead(PIRPIN)){
|
|
|
|
if (!motion) { //changed?
|
|
|
|
Homie.getLogger() << "motion " << ": " << "true" << endl;
|
|
|
|
sensorNode.setProperty("motion").send(String("true"));
|
|
|
|
}
|
|
|
|
motion=true;
|
2020-10-01 20:19:11 +00:00
|
|
|
lastPIRSendValueTime=millis();
|
2020-08-15 17:45:02 +00:00
|
|
|
}else{
|
|
|
|
if (motion) { //changed?
|
|
|
|
Homie.getLogger() << "motion " << ": " << "false" << endl;
|
|
|
|
sensorNode.setProperty("motion").send(String("false"));
|
|
|
|
}
|
|
|
|
motion=false;
|
2020-10-01 20:19:11 +00:00
|
|
|
lastPIRSendValueTime=millis();
|
2020-08-15 17:45:02 +00:00
|
|
|
}
|
|
|
|
lastPIRtime=millis();
|
|
|
|
}
|
2020-10-01 20:19:11 +00:00
|
|
|
if (millis() >= (lastPIRSendValueTime+PIRMaxDelay)) { //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;
|
|
|
|
}
|
|
|
|
lastPIRSendValueTime=millis();
|
|
|
|
}
|
2020-08-15 17:45:02 +00:00
|
|
|
|
|
|
|
if (millis() >= (lastsensorreadtime+sensorupdatedelay))
|
|
|
|
{
|
|
|
|
Serial.println("Sending");
|
|
|
|
|
|
|
|
if (WiFi.status() != WL_CONNECTED) //restart if wifi signal loss
|
|
|
|
{
|
|
|
|
ESP.reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
float temperatureDHT = dht.readTemperature();
|
|
|
|
if (!(isnan(temperatureDHT) == 1)){ //success
|
2020-08-16 09:09:12 +00:00
|
|
|
Homie.getLogger() << "temperature " << ": " << temperatureDHT << endl;
|
|
|
|
sensorNode.setProperty("temperature").send(String(temperatureDHT));
|
2020-08-15 17:45:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
float humidityDHT = dht.readHumidity();
|
|
|
|
if (!(isnan(humidityDHT) == 1)){ //success
|
|
|
|
Homie.getLogger() << "humidity " << ": " << humidityDHT << endl;
|
|
|
|
sensorNode.setProperty("humidity").send(String(humidityDHT));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2020-08-16 09:09:12 +00:00
|
|
|
float light = lightMeter.readLightLevel(); // [lux]
|
2020-08-15 17:45:02 +00:00
|
|
|
Homie.getLogger() << "light " << ": " << light << endl;
|
|
|
|
sensorNode.setProperty("light").send(String(light));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
lastsensorreadtime=millis();
|
|
|
|
}
|
|
|
|
}
|