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();
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

View File

@ -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) :

View File

@ -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");

View File

@ -1,4 +1,5 @@
#include <endpoints/OutputEndpoint.h>
#include <SpejsNode.h>
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;