commit feb8a5e2be1fe911470ee6acaae15a5c909a7b29 Author: Philipp Date: Sun Jun 4 15:35:25 2017 +0200 Initial commit. Full resolution encoder and button to mqtt diff --git a/esp-volumeknob/esp-volumeknob.ino b/esp-volumeknob/esp-volumeknob.ino new file mode 100644 index 0000000..e448349 --- /dev/null +++ b/esp-volumeknob/esp-volumeknob.ino @@ -0,0 +1,81 @@ +//curl -X PUT http://192.168.123.1/config --header "Content-Type: application/json" -d '{"name":"volumeknob3", "device_id":"volumeknob3","wifi":{"ssid":"CTDO-IoT","password":"xxx"},"mqtt":{"host":"raum.ctdo.de","port":1883,"ssl":false,"auth":false},"ota":{"enabled":false}}' + + +#include +#include +#include + +//D4 auf ground macht led leuchten + +#define PIN_ENCA D6 +#define PIN_ENCB D5 +#define PIN_BTN D2 + +#define POSITIONUPDATE 100 + +Bounce debouncerKnobBTN = Bounce(); +int lastKnobBTN = -1; + + +Encoder myEnc(PIN_ENCA, PIN_ENCB); +long lastPositionCheck=0; + +long oldPosition = 0; + + +HomieNode knobNode("volumeknob", "Volumeknob"); + + +void loopHandler() { + + int valueKnobBTN = debouncerKnobBTN.read(); + if (valueKnobBTN != lastKnobBTN) { + Homie.getLogger() << "KnobBTN is now " << (valueKnobBTN ? "open" : "close") << endl; + knobNode.setProperty("btnknob").send(valueKnobBTN ? "false" : "true"); + lastKnobBTN = valueKnobBTN; + } + + + long newPosition = myEnc.read()/4; + if (lastPositionCheck+POSITIONUPDATE < millis() && newPosition != oldPosition) { + long positiondiff=newPosition-oldPosition; + oldPosition = newPosition; + Homie.getLogger() << "posdiff= " << positiondiff << endl; + knobNode.setProperty("encoder").send(String(positiondiff)); + lastPositionCheck=millis(); + } + + + + + ArduinoOTA.handle(); + +} + + +void setup() { + Serial.begin(115200); + Serial << endl << endl; + + pinMode(PIN_BTN, INPUT_PULLUP); + debouncerKnobBTN.attach(PIN_BTN); + debouncerKnobBTN.interval(50); + + + + Homie_setFirmware("volumeknob", "0.1.0"); + Homie.setResetTrigger(PIN_BTN, LOW, 20000); // BTN0 = Flash = PIN_BTN set to 20sec + Homie.setLoopFunction(loopHandler); + + knobNode.advertise("encoder"); + knobNode.advertise("btnknob"); + + Homie.setup(); + ArduinoOTA.setHostname(Homie.getConfiguration().deviceId); + ArduinoOTA.begin(); +} + +void loop() { + Homie.loop(); + debouncerKnobBTN.update(); +}