Blinky lights support

master
informatic 2017-09-29 19:34:23 +02:00
parent 3c790d2c29
commit 904253dca4
4 changed files with 54 additions and 0 deletions

View File

@ -30,6 +30,8 @@ void SpejsNode::init() {
keepaliveTimer.initializeMs(10000, TimerDelegate(&SpejsNode::keepAliveHandler, this)).start(); keepaliveTimer.initializeMs(10000, TimerDelegate(&SpejsNode::keepAliveHandler, this)).start();
loadJSON(); loadJSON();
statusLED.high();
} }
//extern int __papiez_pedofil; //extern int __papiez_pedofil;
@ -52,18 +54,21 @@ void SpejsNode::loadJSON() {
void SpejsNode::keepAliveHandler() { void SpejsNode::keepAliveHandler() {
static int failureCounter = 0; static int failureCounter = 0;
if(!WifiStation.isConnected()) { if(!WifiStation.isConnected()) {
statusLED.high();
Serial.println("keepalive: Network reconnect"); Serial.println("keepalive: Network reconnect");
if(failureCounter++ < 5) if(failureCounter++ < 5)
WifiStation.connect(); WifiStation.connect();
else else
System.restart(); System.restart();
} else if(mqtt.getConnectionState() != eTCS_Connected) { } else if(mqtt.getConnectionState() != eTCS_Connected) {
statusLED.high();
Serial.println("keepalive: MQTT reconnect"); Serial.println("keepalive: MQTT reconnect");
if(failureCounter++ < 5) if(failureCounter++ < 5)
onConnected(); onConnected();
else else
System.restart(); System.restart();
} else { } else {
statusLED.idle();
failureCounter = 0; failureCounter = 0;
uint8_t mode; uint8_t mode;
@ -94,6 +99,8 @@ void SpejsNode::gotIP(IPAddress ip, IPAddress netmask, IPAddress gateway) {
} }
void SpejsNode::onConnected() { void SpejsNode::onConnected() {
statusLED.idle();
Serial.println("Connection successful"); Serial.println("Connection successful");
// MQTT initialization // MQTT initialization

View File

@ -16,6 +16,42 @@
#define D7 13 #define D7 13
#define D8 15 #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 { class SpejsNode {
protected: protected:
Timer keepaliveTimer; Timer keepaliveTimer;
@ -39,6 +75,8 @@ public:
String deviceID; String deviceID;
String deviceType; String deviceType;
LED statusLED;
uint8_t currentSlot; uint8_t currentSlot;
SpejsNode(String _deviceType) : SpejsNode(String _deviceType) :

View File

@ -45,9 +45,13 @@ void ImplementationEndpoint::startOTA() {
otaUpdater->start(); otaUpdater->start();
notify("ota", "started"); notify("ota", "started");
parent->statusLED.high();
} }
void ImplementationEndpoint::otaUpdateCallback(rBootHttpUpdate& updater, bool result) { void ImplementationEndpoint::otaUpdateCallback(rBootHttpUpdate& updater, bool result) {
parent->statusLED.idle();
if(result == true) { if(result == true) {
// success // success
notify("ota", "finished"); notify("ota", "finished");

View File

@ -1,4 +1,5 @@
#include <endpoints/OutputEndpoint.h> #include <endpoints/OutputEndpoint.h>
#include <SpejsNode.h>
EndpointResult OutputEndpoint::onValue(String property, String value) { EndpointResult OutputEndpoint::onValue(String property, String value) {
if (value == "1" or value == "on" or value == "true") { if (value == "1" or value == "on" or value == "true") {
@ -9,6 +10,10 @@ EndpointResult OutputEndpoint::onValue(String property, String value) {
return 400; return 400;
} }
if (currentValue != (parent->statusLED.blinkOn > parent->statusLED.blinkRate/2)) {
parent->statusLED.blinkOn = parent->statusLED.blinkRate - parent->statusLED.blinkOn;
}
digitalWrite(pin, inverted ^ currentValue); digitalWrite(pin, inverted ^ currentValue);
notify("on", currentValue ? "true" : "false"); notify("on", currentValue ? "true" : "false");
return 200; return 200;