move pir to class and rename to hcsr501

This commit is contained in:
interfisch 2021-11-04 19:16:30 +01:00
parent 90b1185795
commit 01ee9cd525
5 changed files with 132 additions and 87 deletions

View File

@ -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();
}
}
}

32
include/sensor_hcsr501.h Normal file
View File

@ -0,0 +1,32 @@
#ifndef SENSOR_HCSR501_H
#define SENSOR_HCSR501_H
#include "sensordata.h"
#include <Homie.h>
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

View File

@ -1,3 +1,4 @@
//ML8511 UV Sensor outputs an analog voltage. ML8511PIN needs to be an ADC pin
//value as uvIntensity (mW/cm^2) //value as uvIntensity (mW/cm^2)
#include "sensor_ml8511.h" #include "sensor_ml8511.h"

View File

@ -82,27 +82,13 @@ monitor_port = /dev/ttyUSB0
monitor_speed = 115200 monitor_speed = 115200
build_flags = build_flags =
-D SENSOR_HCSR501
-D SENSOR_HS1101 -D SENSOR_HCSR501_PIN=D0
-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
lib_deps = lib_deps =
ArduinoJson@6.16.1 #dependency of homie. using older version because of "ambiguous overload for operator|" error ArduinoJson@6.16.1 #dependency of homie. using older version because of "ambiguous overload for operator|" error
Homie@3.0.0 Homie@3.0.0
claws/BH1750@1.1.4
#Arbeitszimmer #Arbeitszimmer
[env:sensoresp1] [env:sensoresp1]
@ -123,10 +109,8 @@ build_flags =
-D dataBMP180_temperature_minchange=0.2 -D dataBMP180_temperature_minchange=0.2
-D dataBMP180_pressure_minchange=0.5 -D dataBMP180_pressure_minchange=0.5
-D SENSOR_PIR -D SENSOR_HCSR501
-D PIRPIN=D6 -D SENSOR_HCSR501_PIN=D6
-D dataPIR_readdelay=100
-D dataPIR_senddelaymax=1000*60*10
-D SENSOR_LDR -D SENSOR_LDR
-D SENSOR_LDR_CALIB1 -D SENSOR_LDR_CALIB1
@ -187,10 +171,8 @@ build_flags =
-D dataDHT22_temperature_minchange=0.2 -D dataDHT22_temperature_minchange=0.2
-D dataDHT22_humidity_minchange=1.0 -D dataDHT22_humidity_minchange=1.0
-D SENSOR_PIR -D SENSOR_HCSR501
-D PIRPIN=D6 -D SENSOR_HCSR501_PIN=D6
-D dataPIR_readdelay=100
-D dataPIR_senddelaymax=1000*60*10
-D SENSOR_BH1750 -D SENSOR_BH1750
-D dataBH1750_minchange=10.0 -D dataBH1750_minchange=10.0
@ -218,10 +200,8 @@ build_flags =
-D dataDHT22_temperature_minchange=0.2 -D dataDHT22_temperature_minchange=0.2
-D dataDHT22_humidity_minchange=1.0 -D dataDHT22_humidity_minchange=1.0
-D SENSOR_PIR -D SENSOR_HCSR501
-D PIRPIN=D0 -D SENSOR_HCSR501_PIN=D0
-D dataPIR_readdelay=100
-D dataPIR_senddelaymax=1000*60*10
-D SENSOR_BH1750 -D SENSOR_BH1750
-D dataBH1750_minchange=10.0 -D dataBH1750_minchange=10.0
@ -258,10 +238,8 @@ build_flags =
-D dataDHT22_temperature_minchange=0.2 -D dataDHT22_temperature_minchange=0.2
-D dataDHT22_humidity_minchange=1.0 -D dataDHT22_humidity_minchange=1.0
-D SENSOR_PIR -D SENSOR_HCSR501
-D PIRPIN=D6 -D SENSOR_HCSR501_PIN=D6
-D dataPIR_readdelay=100
-D dataPIR_senddelaymax=1000*60*10
-D SENSOR_RADAR -D SENSOR_RADAR
-D RADARPIN=D5 -D RADARPIN=D5

