Added a delay at bootup to allow pushing an OTA update in case of broken, looping code.

This commit is contained in:
Fabian Schlenz 2021-01-16 18:15:17 +01:00
parent 11dbea0302
commit f4864dec05
5 changed files with 55 additions and 12 deletions

View File

@ -38,4 +38,5 @@ During startup:
* 1 green corner: FastLED is initialized. Layout is now being analyzed. * 1 green corner: FastLED is initialized. Layout is now being analyzed.
* 2 green corners: Layout is done. WiFi is being connected. * 2 green corners: Layout is done. WiFi is being connected.
* 3 green corners: WiFi connection established. Connecting to MQTT server. * 3 green corners: WiFi connection established. Connecting to MQTT server.
* 4 green corners (only if OTA_DELAY is set): Waiting for an OTA connection.
* Everything green (quarter of a second): Initialization done. * Everything green (quarter of a second): Initialization done.

View File

@ -24,6 +24,10 @@
// Maximum color difference for the random effects. // Maximum color difference for the random effects.
// This changes the hue value +/- this value. Use a maximum value of 127, otherwise strange things might happen. // This changes the hue value +/- this value. Use a maximum value of 127, otherwise strange things might happen.
#define COLOR_DIFFERENCE 25 #define COLOR_DIFFERENCE 25
// If this is enabled, the node layout will be visualized at startup.
#define SHOW_LAYOUT_AT_BOOT
// Wait this many seconds for OTA requests during startup.
#define WAIT_FOR_OTA 5
#define WIFI_SSID "..." #define WIFI_SSID "..."
#define WIFI_PASS "..." #define WIFI_PASS "..."

View File

@ -132,15 +132,31 @@ void show_all() {
} }
void show_status(uint8_t status, CRGB color=CRGB::Green) { void show_status(uint8_t status, CRGB color=CRGB::Green) {
for (int i=0; i<status; i++) { for (int i=0; i<status*LEDS_PER_CORNER && i<LED_COUNT; i++) {
if (i<corners.size()) { leds[i] = color;
corners[i]->set_color(color);
} }
for (int i=status*LEDS_PER_CORNER; i<LED_COUNT; i++) {
leds[i] = CRGB::Black;
} }
for (int i=status; i<corners.size(); i++) { FastLED.show();
corners[i]->set_color(CRGB::Black); }
void display_layout() {
for(Corner* corner : corners) {
corner->set_color(CRGB::Blue);
for(Corner* c : corner->_short_neighbours) {
c->set_color(CRGB::Red);
}
for(Corner* c : corner->_long_neighbours) {
c->set_color(CRGB::Green);
} }
show_all(); show_all();
delay(1500);
for(Corner* c : corners) {
c->set_color(CRGB::Black);
}
}
} }
void setup() { void setup() {
@ -170,12 +186,29 @@ void setup() {
ArduinoOTA.onEnd([](){ show_status(0); }); ArduinoOTA.onEnd([](){ show_status(0); });
ArduinoOTA.begin(); ArduinoOTA.begin();
#ifdef WAIT_FOR_OTA
show_status(4);
LOGln("Waiting %d seconds for OTA requests...", WAIT_FOR_OTA);
unsigned long ota_target_time = millis() + WAIT_FOR_OTA*1000;
while (millis() < ota_target_time) {
ArduinoOTA.handle();
yield();
}
LOGln("Done waiting for OTA requests.");
#endif
show_status(255); show_status(255);
delay(250); delay(250);
show_status(0); show_status(0);
#endif #endif
syslog("Setup done."); LOGln("Setup done.");
#ifdef SHOW_LAYOUT_AT_BOOT
LOGln("SHOW_LAYOUT_AT_BOOT is active - showing layout...");
display_layout();
LOGln("Showing layout is done.");
#endif
} }
void loop() { void loop() {

View File

@ -5,15 +5,15 @@
void connect() { void connect() {
LOGln("Connecting to MQTT broker..."); LOGln("Connecting to MQTT broker...");
if (mqtt.connect(MQTT_CLIENT_ID, MQTT_USER, MQTT_PASS, MQTT_TOPIC "state", 0, true, "OFFLINE")) { if (mqtt.connect(MQTT_CLIENT_ID, MQTT_USER, MQTT_PASS, MQTT_TOPIC "available", 0, true, "offline")) {
LOGln("Connected."); LOGln("Connected.");
mqtt.publish(MQTT_TOPIC "available", "online", true); mqtt.publish(MQTT_TOPIC "available", "online", true);
char buffer[40]; char buffer[40];
snprintf(buffer, 40, "online %s", wifi.localIP().toString().c_str()); snprintf(buffer, 40, "online %s", wifi.localIP().toString().c_str());
mqtt.publish(MQTT_TOPIC "available_long", buffer, true); mqtt.publish(MQTT_TOPIC "available_long", buffer, true);
mqtt.subscribe(MQTT_TOPIC"cmnd"); mqtt.subscribe(MQTT_TOPIC"cmnd");
String discovery_msg = "{\"" String discovery_msg = "{"
"\"~\":\"" MQTT_TOPIC ",\"avty_t\":\"~available\",\"cmd_t\":\"~cmnd\",\"stat_t\":\"~state\"," "\"~\":\"" MQTT_TOPIC "\",\"avty_t\":\"~available\",\"cmd_t\":\"~cmnd\",\"stat_t\":\"~state\","
"\"name\":\"ESPleaf\",\"schema\":\"json\"," "\"name\":\"ESPleaf\",\"schema\":\"json\","
"\"brightness\":true,\"bri_scl\":255," "\"brightness\":true,\"bri_scl\":255,"
"\"effect\":true,\"effect_list\":[\"off\",\"corners\",\"nodes\",\"flash\",\"static\"]," "\"effect\":true,\"effect_list\":[\"off\",\"corners\",\"nodes\",\"flash\",\"static\"],"
@ -62,6 +62,7 @@ void mqtt_setup() {
mqtt.setServer(MQTT_SERVER, MQTT_SERVER_PORT); mqtt.setServer(MQTT_SERVER, MQTT_SERVER_PORT);
mqtt.setCallback(callback); mqtt.setCallback(callback);
mqtt.setSocketTimeout(1); mqtt.setSocketTimeout(1);
mqtt.setBufferSize(400);
connect(); connect();
} }

View File

@ -2,6 +2,8 @@
#include "config.h" #include "config.h"
#ifdef SYSLOG_HOST #ifdef SYSLOG_HOST
static uint16_t syslog_msg_id = 1;
void syslog(String msg, uint8_t severity) { void syslog(String msg, uint8_t severity) {
uint8_t facility = 16; // local0 uint8_t facility = 16; // local0
if (severity > 7) severity = 7; if (severity > 7) severity = 7;
@ -10,7 +12,9 @@
udp.beginPacket(SYSLOG_HOST, SYSLOG_PORT); udp.beginPacket(SYSLOG_HOST, SYSLOG_PORT);
udp.write("<"); udp.write("<");
udp.write(String(facility * 8 + severity).c_str()); udp.write(String(facility * 8 + severity).c_str());
udp.write(">1 - " OTA_HOSTNAME " espleaf 1 1 - "); udp.write(">1 - " OTA_HOSTNAME " core 1 ");
udp.write(String(syslog_msg_id++).c_str());
udp.write(" - ");
udp.write(msg.c_str()); udp.write(msg.c_str());
udp.endPacket(); udp.endPacket();
} }