Browse Source

add radar motion sensor

master
interfisch 7 months ago
parent
commit
f786cbd77c
  1. 5
      platformio.ini
  2. 56
      src/main.cpp

5
platformio.ini

@ -227,6 +227,11 @@ build_flags =
-D dataPIR_readdelay=100
-D dataPIR_senddelaymax=1000*60*10
-D SENSOR_RADAR
-D RADARPIN=D5
-D dataRADAR_readdelay=100
-D dataRADAR_senddelaymax=1000*60*10
-D SENSOR_BH1750
-D dataBH1750_minchange=10.0
-D dataBH1750_senddelaymax=1000*60*2

56
src/main.cpp

@ -94,6 +94,13 @@ struct sensordata
bool value_PIR=false;
#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;
#endif
#ifdef SENSOR_LDR
struct sensordata dataLDR;
@ -334,6 +341,17 @@ void setup() {
#endif
#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
#endif
#ifdef SENSOR_LDR
Serial.println("initializing ldr");
pinMode(LDR_PIN, INPUT); //ldr
@ -480,6 +498,10 @@ void setup() {
sensorNode.advertise("motion");
#endif
#ifdef SENSOR_RADAR
sensorNode.advertise("radar");
#endif
#ifdef SENSOR_BMP180
sensorNode.advertise("temperature");
sensorNode.advertise("pressure");
@ -799,6 +821,36 @@ void loop_PIR()
}
#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()
{
@ -1126,6 +1178,10 @@ void loopHandler() {
loop_PIR();
#endif
#ifdef SENSOR_RADAR
loop_RADAR();
#endif
#ifdef SENSOR_MHZ19
loop_MHZ19();
#endif

Loading…
Cancel
Save