View File

@ -134,7 +134,6 @@
#endif #endif
#ifdef SENSOR_ML8511 #ifdef SENSOR_ML8511
//ML8511 UV Sensor outputs an analog voltage. ML8511PIN needs to be an ADC pin
#include "sensor_ml8511.cpp" #include "sensor_ml8511.cpp"
Sensor_ML8511 sensor_ml8511(SENSOR_ML8511_PIN); Sensor_ML8511 sensor_ml8511(SENSOR_ML8511_PIN);
@ -150,15 +149,16 @@
#endif #endif
#ifdef SENSOR_PIR #ifdef SENSOR_HCSR501
// PIR Sensors HC-SR501 #include "sensor_hcsr501.cpp"
// pir sensor needs 5v through an inductor for filtering. output level is 3.3v Sensor_HCSR501 sensor_hcsr501(SENSOR_HCSR501_PIN);
// 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 #ifndef SENSOR_HCSR501_senddelaymax
// poti closer to jumper is sensitivity (cw increases). other poti is pulse time (cw increases). #define SENSOR_HCSR501_senddelaymax 1000*60*10
// time set to output around 30s pulse #endif
sensordata dataPIR; #ifndef SENSOR_HCSR501_readdelayML8511
bool value_PIR=false; #define SENSOR_HCSR501_readdelay 100
#endif
#endif #endif
#ifdef SENSOR_RADAR #ifdef SENSOR_RADAR
@ -390,15 +390,9 @@ void setup() {
#endif #endif
#ifdef SENSOR_PIR #ifdef SENSOR_HCSR501
Serial.println("initializing pir"); sensor_hcsr501.init();
pinMode(PIRPIN, INPUT_PULLUP); sensor_hcsr501.setSettings(SENSOR_HCSR501_senddelaymax,SENSOR_HCSR501_readdelay);
#ifdef dataPIR_readdelay
dataPIR.readdelay=dataPIR_readdelay;
#endif
#ifdef dataPIR_senddelaymax
dataPIR.senddelaymax=dataPIR_senddelaymax;
#endif
#endif #endif
#ifdef SENSOR_RADAR #ifdef SENSOR_RADAR
@ -567,15 +561,17 @@ void setup() {
sensor_ml8511.advertise(sensorNode); sensor_ml8511.advertise(sensorNode);
#endif #endif
#ifdef SENSOR_HCSR501
sensor_hcsr501.advertise(sensorNode);
#endif
#ifdef SENSOR_LDR #ifdef SENSOR_LDR
sensorNode.advertise("light"); sensorNode.advertise("light");
analogRead(LDR_PIN); //first reading could be false analogRead(LDR_PIN); //first reading could be false
#endif #endif
#ifdef SENSOR_PIR
sensorNode.advertise("motion");
#endif
#ifdef SENSOR_RADAR #ifdef SENSOR_RADAR
sensorNode.advertise("radar"); sensorNode.advertise("radar");
@ -660,35 +656,6 @@ void loop_LDR()
} }
#endif #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 #ifdef SENSOR_RADAR
void loop_RADAR() void loop_RADAR()
@ -1098,13 +1065,14 @@ void loopHandler() {
sensor_ml8511.sensorloop(); sensor_ml8511.sensorloop();
#endif #endif
#ifdef SENSOR_HCSR501
sensor_hcsr501.sensorloop();
#endif
#ifdef SENSOR_LDR #ifdef SENSOR_LDR
loop_LDR(); loop_LDR();
#endif #endif
#ifdef SENSOR_PIR
loop_PIR();
#endif
#ifdef SENSOR_RADAR #ifdef SENSOR_RADAR
loop_RADAR(); loop_RADAR();