Compare commits

...

2 Commits

Author SHA1 Message Date
interfisch bd25dc9b7c move radar to class 2021-11-04 19:25:31 +01:00
interfisch 01ee9cd525 move pir to class and rename to hcsr501 2021-11-04 19:16:30 +01:00
7 changed files with 253 additions and 135 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)
#include "sensor_ml8511.h"

62
include/sensor_radar.cpp Normal file
View File

@ -0,0 +1,62 @@
// High/Low Output Radar Sensor
// For example: RCWL-0516 (needs 5v input (gnd, vin), 3.3v output level. high for 2seconds when movement detected)
#include "sensor_radar.h"
Sensor_Radar::Sensor_Radar(int pin)
{
radarpin=pin;
sensordata data;
}
void Sensor_Radar::init() //Things to be done during setup()
{
Serial.println("initializing Radar");
pinMode(radarpin, INPUT_PULLUP);
init_ok=true;
}
//Also called during setup()
void Sensor_Radar::setSettings(unsigned long senddelaymax, unsigned long readdelay)
{
data.senddelaymax=senddelaymax;
data.readdelay=readdelay;
}
//Called during setup
void Sensor_Radar::advertise(HomieNode& p_sensorNode)
{
sensorNode = &p_sensorNode;
sensorNode->advertise("radar");
}
void Sensor_Radar::sensorloop()
{
if (init_ok) {
sensordata &d=data;
bool _changed=false;
if (millis() >= (d.lastreadtime+d.readdelay)) {
if (digitalRead(radarpin) != (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 radar. reason=");
if (_changed) Serial.println("change"); else Serial.println("time");
if (digitalRead(radarpin)){
Homie.getLogger() << "radar " << ": " << "true" << endl;
sensorNode->setProperty("radar").send(String("true"));
d.value=true;
}else{
Homie.getLogger() << "motion " << ": " << "false" << endl;
sensorNode->setProperty("radar").send(String("false"));
d.value=false;
}
d.lastsent=millis();
}
}
}

32
include/sensor_radar.h Normal file
View File

@ -0,0 +1,32 @@
#ifndef SENSOR_Radar_H
#define SENSOR_Radar_H
#include "sensordata.h"
#include <Homie.h>
class Sensor_Radar
{
private:
HomieNode *sensorNode; //reference to HomieNode
int radarpin;
struct sensordata data; //struct values are changed in setup()
bool init_ok;
public:
Sensor_Radar(int pin);
void init();
void setSettings(unsigned long senddelaymax, unsigned long readdelay);
void advertise(HomieNode& p_sensorNode);
void sensorloop();
};
#endif

View File

@ -82,27 +82,19 @@ 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
-D SENSOR_RADAR
-D SENSOR_RADAR_PIN=D5
-D SENSOR_RADAR_readdelay=100
-D SENSOR_RADAR_senddelaymax=1000*60*10
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 +115,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 +177,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 +206,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,15 +244,13 @@ 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
-D dataRADAR_readdelay=100
-D dataRADAR_senddelaymax=1000*60*10
-D SENSOR_RADAR_PIN=D5
-D SENSOR_RADAR_readdelay=100
-D SENSOR_RADAR_senddelaymax=1000*60*10
-D SENSOR_BH1750
-D dataBH1750_minchange=10.0

View File

@ -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,22 +149,28 @@
#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
// High/Low Output Radar Sensor
// For example: RCWL-0516 (needs 5v input (gnd, vin), 3.3v output level. high for 2seconds when movement detected)
sensordata dataRADAR;
bool value_RADAR=false;
#include "sensor_radar.cpp"
Sensor_Radar sensor_radar(SENSOR_RADAR_PIN);
#ifndef SENSOR_RADAR_senddelaymax
#define SENSOR_RADAR_senddelaymax 1000*60*10
#endif
#ifndef SENSOR_RADAR_readdelayML8511
#define SENSOR_RADAR_readdelay 100
#endif
#endif
@ -390,26 +395,14 @@ 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
Serial.println("initializing radar");
pinMode(RADARPIN, INPUT);
#ifdef dataRADAR_readdelay
dataRADAR.readdelay=dataRADAR_readdelay;
#endif
#ifdef dataRADAR_senddelaymax
dataRADAR.senddelaymax=dataRADAR_senddelaymax;
#endif
sensor_radar.init();
sensor_radar.setSettings(SENSOR_RADAR_senddelaymax,SENSOR_RADAR_readdelay);
#endif
#ifdef SENSOR_LDR
@ -567,19 +560,22 @@ void setup() {
sensor_ml8511.advertise(sensorNode);
#endif
#ifdef SENSOR_HCSR501
sensor_hcsr501.advertise(sensorNode);
#endif
#ifdef SENSOR_RADAR
sensor_radar.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");
#endif
@ -660,64 +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()
{
sensordata &d=dataRADAR;
bool _changed=false;
if (millis() >= (d.lastreadtime+d.readdelay)) {
if (digitalRead(RADARPIN) != value_RADAR){
_changed=true;
}
d.lastreadtime=millis();
}
if (_changed || millis() >= (d.lastsent+d.senddelaymax)) { //send current value after some long time
Serial.print("Sending motion radar. reason=");
if (_changed) Serial.println("change"); else Serial.println("time");
if (digitalRead(RADARPIN)){
Homie.getLogger() << "radar " << ": " << "true" << endl;
sensorNode.setProperty("radar").send(String("true"));
value_RADAR=true;
}else{
Homie.getLogger() << "motion " << ": " << "false" << endl;
sensorNode.setProperty("radar").send(String("false"));
value_RADAR=false;
}
d.lastsent=millis();
}
}
#endif
#ifdef SENSOR_MHZ19
void loop_MHZ19()
@ -1098,17 +1036,20 @@ void loopHandler() {
sensor_ml8511.sensorloop();
#endif
#ifdef SENSOR_HCSR501
sensor_hcsr501.sensorloop();
#endif
#ifdef SENSOR_RADAR
sensor_radar.sensorloop();
#endif
#ifdef SENSOR_LDR
loop_LDR();
#endif
#ifdef SENSOR_PIR
loop_PIR();
#endif
#ifdef SENSOR_RADAR
loop_RADAR();
#endif
#ifdef SENSOR_MHZ19
loop_MHZ19();