Compare commits

...

3 Commits

9 changed files with 109 additions and 21 deletions

View File

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

View File

@ -24,6 +24,10 @@
// 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.
#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_PASS "..."
@ -39,4 +43,7 @@
#define MQTT_TOPIC_STATE_LONG "espleaf/state_long"
#define MQTT_TOPIC_COMMANDS "esplead/cmnd"
#define SYSLOG_HOST "..."
#define SYSLOG_PORT 514
//#define TEST_MODE

8
include/syslog.h Normal file
View File

@ -0,0 +1,8 @@
#pragma once
#include <Arduino.h>
#include <WiFiUdp.h>
#ifdef SYSLOG_HOST
void syslog(String msg, uint8_t severity=7);
#endif

View File

@ -1,8 +1,15 @@
#pragma once
#include "my_fastled.h"
#include "syslog.h"
#define LOG(...) Serial.printf(__VA_ARGS__)
#define LOGln(...) Serial.printf(__VA_ARGS__); Serial.println()
//#define LOG(...) Serial.printf(__VA_ARGS__)
//#define LOGln(...) Serial.printf(__VA_ARGS__); Serial.println()
#ifdef SYSLOG_HOST
#define LOGln(...) { char buffer[512]; snprintf(buffer, 512, __VA_ARGS__); syslog(buffer); Serial.println(buffer);}
#else
#define LOGln(...) Serial.printf(__VA_ARGS__); Serial.println()
#endif
void clear_leds();

View File

@ -39,6 +39,7 @@ void Corner::infect(uint16_t infect_short_level, uint16_t infect_long_level) {
}
bool Corner::reached_level(uint16_t level) {
if (color_blend_state >= 1024) return false;
uint16_t old_cbs = color_blend_state >= effect_speed ? color_blend_state - effect_speed : 0;
return (old_cbs < level && color_blend_state >= level);
}

View File

@ -10,6 +10,7 @@
#include "prototypes.h"
#include "mqtt.h"
#include "state.h"
#include "syslog.h"
std::vector<Node*> nodes;
std::list<Edge*> edges;
@ -131,15 +132,31 @@ void show_all() {
}
void show_status(uint8_t status, CRGB color=CRGB::Green) {
for (int i=0; i<status; i++) {
if (i<corners.size()) {
corners[i]->set_color(color);
for (int i=0; i<status*LEDS_PER_CORNER && i<LED_COUNT; i++) {
leds[i] = color;
}
for (int i=status*LEDS_PER_CORNER; i<LED_COUNT; i++) {
leds[i] = CRGB::Black;
}
FastLED.show();
}
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();
delay(1500);
for(Corner* c : corners) {
c->set_color(CRGB::Black);
}
}
for (int i=status; i<corners.size(); i++) {
corners[i]->set_color(CRGB::Black);
}
show_all();
}
void setup() {
@ -150,16 +167,16 @@ void setup() {
setup_fastled();
show_status(1);
setup_layout();
show_status(2);
#ifdef TEST_MODE
LOGln("TEST_MODE is active!");
#else
show_status(2);
wifi_setup();
show_status(3);
setup_layout();
show_status(4);
mqtt_setup();
ArduinoOTA.setHostname(OTA_HOSTNAME);
ArduinoOTA.onProgress([&](unsigned int progress, unsigned int total){
@ -169,10 +186,33 @@ void setup() {
ArduinoOTA.onEnd([](){ show_status(0); });
ArduinoOTA.begin();
#ifdef WAIT_FOR_OTA
show_status(5);
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);
delay(250);
show_status(0);
#endif
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
for(Corner* corner : corners) {
corner->set_color(CRGB::Black);
}
}
void loop() {

View File

@ -5,15 +5,15 @@
void connect() {
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.");
mqtt.publish(MQTT_TOPIC "available", "online", true);
char buffer[40];
snprintf(buffer, 40, "online %s", wifi.localIP().toString().c_str());
mqtt.publish(MQTT_TOPIC "available_long", buffer, true);
mqtt.subscribe(MQTT_TOPIC"cmnd");
String discovery_msg = "{\""
"\"~\":\"" MQTT_TOPIC ",\"avty_t\":\"~available\",\"cmd_t\":\"~cmnd\",\"stat_t\":\"~state\","
String discovery_msg = "{"
"\"~\":\"" MQTT_TOPIC "\",\"avty_t\":\"~available\",\"cmd_t\":\"~cmnd\",\"stat_t\":\"~state\","
"\"name\":\"ESPleaf\",\"schema\":\"json\","
"\"brightness\":true,\"bri_scl\":255,"
"\"effect\":true,\"effect_list\":[\"off\",\"corners\",\"nodes\",\"flash\",\"static\"],"
@ -62,6 +62,7 @@ void mqtt_setup() {
mqtt.setServer(MQTT_SERVER, MQTT_SERVER_PORT);
mqtt.setCallback(callback);
mqtt.setSocketTimeout(1);
mqtt.setBufferSize(400);
connect();
}

21
src/syslog.cpp Normal file
View File

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

View File

@ -2,13 +2,14 @@
#include "tools.h"
void wifi_setup() {
LOG("Connecting to WiFi %s...", WIFI_SSID);
Serial.printf("Connecting to WiFi %s...", WIFI_SSID);
WiFi.mode(WIFI_STA);
WiFi.begin(WIFI_SSID, WIFI_PASS);
while (WiFi.status() != WL_CONNECTED) {
LOG(".");
Serial.print(".");
delay(300);
}
LOGln(" Connected as %s", WiFi.localIP().toString().c_str());
Serial.println();
LOGln("Connected as %s", WiFi.localIP().toString().c_str());
random16_add_entropy(micros());
}