From 0a4eda713d3dc0511d12e26397d51c58a1fba83a Mon Sep 17 00:00:00 2001 From: Juergen Jung Date: Fri, 17 Feb 2017 03:01:52 +0100 Subject: [PATCH] Input Sensor for motion --- esp-wemos-schild/esp-wemos-schild.ino | 24 ++++++++++++++++++++---- platformio.ini | 2 +- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/esp-wemos-schild/esp-wemos-schild.ino b/esp-wemos-schild/esp-wemos-schild.ino index e1ba9e7..598af35 100644 --- a/esp-wemos-schild/esp-wemos-schild.ino +++ b/esp-wemos-schild/esp-wemos-schild.ino @@ -6,12 +6,12 @@ #include "NeoPatterns.h" #define PIN D1 +#define SENSOR D3 #define NUMPIXELS 144 -byte wPos = 0; -uint8_t state = 0; +bool lastSensorValue = false; void StripComplete(){ return; @@ -20,6 +20,8 @@ void StripComplete(){ NeoPatterns pixels = NeoPatterns(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800,&StripComplete); HomieNode stripNode("strip", "strip"); +HomieNode sensorNode("sensor", "sensor"); +Bounce debouncer = Bounce(); bool onSetPixel(const HomieRange& range, const String& value){ if(!range.isRange) { @@ -60,12 +62,11 @@ bool onSetEffect(const HomieRange& range, const String& value){ pixels.TheaterChase(pixels.Color(0,0,255), pixels.Color(255,0,00), 100); } else if(effect == "fade") { - pixels.Fade(pixels.Wheel(255),pixels.Wheel(0),255,25); + pixels.Fade(pixels.Wheel(0),pixels.Wheel(255),pixels.Color(255,255,255),25); } else { pixels.None(); } - stripNode.setProperty("effect").send(value); } @@ -89,19 +90,33 @@ bool onSetLength(const HomieRange& range, const String& value){ void loopHandler() { pixels.Update(); + + int sensorValue = debouncer.read(); + if (Homie.isConfigured() && Homie.isConnected() && sensorValue != lastSensorValue) { + sensorNode.setProperty("motion").send(sensorValue ? "true" : "false"); + lastSensorValue = sensorValue; + } + } void setup() { Serial.begin(115200); + + debouncer.attach(SENSOR,INPUT); + debouncer.interval(50); + Homie_setFirmware("schild", "1.0.0"); Homie.setLoopFunction(loopHandler); + stripNode.advertiseRange("pixel", 0, NUMPIXELS-1).settable(onSetPixel); stripNode.advertise("brightness").settable(onSetBrightness); stripNode.advertise("effect").settable(onSetEffect); stripNode.advertise("clear").settable(onSetClear); stripNode.advertise("length").settable(onSetLength); + sensorNode.advertise("motion"); + pixels.begin(); pixels.clear(); pixels.setBrightness(64); @@ -116,5 +131,6 @@ void setup() { void loop() { Homie.loop(); + debouncer.update(); ArduinoOTA.handle(); } diff --git a/platformio.ini b/platformio.ini index 2a6fa9b..77d00ab 100644 --- a/platformio.ini +++ b/platformio.ini @@ -13,7 +13,7 @@ platform=espressif8266 board=d1_mini framework=arduino -;upload_port = /dev/tty.wchusbserial640 +;upload_port = /dev/tty.wchusbserial410 ;upload_speed = 921600 ;upload_port = 5ccf7f1db369.local