move radar to class

This commit is contained in:
interfisch 2021-11-04 19:25:31 +01:00
parent 01ee9cd525
commit bd25dc9b7c
4 changed files with 123 additions and 50 deletions

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

@ -86,6 +86,12 @@ build_flags =
-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
@ -242,9 +248,9 @@ build_flags =
-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

@ -162,10 +162,15 @@
#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
@ -396,14 +401,8 @@ void setup() {
#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
@ -566,6 +565,10 @@ void setup() {
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
@ -573,9 +576,6 @@ void setup() {
#ifdef SENSOR_RADAR
sensorNode.advertise("radar");
#endif
@ -657,35 +657,6 @@ void loop_LDR()
#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()
{
@ -1069,14 +1040,16 @@ void loopHandler() {
sensor_hcsr501.sensorloop();
#endif
#ifdef SENSOR_RADAR
sensor_radar.sensorloop();
#endif
#ifdef SENSOR_LDR
loop_LDR();
#endif
#ifdef SENSOR_RADAR
loop_RADAR();
#endif
#ifdef SENSOR_MHZ19
loop_MHZ19();