initial commit
commit
0b95804018
|
|
@ -0,0 +1,55 @@
|
||||||
|
#include <Homie.h>
|
||||||
|
|
||||||
|
const byte TRIGGER_PIN = 3; //pin 3 which is RX pin. Doesn't seem to work on GPIO0 or GPIO2, maybe because they have internal pull up resistors?
|
||||||
|
const unsigned int TRIGGER_HOLD_TIME = 5000; // 5 seconds
|
||||||
|
unsigned long triggerTime = 0;
|
||||||
|
enum trigger_status {start, stop};
|
||||||
|
|
||||||
|
trigger_status triggerStatus = stop;
|
||||||
|
|
||||||
|
HomieNode motionNode("motion", "Motion", "motion");
|
||||||
|
|
||||||
|
void loopHandler(){
|
||||||
|
|
||||||
|
if (digitalRead(TRIGGER_PIN) == HIGH) { // if triggered
|
||||||
|
triggerTime = millis(); // set trigger timer to current time
|
||||||
|
|
||||||
|
if (triggerStatus == stop) { // if trigger status == stop
|
||||||
|
motionNode.setProperty("motion").send("true"); // send mqtt "motion started"
|
||||||
|
triggerStatus = start; // set trigger status to start
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
else { // if not triggered
|
||||||
|
if (triggerStatus == start) { // if trigger status == start
|
||||||
|
if (millis() - triggerTime >= TRIGGER_HOLD_TIME) { // if difference between 'current time' and 'trigger timer' >= 'trigger hold time'
|
||||||
|
motionNode.setProperty("motion").send("false"); // send mqtt "motion stopped"
|
||||||
|
triggerStatus = stop; // set trigger status to "stop"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} //end else
|
||||||
|
} // loopHandler()
|
||||||
|
|
||||||
|
|
||||||
|
void setup() {
|
||||||
|
//Serial.begin(115200);
|
||||||
|
Homie_setFirmware("homie-motion", "1.0.0");
|
||||||
|
Homie.setLoopFunction(loopHandler);
|
||||||
|
|
||||||
|
pinMode(TRIGGER_PIN, INPUT);
|
||||||
|
triggerStatus = stop;
|
||||||
|
|
||||||
|
motionNode.advertise("motion").setName("Motion Detected")
|
||||||
|
.setDatatype("boolean")
|
||||||
|
.setRetained(false);
|
||||||
|
Homie.setup();
|
||||||
|
|
||||||
|
motionNode.setProperty("motion").send("false");
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// the loop routine runs over and over again forever:
|
||||||
|
void loop() {
|
||||||
|
Homie.loop();
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue