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