2021-11-04 18:16:30 +00:00
// PIR Sensors HC-SR501
// pir sensor needs 5v through an inductor for filtering. output level is 3.3v
2023-01-29 19:39:04 +00:00
// 100nF capacitor should 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
2021-11-04 18:16:30 +00:00
// 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).
2023-01-29 19:39:04 +00:00
// Set jumper to inner position (closer to IC)
2021-11-04 18:16:30 +00:00
// time set to output around 30s pulse
2023-01-29 19:39:04 +00:00
// pinout is Vin, Signal, GND. Vin pin is closest to diode
2021-11-04 18:16:30 +00:00
# include "sensor_hcsr501.h"
Sensor_HCSR501 : : Sensor_HCSR501 ( int pin )
{
hcsr501pin = pin ;
}
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 ( ) ;
}
}
}