63 lines
1.6 KiB
C++
63 lines
1.6 KiB
C++
|
// 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();
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
|