diff --git a/spejsiot/SpejsNode.cpp b/spejsiot/SpejsNode.cpp index 8e7dbf4..0b71c8f 100644 --- a/spejsiot/SpejsNode.cpp +++ b/spejsiot/SpejsNode.cpp @@ -30,6 +30,8 @@ void SpejsNode::init() { keepaliveTimer.initializeMs(10000, TimerDelegate(&SpejsNode::keepAliveHandler, this)).start(); loadJSON(); + + statusLED.high(); } //extern int __papiez_pedofil; @@ -52,18 +54,21 @@ void SpejsNode::loadJSON() { void SpejsNode::keepAliveHandler() { static int failureCounter = 0; if(!WifiStation.isConnected()) { + statusLED.high(); Serial.println("keepalive: Network reconnect"); if(failureCounter++ < 5) WifiStation.connect(); else System.restart(); } else if(mqtt.getConnectionState() != eTCS_Connected) { + statusLED.high(); Serial.println("keepalive: MQTT reconnect"); if(failureCounter++ < 5) onConnected(); else System.restart(); } else { + statusLED.idle(); failureCounter = 0; uint8_t mode; @@ -94,6 +99,8 @@ void SpejsNode::gotIP(IPAddress ip, IPAddress netmask, IPAddress gateway) { } void SpejsNode::onConnected() { + statusLED.idle(); + Serial.println("Connection successful"); // MQTT initialization diff --git a/spejsiot/SpejsNode.h b/spejsiot/SpejsNode.h index 51d7c82..6e19969 100644 --- a/spejsiot/SpejsNode.h +++ b/spejsiot/SpejsNode.h @@ -16,6 +16,42 @@ #define D7 13 #define D8 15 +class LED { + int highState = HIGH; + int pin = 2; + Timer animateTimer; + + void animate() { + if (millis() % blinkRate > blinkOn) { + digitalWrite(pin, !highState); + } else { + digitalWrite(pin, highState); + } + } + +public: + int blinkRate = 4000; + int blinkOn = 100; + + LED() { } + + void config(int pin_, bool highState_ = HIGH) { + pin = pin_; + highState = highState_; + + pinMode(pin, OUTPUT); + animateTimer.initializeMs(50, TimerDelegate(&LED::animate, this)).start(); + } + + void idle() { + blinkRate = 4000; + } + + void high() { + blinkRate = 300; + } +}; + class SpejsNode { protected: Timer keepaliveTimer; @@ -39,6 +75,8 @@ public: String deviceID; String deviceType; + LED statusLED; + uint8_t currentSlot; SpejsNode(String _deviceType) : diff --git a/spejsiot/endpoints/ImplementationEndpoint.cpp b/spejsiot/endpoints/ImplementationEndpoint.cpp index 613c278..8f8aa07 100644 --- a/spejsiot/endpoints/ImplementationEndpoint.cpp +++ b/spejsiot/endpoints/ImplementationEndpoint.cpp @@ -45,9 +45,13 @@ void ImplementationEndpoint::startOTA() { otaUpdater->start(); notify("ota", "started"); + + parent->statusLED.high(); } void ImplementationEndpoint::otaUpdateCallback(rBootHttpUpdate& updater, bool result) { + parent->statusLED.idle(); + if(result == true) { // success notify("ota", "finished"); diff --git a/spejsiot/endpoints/OutputEndpoint.cpp b/spejsiot/endpoints/OutputEndpoint.cpp index 1c9e26a..72fdcec 100644 --- a/spejsiot/endpoints/OutputEndpoint.cpp +++ b/spejsiot/endpoints/OutputEndpoint.cpp @@ -1,4 +1,5 @@ #include +#include EndpointResult OutputEndpoint::onValue(String property, String value) { if (value == "1" or value == "on" or value == "true") { @@ -9,6 +10,10 @@ EndpointResult OutputEndpoint::onValue(String property, String value) { return 400; } + if (currentValue != (parent->statusLED.blinkOn > parent->statusLED.blinkRate/2)) { + parent->statusLED.blinkOn = parent->statusLED.blinkRate - parent->statusLED.blinkOn; + } + digitalWrite(pin, inverted ^ currentValue); notify("on", currentValue ? "true" : "false"); return 200;