Initial commit. Full resolution encoder and button to mqtt

This commit is contained in:
interfisch 2017-06-04 15:35:25 +02:00
commit feb8a5e2be
1 changed files with 81 additions and 0 deletions

View File

@ -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 <Homie.h>
#include <ArduinoOTA.h>
#include <Encoder.h>
//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();
}