From 01ee9cd525214dfbafc6b305db1fe811fb891a10 Mon Sep 17 00:00:00 2001 From: Fisch Date: Thu, 4 Nov 2021 19:16:30 +0100 Subject: [PATCH] move pir to class and rename to hcsr501 --- include/sensor_hcsr501.cpp | 66 ++++++++++++++++++++++++++++++++ include/sensor_hcsr501.h | 32 ++++++++++++++++ include/sensor_ml8511.cpp | 1 + platformio.ini | 42 +++++--------------- src/main.cpp | 78 +++++++++++--------------------------- 5 files changed, 132 insertions(+), 87 deletions(-) create mode 100644 include/sensor_hcsr501.cpp create mode 100644 include/sensor_hcsr501.h diff --git a/include/sensor_hcsr501.cpp b/include/sensor_hcsr501.cpp new file mode 100644 index 0000000..541a900 --- /dev/null +++ b/include/sensor_hcsr501.cpp @@ -0,0 +1,66 @@ +// PIR Sensors HC-SR501 +// pir sensor needs 5v through an inductor for filtering. output level is 3.3v +// 100nF capacitor SENSOR_HCSR501_minchangeshould be soldered between pins 12 and 13 of BISS0001 to stop interference from esp causing false triggers (in some setups). source: https://www.letscontrolit.com/forum/viewtopic.php?t=671 +// hc-sr501 should also be a few cm away from the esp. interference can cause false triggering +// poti closer to jumper is sensitivity (cw increases). other poti is pulse time (cw increases). +// time set to output around 30s pulse +#include "sensor_hcsr501.h" + + +Sensor_HCSR501::Sensor_HCSR501(int pin) +{ + hcsr501pin=pin; + sensordata data; +} + +void Sensor_HCSR501::init() //Things to be done during setup() +{ + Serial.println("initializing HCSR501"); + pinMode(hcsr501pin, INPUT_PULLUP); + init_ok=true; +} + +//Also called during setup() +void Sensor_HCSR501::setSettings(unsigned long senddelaymax, unsigned long readdelay) +{ + data.senddelaymax=senddelaymax; + data.readdelay=readdelay; +} + +//Called during setup +void Sensor_HCSR501::advertise(HomieNode& p_sensorNode) +{ + sensorNode = &p_sensorNode; + sensorNode->advertise("motion"); +} + +void Sensor_HCSR501::sensorloop() +{ + if (init_ok) { + sensordata &d=data; + bool _changed=false; + if (millis() >= (d.lastreadtime+d.readdelay)) { + if (digitalRead(hcsr501pin) != (d.value>0)){ + _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(hcsr501pin)){ + Homie.getLogger() << "motion " << ": " << "true" << endl; + sensorNode->setProperty("motion").send(String("true")); + d.value=true; + }else{ + Homie.getLogger() << "motion " << ": " << "false" << endl; + sensorNode->setProperty("motion").send(String("false")); + d.value=false; + } + d.lastsent=millis(); + } + } +} + + diff --git a/include/sensor_hcsr501.h b/include/sensor_hcsr501.h new file mode 100644 index 0000000..1c3858a --- /dev/null +++ b/include/sensor_hcsr501.h @@ -0,0 +1,32 @@ +#ifndef SENSOR_HCSR501_H +#define SENSOR_HCSR501_H + +#include "sensordata.h" +#include + + + +class Sensor_HCSR501 +{ + +private: + HomieNode *sensorNode; //reference to HomieNode + + int hcsr501pin; + + struct sensordata data; //struct values are changed in setup() + + bool init_ok; +public: + Sensor_HCSR501(int pin); + + + void init(); + void setSettings(unsigned long senddelaymax, unsigned long readdelay); + void advertise(HomieNode& p_sensorNode); + void sensorloop(); + +}; + +#endif + diff --git a/include/sensor_ml8511.cpp b/include/sensor_ml8511.cpp index bc923bd..eb5f9c2 100644 --- a/include/sensor_ml8511.cpp +++ b/include/sensor_ml8511.cpp @@ -1,3 +1,4 @@ +//ML8511 UV Sensor outputs an analog voltage. ML8511PIN needs to be an ADC pin //value as uvIntensity (mW/cm^2) #include "sensor_ml8511.h" diff --git a/platformio.ini b/platformio.ini index d1f1e15..62e0fd4 100644 --- a/platformio.ini +++ b/platformio.ini @@ -82,27 +82,13 @@ monitor_port = /dev/ttyUSB0 monitor_speed = 115200 build_flags = - - -D SENSOR_HS1101 - -D SENSOR_HS1101_PIN=D5 - -D SENSOR_HS1101_humidity_minchange=10 - - - -D SENSOR_BH1750 - -D SENSOR_BH1750_light_minchange=10.0 - -D SENSOR_BH1750_light_senddelaymax=1000*60*2 - - -D SENSOR_ML8511 - -D SENSOR_ML8511_PIN=A0 - -D SENSOR_ML8511_minchange=0.2 - - + -D SENSOR_HCSR501 + -D SENSOR_HCSR501_PIN=D0 lib_deps = ArduinoJson@6.16.1 #dependency of homie. using older version because of "ambiguous overload for operator|" error Homie@3.0.0 - claws/BH1750@1.1.4 #Arbeitszimmer [env:sensoresp1] @@ -123,10 +109,8 @@ build_flags = -D dataBMP180_temperature_minchange=0.2 -D dataBMP180_pressure_minchange=0.5 - -D SENSOR_PIR - -D PIRPIN=D6 - -D dataPIR_readdelay=100 - -D dataPIR_senddelaymax=1000*60*10 + -D SENSOR_HCSR501 + -D SENSOR_HCSR501_PIN=D6 -D SENSOR_LDR -D SENSOR_LDR_CALIB1 @@ -187,10 +171,8 @@ build_flags = -D dataDHT22_temperature_minchange=0.2 -D dataDHT22_humidity_minchange=1.0 - -D SENSOR_PIR - -D PIRPIN=D6 - -D dataPIR_readdelay=100 - -D dataPIR_senddelaymax=1000*60*10 + -D SENSOR_HCSR501 + -D SENSOR_HCSR501_PIN=D6 -D SENSOR_BH1750 -D dataBH1750_minchange=10.0 @@ -218,10 +200,8 @@ build_flags = -D dataDHT22_temperature_minchange=0.2 -D dataDHT22_humidity_minchange=1.0 - -D SENSOR_PIR - -D PIRPIN=D0 - -D dataPIR_readdelay=100 - -D dataPIR_senddelaymax=1000*60*10 + -D SENSOR_HCSR501 + -D SENSOR_HCSR501_PIN=D0 -D SENSOR_BH1750 -D dataBH1750_minchange=10.0 @@ -258,10 +238,8 @@ build_flags = -D dataDHT22_temperature_minchange=0.2 -D dataDHT22_humidity_minchange=1.0 - -D SENSOR_PIR - -D PIRPIN=D6 - -D dataPIR_readdelay=100 - -D dataPIR_senddelaymax=1000*60*10 + -D SENSOR_HCSR501 + -D SENSOR_HCSR501_PIN=D6 -D SENSOR_RADAR -D RADARPIN=D5 diff --git a/src/main.cpp b/src/main.cpp index 60ac8c7..e173e60 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -134,7 +134,6 @@ #endif #ifdef SENSOR_ML8511 - //ML8511 UV Sensor outputs an analog voltage. ML8511PIN needs to be an ADC pin #include "sensor_ml8511.cpp" Sensor_ML8511 sensor_ml8511(SENSOR_ML8511_PIN); @@ -150,15 +149,16 @@ #endif -#ifdef SENSOR_PIR - // PIR Sensors HC-SR501 - // pir sensor needs 5v through an inductor for filtering. output level is 3.3v - // 100nF capacitor SENSOR_ML8511_minchangeshould be soldered between pins 12 and 13 of BISS0001 to stop interference from esp causing false triggers (in some setups). source: https://www.letscontrolit.com/forum/viewtopic.php?t=671 - // hc-sr501 should also be a few cm away from the esp. interference can cause false triggering - // poti closer to jumper is sensitivity (cw increases). other poti is pulse time (cw increases). - // time set to output around 30s pulse - sensordata dataPIR; - bool value_PIR=false; +#ifdef SENSOR_HCSR501 + #include "sensor_hcsr501.cpp" + Sensor_HCSR501 sensor_hcsr501(SENSOR_HCSR501_PIN); + + #ifndef SENSOR_HCSR501_senddelaymax + #define SENSOR_HCSR501_senddelaymax 1000*60*10 + #endif + #ifndef SENSOR_HCSR501_readdelayML8511 + #define SENSOR_HCSR501_readdelay 100 + #endif #endif #ifdef SENSOR_RADAR @@ -390,18 +390,12 @@ void setup() { #endif - #ifdef SENSOR_PIR - Serial.println("initializing pir"); - pinMode(PIRPIN, INPUT_PULLUP); - #ifdef dataPIR_readdelay - dataPIR.readdelay=dataPIR_readdelay; - #endif - #ifdef dataPIR_senddelaymax - dataPIR.senddelaymax=dataPIR_senddelaymax; - #endif + #ifdef SENSOR_HCSR501 + sensor_hcsr501.init(); + sensor_hcsr501.setSettings(SENSOR_HCSR501_senddelaymax,SENSOR_HCSR501_readdelay); #endif - #ifdef SENSOR_RADAR + #ifdef SENSOR_RADAR Serial.println("initializing radar"); pinMode(RADARPIN, INPUT); #ifdef dataRADAR_readdelay @@ -567,15 +561,17 @@ void setup() { sensor_ml8511.advertise(sensorNode); #endif + + #ifdef SENSOR_HCSR501 + sensor_hcsr501.advertise(sensorNode); + #endif + #ifdef SENSOR_LDR sensorNode.advertise("light"); analogRead(LDR_PIN); //first reading could be false #endif - #ifdef SENSOR_PIR - sensorNode.advertise("motion"); - #endif #ifdef SENSOR_RADAR sensorNode.advertise("radar"); @@ -660,35 +656,6 @@ void loop_LDR() } #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 - #ifdef SENSOR_RADAR void loop_RADAR() @@ -1098,13 +1065,14 @@ void loopHandler() { sensor_ml8511.sensorloop(); #endif + #ifdef SENSOR_HCSR501 + sensor_hcsr501.sensorloop(); + #endif + #ifdef SENSOR_LDR loop_LDR(); #endif - #ifdef SENSOR_PIR - loop_PIR(); - #endif #ifdef SENSOR_RADAR loop_RADAR();