separate sensors into ifdef blocks

This commit is contained in:
interfisch 2020-10-02 19:45:33 +02:00
parent 5309dd7d8c
commit 12f65702d0
1 changed files with 186 additions and 89 deletions

View File

@ -1,14 +1,72 @@
//#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 <Adafruit_Sensor.h> //required for dht library
#include <DHT.h>
#include <Wire.h>
#include <BH1750.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 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
// config contains homie device name, mqtt ip and wifi credentials
/*
float humidity; //[%RH] DHT
@ -19,49 +77,27 @@
//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
void loopHandler();
void checkESPStatus();
void setup() {
Serial.begin(115200);
Serial.println();
Serial.println("Welcome");
Serial.println("Booting");
pinMode(PIRPIN, INPUT_PULLUP);
#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)) {
@ -69,6 +105,12 @@ void setup() {
} else {
Serial.println(F("Error initialising BH1750"));
}
#endif
#ifdef SENSOR_PIR
pinMode(PIRPIN, INPUT_PULLUP);
#endif
Serial.println("connecting..");
@ -77,62 +119,37 @@ void setup() {
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();
}
void loopHandler() {
if (millis() >= (lastPIRtime+PIRdelay)){
if (digitalRead(PIRPIN)){
if (!motion) { //changed?
Homie.getLogger() << "motion " << ": " << "true" << endl;
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))
#ifdef SENSOR_DHT22
void loop_DHT22()
{
if (millis() >= (lastDHT22time+readdelay_DHT22))
{
Serial.println("Sending");
if (WiFi.status() != WL_CONNECTED) //restart if wifi signal loss
{
ESP.reset();
}
checkESPStatus();
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]
Homie.getLogger() << "light " << ": " << light << endl;
sensorNode.setProperty("light").send(String(light));
lastsensorreadtime=millis();
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();
}
}