From f76b610021d08a01ff9eda09b0fdb85d206af436 Mon Sep 17 00:00:00 2001 From: starcalc Date: Sat, 18 Nov 2017 20:43:30 +0100 Subject: [PATCH] =?UTF-8?q?PIR-Sensor=20am=20Pin=20D0=20hinzugef=C3=BCgt.?= =?UTF-8?q?=20ACHTUNG:=20UNTESTED!?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- esp-deckenlicht.ino | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/esp-deckenlicht.ino b/esp-deckenlicht.ino index 6517c42..b46cc0f 100644 --- a/esp-deckenlicht.ino +++ b/esp-deckenlicht.ino @@ -5,6 +5,7 @@ #define PIN_LIGHT1 D6 #define PIN_LIGHT2 D7 #define PIN_LIGHT3 D8 +#define PIN_SENSOR D0 #define FULL 255 #define LOWER 50 @@ -22,7 +23,11 @@ int step = 0; bool disco = false; int lastEvent = 0; +bool lastSensorValue = false; + HomieNode lightNode("strip", "strip"); +HomieNode sensorNode("sensor", "sensor"); +Bounce debouncer = Bounce(); bool speedHandler(const HomieRange& range, const String& value) { Homie.getLogger() << "speed " << ": " << value << endl; @@ -145,6 +150,12 @@ void loopHandler() step++; } } + bool sensorValue = debouncer.read(); + if (Homie.isConfigured() && Homie.isConnected() && sensorValue != lastSensorValue) { + sensorNode.setProperty("motion").send(sensorValue ? "true" : "false"); + lastSensorValue = sensorValue; + } + } void setup() { @@ -155,6 +166,10 @@ void setup() { pinMode(PIN_LIGHT1, OUTPUT); pinMode(PIN_LIGHT2, OUTPUT); pinMode(PIN_LIGHT3, OUTPUT); + + debouncer.attach(SENSOR_PIN,INPUT); + debouncer.interval(50); + Homie_setFirmware(FW_NAME, FW_VERSION); Homie_setBrand(FW_NAME); @@ -167,6 +182,8 @@ void setup() { lightNode.advertise("light1").settable(light1Handler); lightNode.advertise("light2").settable(light2Handler); lightNode.advertise("light3").settable(light3Handler); + + sensorNode.advertise("motion"); Homie.setup(); @@ -176,6 +193,7 @@ void setup() { void loop() { Homie.loop(); + debouncer.update(); ArduinoOTA.handle(); }