312 lines
7.3 KiB
C++
312 lines
7.3 KiB
C++
//#define DEBUG
|
|
|
|
/* Listed below are all possible defines with comments. Defines are done in platformio.ini under the sensors environment
|
|
// DHT22
|
|
#define SENSOR_DHT22
|
|
#define DHTPIN D7 // Digital pin connected to the DHT sensor. // dht pins: 1=power, 2=data, 3=NC, 4=GND. 10k from data to power needed
|
|
|
|
// 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 <Homie.h>
|
|
#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 lastreadtime=0;
|
|
unsigned long readdelay=1000*10; //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*5; //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
|
|
|
|
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
|
|
|
|
#ifdef SENSOR_BH1750
|
|
#include <Wire.h>
|
|
#include <BH1750.h>
|
|
BH1750 lightMeter(0x23);
|
|
sensordata dataBH1750;
|
|
float value_lightBH1750=0;
|
|
#endif
|
|
|
|
|
|
#ifdef SENSOR_PIR
|
|
sensordata dataPIR;
|
|
bool value_PIR=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();
|
|
#ifdef dataDHT22_temperature_minchange
|
|
dataDHT22_temperature.minchange=dataDHT22_temperature_minchange;
|
|
#endif
|
|
#ifdef dataDHT22_humidity_minchange
|
|
dataDHT22_humidity.minchange=dataDHT22_humidity.minchange;
|
|
#endif
|
|
#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"));
|
|
}
|
|
#ifdef dataBH1750_minchange
|
|
dataBH1750.minchange=dataBH1750_minchange;
|
|
#endif
|
|
#ifdef dataBH1750_senddelaymax
|
|
dataBH1750.senddelaymax=dataBH1750_senddelaymax;
|
|
#endif
|
|
#endif
|
|
|
|
|
|
#ifdef SENSOR_PIR
|
|
pinMode(PIRPIN, INPUT_PULLUP);
|
|
#ifdef dataPIR_readdelay
|
|
dataPIR.readdelay=dataPIR_readdelay;
|
|
#endif
|
|
#ifdef dataPIR_senddelaymax
|
|
dataPIR.senddelaymax=dataPIR_senddelaymax;
|
|
#endif
|
|
#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");
|
|
lightMeter.readLightLevel(); //make first reading, could be 0
|
|
#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_temperature()
|
|
{
|
|
sensordata &d=dataDHT22_temperature;
|
|
bool _changed=false;
|
|
|
|
if (millis() >= (d.lastreadtime+d.readdelay)) {
|
|
value_temperatureDHT = dht.readTemperature();
|
|
if (fabs(d.lastsentvalue-value_temperatureDHT)>=d.minchange){
|
|
_changed=true;
|
|
}
|
|
d.lastreadtime=millis();
|
|
}
|
|
|
|
if (_changed || millis() >= (d.lastsent+d.senddelaymax)) {
|
|
Serial.print("Sending DHT22_temperature. reason=");
|
|
if (_changed) Serial.println("change"); else Serial.println("time");
|
|
checkESPStatus();
|
|
|
|
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;
|
|
|
|
|
|
if (millis() >= (d.lastreadtime+d.readdelay)) {
|
|
value_humidityDHT = dht.readHumidity();
|
|
if (fabs(d.lastsentvalue-value_humidityDHT)>=d.minchange){
|
|
_changed=true;
|
|
}
|
|
d.lastreadtime=millis();
|
|
}
|
|
|
|
if (_changed || millis() >= (d.lastsent+d.senddelaymax)) {
|
|
Serial.print("Sending DHT22_humidity. reason=");
|
|
if (_changed) Serial.println("change"); else Serial.println("time");
|
|
checkESPStatus();
|
|
|
|
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();
|
|
}
|
|
}
|
|
#endif
|
|
|
|
#ifdef SENSOR_BH1750
|
|
void loop_BH1750()
|
|
{
|
|
sensordata &d=dataBH1750;
|
|
|
|
bool _changed=false;
|
|
if (millis() >= (d.lastreadtime+d.readdelay)) {
|
|
value_lightBH1750 = lightMeter.readLightLevel(); // [lux]
|
|
if (fabs(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();
|
|
|
|
Homie.getLogger() << "light " << ": " << value_lightBH1750 << endl;
|
|
sensorNode.setProperty("light").send(String(value_lightBH1750));
|
|
d.lastsentvalue=value_lightBH1750;
|
|
|
|
d.lastsent=millis();
|
|
}
|
|
}
|
|
#endif
|
|
|
|
#ifdef SENSOR_PIR
|
|
void loop_PIR()
|
|
{
|
|
sensordata &d=dataPIR;
|
|
bool _changed=false;
|
|
if (millis() >= (d.lastreadtime+d.readdelay)) {
|
|
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() << "motion " << ": " << "true" << endl;
|
|
sensorNode.setProperty("motion").send(String("true"));
|
|
value_PIR=true;
|
|
}else{
|
|
Homie.getLogger() << "motion " << ": " << "false" << endl;
|
|
sensorNode.setProperty("motion").send(String("false"));
|
|
value_PIR=false;
|
|
}
|
|
d.lastsent=millis();
|
|
}
|
|
}
|
|
#endif
|
|
|
|
|
|
|
|
void loopHandler() {
|
|
|
|
#ifdef SENSOR_DHT22
|
|
loop_DHT22_temperature();
|
|
loop_DHT22_humidity();
|
|
#endif
|
|
|
|
#ifdef SENSOR_BH1750
|
|
loop_BH1750();
|
|
#endif
|
|
|
|
|
|
#ifdef SENSOR_PIR
|
|
loop_PIR();
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
void checkESPStatus()
|
|
{
|
|
if (WiFi.status() != WL_CONNECTED) //restart if wifi signal loss
|
|
{
|
|
ESP.reset();
|
|
}
|
|
}
|