separate sensors into ifdef blocks
This commit is contained in:
parent
5309dd7d8c
commit
12f65702d0
271
src/main.cpp
271
src/main.cpp
|
@ -1,14 +1,72 @@
|
||||||
//#define DEBUG
|
//#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 "Arduino.h"
|
||||||
|
|
||||||
#include <Adafruit_Sensor.h> //required for dht library
|
#include <Homie.h>
|
||||||
#include <DHT.h>
|
#define FW_NAME "sensoresp" //gets printed on topic/$fw/name
|
||||||
#include <Wire.h>
|
#define FW_VERSION "1.0.0" //gets printed on topic/$fw/version
|
||||||
#include <BH1750.h>
|
|
||||||
|
/*
|
||||||
|
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
|
||||||
|
|
||||||
|
#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
|
||||||
|
|
||||||
|
|
||||||
|
#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
|
// 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 humidity; //[%RH] DHT
|
||||||
|
@ -19,56 +77,40 @@
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
//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
|
|
||||||
|
|
||||||
#define PIRPIN D6 //pir sensor needs 5v. output level is 3.3v
|
|
||||||
|
|
||||||
BH1750 lightMeter(0x23);
|
|
||||||
|
|
||||||
//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;
|
|
||||||
|
|
||||||
|
|
||||||
#include <Homie.h>
|
|
||||||
#define FW_NAME "sensoresp3"
|
|
||||||
#define FW_VERSION "1.0.0"
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
HomieNode sensorNode("sensors", "Sensors","sensors");
|
|
||||||
|
|
||||||
|
HomieNode sensorNode("sensors", "Sensors","sensors"); //id, name, type
|
||||||
|
|
||||||
char tempstring[16]; //for dtostrf
|
char tempstring[16]; //for dtostrf
|
||||||
|
|
||||||
|
|
||||||
void loopHandler();
|
void loopHandler();
|
||||||
|
void checkESPStatus();
|
||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
Serial.begin(115200);
|
Serial.begin(115200);
|
||||||
Serial.println();
|
Serial.println();
|
||||||
Serial.println("Welcome");
|
Serial.println("Booting");
|
||||||
|
|
||||||
pinMode(PIRPIN, INPUT_PULLUP);
|
|
||||||
|
|
||||||
Serial.println("initializing dht");
|
#ifdef SENSOR_DHT22
|
||||||
dht.begin(); // dht pins: 1=power, 2=data, 3=NC, 4=GND. 10k from data to power needed
|
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");
|
#ifdef SENSOR_BH1750
|
||||||
Wire.begin(); //initialize i2c. SDA=D2, SCL=D1
|
Serial.println("initializing bh1750");
|
||||||
if (lightMeter.begin(BH1750::CONTINUOUS_HIGH_RES_MODE)) {
|
Wire.begin(); //initialize i2c. SDA=D2, SCL=D1
|
||||||
Serial.println(F("BH1750 Advanced begin"));
|
if (lightMeter.begin(BH1750::CONTINUOUS_HIGH_RES_MODE)) {
|
||||||
} else {
|
Serial.println(F("BH1750 Advanced begin"));
|
||||||
Serial.println(F("Error initialising BH1750"));
|
} else {
|
||||||
}
|
Serial.println(F("Error initialising BH1750"));
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
#ifdef SENSOR_PIR
|
||||||
|
pinMode(PIRPIN, INPUT_PULLUP);
|
||||||
|
#endif
|
||||||
|
|
||||||
Serial.println("connecting..");
|
Serial.println("connecting..");
|
||||||
|
|
||||||
|
@ -77,62 +119,37 @@ void setup() {
|
||||||
Homie.setLoopFunction(loopHandler);
|
Homie.setLoopFunction(loopHandler);
|
||||||
|
|
||||||
|
|
||||||
sensorNode.advertise("temperature");
|
#ifdef SENSOR_DHT22
|
||||||
sensorNode.advertise("humidity");
|
sensorNode.advertise("temperature");
|
||||||
sensorNode.advertise("light");
|
sensorNode.advertise("humidity");
|
||||||
sensorNode.advertise("motion");
|
#endif
|
||||||
|
|
||||||
|
#ifdef SENSOR_BH1750
|
||||||
|
sensorNode.advertise("light");
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
#ifdef SENSOR_PIR
|
||||||
|
sensorNode.advertise("motion");
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
Homie.setup();
|
Homie.setup();
|
||||||
Serial.println("connected"); //wird nicht ausgegeben. keine ahnung warum.
|
Serial.println("connected"); //wird nicht ausgegeben. keine ahnung warum.
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void loop() {
|
void loop() {
|
||||||
Homie.loop();
|
Homie.loop();
|
||||||
}
|
}
|
||||||
|
|
||||||
void loopHandler() {
|
|
||||||
|
|
||||||
if (millis() >= (lastPIRtime+PIRdelay)){
|
#ifdef SENSOR_DHT22
|
||||||
if (digitalRead(PIRPIN)){
|
void loop_DHT22()
|
||||||
if (!motion) { //changed?
|
{
|
||||||
Homie.getLogger() << "motion " << ": " << "true" << endl;
|
if (millis() >= (lastDHT22time+readdelay_DHT22))
|
||||||
sensorNode.setProperty("motion").send(String("true"));
|
|
||||||
}
|
|
||||||
motion=true;
|
|
||||||
lastPIRSendValueTime=millis();
|
|
||||||
}else{
|
|
||||||
if (motion) { //changed?
|
|
||||||
Homie.getLogger() << "motion " << ": " << "false" << endl;
|
|
||||||
sensorNode.setProperty("motion").send(String("false"));
|
|
||||||
}
|
|
||||||
motion=false;
|
|
||||||
lastPIRSendValueTime=millis();
|
|
||||||
}
|
|
||||||
lastPIRtime=millis();
|
|
||||||
}
|
|
||||||
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();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (millis() >= (lastsensorreadtime+sensorupdatedelay))
|
|
||||||
{
|
{
|
||||||
Serial.println("Sending");
|
Serial.println("Sending");
|
||||||
|
checkESPStatus();
|
||||||
if (WiFi.status() != WL_CONNECTED) //restart if wifi signal loss
|
|
||||||
{
|
|
||||||
ESP.reset();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
float temperatureDHT = dht.readTemperature();
|
float temperatureDHT = dht.readTemperature();
|
||||||
|
@ -148,13 +165,93 @@ void loopHandler() {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
lastDHT22time=millis();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifdef SENSOR_BH1750
|
||||||
|
void loop_BH1750()
|
||||||
|
{
|
||||||
|
if (millis() >= (lastBH1750time+readdelay_BH1750))
|
||||||
|
{
|
||||||
|
Serial.println("Sending");
|
||||||
|
checkESPStatus();
|
||||||
|
|
||||||
float light = lightMeter.readLightLevel(); // [lux]
|
float light = lightMeter.readLightLevel(); // [lux]
|
||||||
Homie.getLogger() << "light " << ": " << light << endl;
|
Homie.getLogger() << "light " << ": " << light << endl;
|
||||||
sensorNode.setProperty("light").send(String(light));
|
sensorNode.setProperty("light").send(String(light));
|
||||||
|
|
||||||
|
lastBH1750time=millis();
|
||||||
|
}
|
||||||
lastsensorreadtime=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();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue