From b699cf75a29da65bb4544a579ab83b4b099299e6 Mon Sep 17 00:00:00 2001 From: Tobias Blum Date: Sun, 6 Aug 2017 00:52:25 +0200 Subject: [PATCH 1/7] Initial First take on MQTT integration. --- Arduino/McLighting/McLighting.ino | 24 +++ Arduino/McLighting/definitions.h | 16 +- Arduino/McLighting/request_handlers.h | 292 +++++++++++++++++++------- 3 files changed, 250 insertions(+), 82 deletions(-) diff --git a/Arduino/McLighting/McLighting.ino b/Arduino/McLighting/McLighting.ino index 432e395..794f302 100644 --- a/Arduino/McLighting/McLighting.ino +++ b/Arduino/McLighting/McLighting.ino @@ -23,6 +23,14 @@ #include #endif +// MQTT +#ifdef ENABLE_MQTT + #include + + WiFiClient espClient; + PubSubClient mqtt_client(espClient); +#endif + // *************************************************************************** // Instanciate HTTP(80) / WebSockets(81) Server // *************************************************************************** @@ -195,6 +203,15 @@ void setup() { #endif + // *************************************************************************** + // Configure MQTT + // *************************************************************************** + #ifdef ENABLE_MQTT + mqtt_client.setServer(mqtt_server, 1883); + mqtt_client.setCallback(mqtt_callback); + #endif + + // *************************************************************************** // Setup: MDNS responder // *************************************************************************** @@ -412,6 +429,13 @@ void loop() { ArduinoOTA.handle(); #endif + #ifdef ENABLE_MQTT + if (!mqtt_client.connected()) { + mqtt_reconnect(); + } + mqtt_client.loop(); + #endif + // Simple statemachine that handles the different modes if (mode == SET_MODE) { DBG_OUTPUT_PORT.printf("SET_MODE: %d %d\n", ws2812fx_mode, mode); diff --git a/Arduino/McLighting/definitions.h b/Arduino/McLighting/definitions.h index 8481c9a..30ff7c1 100644 --- a/Arduino/McLighting/definitions.h +++ b/Arduino/McLighting/definitions.h @@ -1,12 +1,18 @@ // Neopixel #define PIN 5 // PIN where neopixel / WS2811 strip is attached -#define NUMLEDS 60 // Number of leds in the strip +#define NUMLEDS 24 // Number of leds in the strip -#define HOSTNAME "ESP8266_02" // Friedly hostname +#define HOSTNAME "ESP8266_VORONOI" // Friedly hostname -// #define ENABLE_OTA // If defined, enable Arduino OTA code. +#define ENABLE_OTA // If defined, enable Arduino OTA code. +#define ENABLE_MQTT // If defined, enable MQTT client code. +#ifdef ENABLE_MQTT + const char mqtt_intopic[] = "inTopic"; + const char mqtt_outtopic[] = "outTopic"; + const char mqtt_server[] = "raspberrypi2"; +#endif // *************************************************************************** // Global variables / definitions @@ -16,9 +22,9 @@ // List of all color modes enum MODE { SET_MODE, HOLD, OFF, ALL, WIPE, RAINBOW, RAINBOWCYCLE, THEATERCHASE, THEATERCHASERAINBOW, TV }; -MODE mode = RAINBOW; // Standard mode that is active when software starts +MODE mode = RAINBOW; // Standard mode that is active when software starts -int ws2812fx_speed = 10; // Global variable for storing the delay between color changes --> smaller == faster +int ws2812fx_speed = 10; // Global variable for storing the delay between color changes --> smaller == faster int brightness = 192; // Global variable for storing the brightness (255 == 100%) int ws2812fx_mode = 0; // Helper variable to set WS2812FX modes diff --git a/Arduino/McLighting/request_handlers.h b/Arduino/McLighting/request_handlers.h index 86a511f..58ef200 100644 --- a/Arduino/McLighting/request_handlers.h +++ b/Arduino/McLighting/request_handlers.h @@ -37,6 +37,101 @@ void getArgs() { DBG_OUTPUT_PORT.println(brightness); } + + + +void handleSetMainColor(uint8_t * payload) { + // decode rgb data + uint32_t rgb = (uint32_t) strtol((const char *) &payload[1], NULL, 16); + main_color.red = ((rgb >> 16) & 0xFF); + main_color.green = ((rgb >> 8) & 0xFF); + main_color.blue = ((rgb >> 0) & 0xFF); + strip.setColor(main_color.red, main_color.green, main_color.blue); +} + +void handleSetAllMode(uint8_t * payload) { + // decode rgb data + uint32_t rgb = (uint32_t) strtol((const char *) &payload[1], NULL, 16); + + main_color.red = ((rgb >> 16) & 0xFF); + main_color.green = ((rgb >> 8) & 0xFF); + main_color.blue = ((rgb >> 0) & 0xFF); + + for (int i = 0; i < strip.numPixels(); i++) { + strip.setPixelColor(i, main_color.red, main_color.green, main_color.blue); + } + strip.show(); + DBG_OUTPUT_PORT.printf("WS: Set all leds to main color: [%u] [%u] [%u]\n", main_color.red, main_color.green, main_color.blue); + exit_func = true; + mode = ALL; +} + +void handleSetSingleLED(uint8_t * payload) { + // decode led index + uint64_t rgb = (uint64_t) strtol((const char *) &payload[1], NULL, 16); + + uint8_t led = ((rgb >> 24) & 0xFF); + if (led < strip.numPixels()) { + ledstates[led].red = ((rgb >> 16) & 0xFF); + ledstates[led].green = ((rgb >> 8) & 0xFF); + ledstates[led].blue = ((rgb >> 0) & 0xFF); + DBG_OUTPUT_PORT.printf("WS: Set single led [%u] to [%u] [%u] [%u]!\n", led, ledstates[led].red, ledstates[led].green, ledstates[led].blue); + + for (uint8_t i = 0; i < strip.numPixels(); i++) { + strip.setPixelColor(i, ledstates[i].red, ledstates[i].green, ledstates[i].blue); + //DBG_OUTPUT_PORT.printf("[%u]--[%u] [%u] [%u] [%u] LED index!\n", rgb, i, ledstates[i].red, ledstates[i].green, ledstates[i].blue); + } + strip.show(); + } + exit_func = true; + mode = ALL; +} + +void handleSetNamedMode(String str_mode) { + exit_func = true; + + if (str_mode.startsWith("=off")) { + mode = OFF; + } + if (str_mode.startsWith("=all")) { + mode = ALL; + } + if (str_mode.startsWith("=wipe")) { + mode = WIPE; + } + if (str_mode.startsWith("=rainbow")) { + mode = RAINBOW; + } + if (str_mode.startsWith("=rainbowCycle")) { + mode = RAINBOWCYCLE; + } + if (str_mode.startsWith("=theaterchase")) { + mode = THEATERCHASE; + } + if (str_mode.startsWith("=theaterchaseRainbow")) { + mode = THEATERCHASERAINBOW; + } + if (str_mode.startsWith("=tv")) { + mode = TV; + } +} + +void handleSetWS2812FXMode(uint8_t * payload) { + mode = HOLD; + uint8_t ws2812fx_mode = (uint8_t) strtol((const char *) &payload[1], NULL, 10); + ws2812fx_mode = constrain(ws2812fx_mode, 0, 255); + strip.setColor(main_color.red, main_color.green, main_color.blue); + strip.setMode(ws2812fx_mode); +} + + + + + + + + + void handleMinimalUpload() { char temp[1500]; int sec = millis() / 1000; @@ -127,17 +222,12 @@ void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t lenght // # ==> Set main color if (payload[0] == '#') { - // decode rgb data - uint32_t rgb = (uint32_t) strtol((const char *) &payload[1], NULL, 16); - main_color.red = ((rgb >> 16) & 0xFF); - main_color.green = ((rgb >> 8) & 0xFF); - main_color.blue = ((rgb >> 0) & 0xFF); - strip.setColor(main_color.red, main_color.green, main_color.blue); + handleSetMainColor(payload); DBG_OUTPUT_PORT.printf("Set main color to: [%u] [%u] [%u]\n", main_color.red, main_color.green, main_color.blue); webSocket.sendTXT(num, "OK"); } - // # ==> Set speed + // ? ==> Set speed if (payload[0] == '?') { uint8_t d = (uint8_t) strtol((const char *) &payload[1], NULL, 10); ws2812fx_speed = constrain(d, 0, 255); @@ -146,7 +236,7 @@ void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t lenght webSocket.sendTXT(num, "OK"); } - // # ==> Set brightness + // % ==> Set brightness if (payload[0] == '%') { uint8_t b = (uint8_t) strtol((const char *) &payload[1], NULL, 10); brightness = ((b >> 0) & 0xFF); @@ -155,79 +245,24 @@ void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t lenght webSocket.sendTXT(num, "OK"); } - // * ==> Set main color and light all LEDs (Shortcut) if (payload[0] == '*') { - // decode rgb data - uint32_t rgb = (uint32_t) strtol((const char *) &payload[1], NULL, 16); - - main_color.red = ((rgb >> 16) & 0xFF); - main_color.green = ((rgb >> 8) & 0xFF); - main_color.blue = ((rgb >> 0) & 0xFF); - - for (int i = 0; i < strip.numPixels(); i++) { - strip.setPixelColor(i, main_color.red, main_color.green, main_color.blue); - } - strip.show(); - DBG_OUTPUT_PORT.printf("WS: Set all leds to main color: [%u] [%u] [%u]\n", main_color.red, main_color.green, main_color.blue); - exit_func = true; - mode = ALL; + handleSetAllMode(payload); webSocket.sendTXT(num, "OK"); } // ! ==> Set single LED in given color if (payload[0] == '!') { - // decode led index - uint64_t rgb = (uint64_t) strtol((const char *) &payload[1], NULL, 16); - - uint8_t led = ((rgb >> 24) & 0xFF); - if (led < strip.numPixels()) { - ledstates[led].red = ((rgb >> 16) & 0xFF); - ledstates[led].green = ((rgb >> 8) & 0xFF); - ledstates[led].blue = ((rgb >> 0) & 0xFF); - DBG_OUTPUT_PORT.printf("WS: Set single led [%u] to [%u] [%u] [%u]!\n", led, ledstates[led].red, ledstates[led].green, ledstates[led].blue); - - for (uint8_t i = 0; i < strip.numPixels(); i++) { - strip.setPixelColor(i, ledstates[i].red, ledstates[i].green, ledstates[i].blue); - //DBG_OUTPUT_PORT.printf("[%u]--[%u] [%u] [%u] [%u] LED index!\n", rgb, i, ledstates[i].red, ledstates[i].green, ledstates[i].blue); - } - strip.show(); - } - exit_func = true; - mode = ALL; + handleSetSingleLED(payload); webSocket.sendTXT(num, "OK"); } - // ! ==> Activate mode + // = ==> Activate named mode if (payload[0] == '=') { // we get mode data String str_mode = String((char *) &payload[0]); - exit_func = true; - if (str_mode.startsWith("=off")) { - mode = OFF; - } - if (str_mode.startsWith("=all")) { - mode = ALL; - } - if (str_mode.startsWith("=wipe")) { - mode = WIPE; - } - if (str_mode.startsWith("=rainbow")) { - mode = RAINBOW; - } - if (str_mode.startsWith("=rainbowCycle")) { - mode = RAINBOWCYCLE; - } - if (str_mode.startsWith("=theaterchase")) { - mode = THEATERCHASE; - } - if (str_mode.startsWith("=theaterchaseRainbow")) { - mode = THEATERCHASERAINBOW; - } - if (str_mode.startsWith("=tv")) { - mode = TV; - } + handleSetNamedMode(str_mode); DBG_OUTPUT_PORT.printf("Activated mode [%u]!\n", mode); webSocket.sendTXT(num, "OK"); @@ -242,7 +277,7 @@ void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t lenght webSocket.sendTXT(num, json); } - // $ ==> Get WS2812 modes. + // ~ ==> Get WS2812 modes. if (payload[0] == '~') { DBG_OUTPUT_PORT.printf("Get WS2812 modes."); @@ -251,16 +286,9 @@ void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t lenght webSocket.sendTXT(num, json); } - // $ ==> Set WS2812 mode. + // / ==> Set WS2812 mode. if (payload[0] == '/') { - mode = HOLD; - uint8_t ws2812fx_mode = (uint8_t) strtol((const char *) &payload[1], NULL, 10); - ws2812fx_mode = constrain(ws2812fx_mode, 0, 255); - strip.setColor(main_color.red, main_color.green, main_color.blue); - strip.setMode(ws2812fx_mode); - - //String json = listStatusJSON(); - //DBG_OUTPUT_PORT.println(json); + handleSetWS2812FXMode(payload); webSocket.sendTXT(num, "OK"); } break; @@ -271,3 +299,113 @@ void checkForRequests() { webSocket.loop(); server.handleClient(); } + + +// *************************************************************************** +// MQTT callback / connection handler +// *************************************************************************** +#ifdef ENABLE_MQTT + void mqtt_callback(char* topic, byte* payload, unsigned int length) { + DBG_OUTPUT_PORT.print("Message arrived ["); + DBG_OUTPUT_PORT.print(topic); + DBG_OUTPUT_PORT.print("] "); + for (int i = 0; i < length; i++) { + DBG_OUTPUT_PORT.print((char)payload[i]); + } + DBG_OUTPUT_PORT.println(); + + // # ==> Set main color + if (payload[0] == '#') { + handleSetMainColor(payload); + DBG_OUTPUT_PORT.printf("MQTT: Set main color to [%u] [%u] [%u]\n", main_color.red, main_color.green, main_color.blue); + mqtt_client.publish(mqtt_outtopic, "OK"); + } + + // ? ==> Set speed + if (payload[0] == '?') { + uint8_t d = (uint8_t) strtol((const char *) &payload[1], NULL, 10); + ws2812fx_speed = constrain(d, 0, 255); + strip.setSpeed(ws2812fx_speed); + DBG_OUTPUT_PORT.printf("MQTT: Set speed to [%u]\n", ws2812fx_speed); + mqtt_client.publish(mqtt_outtopic, "OK"); + } + + // % ==> Set brightness + if (payload[0] == '%') { + uint8_t b = (uint8_t) strtol((const char *) &payload[1], NULL, 10); + brightness = constrain(b, 0, 255); + strip.setBrightness(brightness); + DBG_OUTPUT_PORT.printf("MQTT: Set brightness to [%u]\n", brightness); + mqtt_client.publish(mqtt_outtopic, "OK"); + } + + // * ==> Set main color and light all LEDs (Shortcut) + if (payload[0] == '*') { + handleSetAllMode(payload); + DBG_OUTPUT_PORT.printf("MQTT: Set main color and light all LEDs [%u]\n", payload); + mqtt_client.publish(mqtt_outtopic, "OK"); + } + + // ! ==> Set single LED in given color + if (payload[0] == '!') { + handleSetSingleLED(payload); + DBG_OUTPUT_PORT.printf("MQTT: Set single LED in given color [%u]\n", payload); + mqtt_client.publish(mqtt_outtopic, "OK"); + } + + // = ==> Activate named mode + if (payload[0] == '=') { + String str_mode = String((char *) &payload[0]); + handleSetNamedMode(str_mode); + DBG_OUTPUT_PORT.printf("MQTT: Activate named mode [%u]\n", payload); + mqtt_client.publish(mqtt_outtopic, "OK"); + } + + // $ ==> Get status Info. + if (payload[0] == '$') { + DBG_OUTPUT_PORT.printf("MQTT: Get status info."); + mqtt_client.publish(mqtt_outtopic, listStatusJSON()); + } + + // ~ ==> Get WS2812 modes. + // TODO: Fix this, doesn't return anything. Too long? + if (payload[0] == '~') { + DBG_OUTPUT_PORT.printf("MQTT: Get WS2812 modes."); + String json_modes = listModesJSON(); + DBG_OUTPUT_PORT.printf(json_modes.c_str()); + + mqtt_client.publish(mqtt_outtopic, json_modes.c_str()); + } + + // / ==> Set WS2812 mode. + if ((char)payload[0] == '/') { + handleSetWS2812FXMode(payload); + DBG_OUTPUT_PORT.printf("MQTT: Set WS2812 mode [%u]\n", payload); + mqtt_client.publish(mqtt_outtopic, "OK"); + } + } + + void mqtt_reconnect() { + // Loop until we're reconnected + while (!mqtt_client.connected()) { + DBG_OUTPUT_PORT.print("Attempting MQTT connection..."); + // Attempt to connect + if (mqtt_client.connect("ESP8266Client")) { + DBG_OUTPUT_PORT.println("connected"); + // Once connected, publish an announcement... + char * message = new char[18 + strlen(HOSTNAME) + 1]; + strcpy(message, "McLighting ready: "); + strcat(message, HOSTNAME); + mqtt_client.publish(mqtt_outtopic, message); + // ... and resubscribe + mqtt_client.subscribe(mqtt_intopic); + } else { + DBG_OUTPUT_PORT.print("failed, rc="); + DBG_OUTPUT_PORT.print(mqtt_client.state()); + DBG_OUTPUT_PORT.println(" try again in 5 seconds"); + // Wait 5 seconds before retrying + delay(5000); + } + } + } +#endif From b71834bbb229ead698d1b7f5402cb02383e23213 Mon Sep 17 00:00:00 2001 From: Tobias Blum Date: Sun, 6 Aug 2017 16:32:39 +0200 Subject: [PATCH 2/7] Deactivated: Get WS2812 modes. Message too large --- Arduino/McLighting/definitions.h | 1 + Arduino/McLighting/request_handlers.h | 21 ++++++++++++--------- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/Arduino/McLighting/definitions.h b/Arduino/McLighting/definitions.h index 30ff7c1..0b28f27 100644 --- a/Arduino/McLighting/definitions.h +++ b/Arduino/McLighting/definitions.h @@ -8,6 +8,7 @@ #define ENABLE_OTA // If defined, enable Arduino OTA code. #define ENABLE_MQTT // If defined, enable MQTT client code. +#define MQTT_MAX_PACKET_SIZE 4096 #ifdef ENABLE_MQTT const char mqtt_intopic[] = "inTopic"; const char mqtt_outtopic[] = "outTopic"; diff --git a/Arduino/McLighting/request_handlers.h b/Arduino/McLighting/request_handlers.h index 58ef200..88f1e55 100644 --- a/Arduino/McLighting/request_handlers.h +++ b/Arduino/McLighting/request_handlers.h @@ -342,14 +342,14 @@ void checkForRequests() { // * ==> Set main color and light all LEDs (Shortcut) if (payload[0] == '*') { handleSetAllMode(payload); - DBG_OUTPUT_PORT.printf("MQTT: Set main color and light all LEDs [%u]\n", payload); + DBG_OUTPUT_PORT.printf("MQTT: Set main color and light all LEDs [%s]\n", payload); mqtt_client.publish(mqtt_outtopic, "OK"); } // ! ==> Set single LED in given color if (payload[0] == '!') { handleSetSingleLED(payload); - DBG_OUTPUT_PORT.printf("MQTT: Set single LED in given color [%u]\n", payload); + DBG_OUTPUT_PORT.printf("MQTT: Set single LED in given color [%s]\n", payload); mqtt_client.publish(mqtt_outtopic, "OK"); } @@ -357,30 +357,33 @@ void checkForRequests() { if (payload[0] == '=') { String str_mode = String((char *) &payload[0]); handleSetNamedMode(str_mode); - DBG_OUTPUT_PORT.printf("MQTT: Activate named mode [%u]\n", payload); + DBG_OUTPUT_PORT.printf("MQTT: Activate named mode [%s]\n", payload); mqtt_client.publish(mqtt_outtopic, "OK"); } // $ ==> Get status Info. if (payload[0] == '$') { - DBG_OUTPUT_PORT.printf("MQTT: Get status info."); + DBG_OUTPUT_PORT.printf("MQTT: Get status info.\n"); mqtt_client.publish(mqtt_outtopic, listStatusJSON()); } // ~ ==> Get WS2812 modes. // TODO: Fix this, doesn't return anything. Too long? if (payload[0] == '~') { - DBG_OUTPUT_PORT.printf("MQTT: Get WS2812 modes."); - String json_modes = listModesJSON(); - DBG_OUTPUT_PORT.printf(json_modes.c_str()); + DBG_OUTPUT_PORT.printf("MQTT: Get WS2812 modes.\n"); + DBG_OUTPUT_PORT.printf("Error: Not implemented. Message too large for pubsubclient."); + mqtt_client.publish(mqtt_outtopic, "ERROR: Not implemented. Message too large for pubsubclient."); + //String json_modes = listModesJSON(); + //DBG_OUTPUT_PORT.printf(json_modes.c_str()); - mqtt_client.publish(mqtt_outtopic, json_modes.c_str()); + //int res = mqtt_client.publish(mqtt_outtopic, json_modes.c_str(), json_modes.length()); + //DBG_OUTPUT_PORT.printf("Result: %d / %d", res, json_modes.length()); } // / ==> Set WS2812 mode. if ((char)payload[0] == '/') { handleSetWS2812FXMode(payload); - DBG_OUTPUT_PORT.printf("MQTT: Set WS2812 mode [%u]\n", payload); + DBG_OUTPUT_PORT.printf("MQTT: Set WS2812 mode [%s]\n", payload); mqtt_client.publish(mqtt_outtopic, "OK"); } } From 90578a4b33c5ec22b423dc7fa097302beb742700 Mon Sep 17 00:00:00 2001 From: Tobias Blum Date: Sun, 6 Aug 2017 18:11:01 +0200 Subject: [PATCH 3/7] Dynamic topic names --- Arduino/McLighting/McLighting.ino | 5 +- Arduino/McLighting/definitions.h | 10 +- Arduino/McLighting/request_handlers.h | 128 +++++++++++++------------- 3 files changed, 75 insertions(+), 68 deletions(-) diff --git a/Arduino/McLighting/McLighting.ino b/Arduino/McLighting/McLighting.ino index 794f302..567bcbe 100644 --- a/Arduino/McLighting/McLighting.ino +++ b/Arduino/McLighting/McLighting.ino @@ -120,7 +120,7 @@ void setup() { // set builtin led pin as output pinMode(BUILTIN_LED, OUTPUT); // start ticker with 0.5 because we start in AP mode and try to connect - ticker.attach(0.6, tick); + ticker.attach(0.5, tick); // *************************************************************************** // Setup: Neopixel @@ -207,6 +207,9 @@ void setup() { // Configure MQTT // *************************************************************************** #ifdef ENABLE_MQTT + String(String(HOSTNAME) + "/in").toCharArray(mqtt_intopic, 32); + String(String(HOSTNAME) + "/out").toCharArray(mqtt_outtopic, 32); + mqtt_client.setServer(mqtt_server, 1883); mqtt_client.setCallback(mqtt_callback); #endif diff --git a/Arduino/McLighting/definitions.h b/Arduino/McLighting/definitions.h index 0b28f27..fea7bc7 100644 --- a/Arduino/McLighting/definitions.h +++ b/Arduino/McLighting/definitions.h @@ -3,16 +3,16 @@ #define NUMLEDS 24 // Number of leds in the strip -#define HOSTNAME "ESP8266_VORONOI" // Friedly hostname +const char HOSTNAME[] = "ESP8266_VORONOI"; // Friedly hostname #define ENABLE_OTA // If defined, enable Arduino OTA code. #define ENABLE_MQTT // If defined, enable MQTT client code. -#define MQTT_MAX_PACKET_SIZE 4096 #ifdef ENABLE_MQTT - const char mqtt_intopic[] = "inTopic"; - const char mqtt_outtopic[] = "outTopic"; - const char mqtt_server[] = "raspberrypi2"; + #define MQTT_MAX_PACKET_SIZE 256 + char mqtt_intopic[strlen(HOSTNAME) + 3]; // Topic in will be: /in + char mqtt_outtopic[strlen(HOSTNAME) + 4]; // Topic out will be: /out + const char mqtt_server[] = "raspberrypi2"; // Hostname of the MQTT broker #endif // *************************************************************************** diff --git a/Arduino/McLighting/request_handlers.h b/Arduino/McLighting/request_handlers.h index 88f1e55..d66f3ff 100644 --- a/Arduino/McLighting/request_handlers.h +++ b/Arduino/McLighting/request_handlers.h @@ -38,20 +38,21 @@ void getArgs() { } - - -void handleSetMainColor(uint8_t * payload) { +// *************************************************************************** +// Handler functions for WS and MQTT +// *************************************************************************** +void handleSetMainColor(uint8_t * mypayload) { // decode rgb data - uint32_t rgb = (uint32_t) strtol((const char *) &payload[1], NULL, 16); + uint32_t rgb = (uint32_t) strtol((const char *) &mypayload[1], NULL, 16); main_color.red = ((rgb >> 16) & 0xFF); main_color.green = ((rgb >> 8) & 0xFF); main_color.blue = ((rgb >> 0) & 0xFF); strip.setColor(main_color.red, main_color.green, main_color.blue); } -void handleSetAllMode(uint8_t * payload) { +void handleSetAllMode(uint8_t * mypayload) { // decode rgb data - uint32_t rgb = (uint32_t) strtol((const char *) &payload[1], NULL, 16); + uint32_t rgb = (uint32_t) strtol((const char *) &mypayload[1], NULL, 16); main_color.red = ((rgb >> 16) & 0xFF); main_color.green = ((rgb >> 8) & 0xFF); @@ -66,9 +67,9 @@ void handleSetAllMode(uint8_t * payload) { mode = ALL; } -void handleSetSingleLED(uint8_t * payload) { +void handleSetSingleLED(uint8_t * mypayload) { // decode led index - uint64_t rgb = (uint64_t) strtol((const char *) &payload[1], NULL, 16); + uint64_t rgb = (uint64_t) strtol((const char *) &mypayload[1], NULL, 16); uint8_t led = ((rgb >> 24) & 0xFF); if (led < strip.numPixels()) { @@ -116,65 +117,14 @@ void handleSetNamedMode(String str_mode) { } } -void handleSetWS2812FXMode(uint8_t * payload) { +void handleSetWS2812FXMode(uint8_t * mypayload) { mode = HOLD; - uint8_t ws2812fx_mode = (uint8_t) strtol((const char *) &payload[1], NULL, 10); + uint8_t ws2812fx_mode = (uint8_t) strtol((const char *) &mypayload[1], NULL, 10); ws2812fx_mode = constrain(ws2812fx_mode, 0, 255); strip.setColor(main_color.red, main_color.green, main_color.blue); strip.setMode(ws2812fx_mode); } - - - - - - - - -void handleMinimalUpload() { - char temp[1500]; - int sec = millis() / 1000; - int min = sec / 60; - int hr = min / 60; - - snprintf ( temp, 1500, - "\ - \ - \ - ESP8266 Upload\ - \ - \ - \ - \ - \ -
\ - \ - \ - \ -
\ - \ - ", - hr, min % 60, sec % 60 - ); - server.send ( 200, "text/html", temp ); -} - -void handleNotFound() { - String message = "File Not Found\n\n"; - message += "URI: "; - message += server.uri(); - message += "\nMethod: "; - message += ( server.method() == HTTP_GET ) ? "GET" : "POST"; - message += "\nArguments: "; - message += server.args(); - message += "\n"; - for ( uint8_t i = 0; i < server.args(); i++ ) { - message += " " + server.argName ( i ) + ": " + server.arg ( i ) + "\n"; - } - server.send ( 404, "text/plain", message ); -} - char* listStatusJSON() { char json[255]; snprintf(json, sizeof(json), "{\"mode\":%d, \"ws2812fx_mode\":%d, \"ws2812fx_mode_name\":\"%s\", \"speed\":%d, \"brightness\":%d, \"color\":[%d, %d, %d]}", mode, strip.getMode(), strip.getModeName(strip.getMode()), ws2812fx_speed, brightness, main_color.red, main_color.green, main_color.blue); @@ -202,6 +152,57 @@ void getModesJSON() { server.send ( 200, "application/json", listModesJSON() ); } + +// *************************************************************************** +// HTTP request handlers +// *************************************************************************** +void handleMinimalUpload() { + char temp[1500]; + int sec = millis() / 1000; + int min = sec / 60; + int hr = min / 60; + + snprintf ( temp, 1500, + "\ + \ + \ + ESP8266 Upload\ + \ + \ + \ + \ + \ +
\ + \ + \ + \ +
\ + \ + ", + hr, min % 60, sec % 60 + ); + server.send ( 200, "text/html", temp ); +} + +void handleNotFound() { + String message = "File Not Found\n\n"; + message += "URI: "; + message += server.uri(); + message += "\nMethod: "; + message += ( server.method() == HTTP_GET ) ? "GET" : "POST"; + message += "\nArguments: "; + message += server.args(); + message += "\n"; + for ( uint8_t i = 0; i < server.args(); i++ ) { + message += " " + server.argName ( i ) + ": " + server.arg ( i ) + "\n"; + } + server.send ( 404, "text/plain", message ); +} + + +// *************************************************************************** +// WS request handlers +// *************************************************************************** void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t lenght) { switch (type) { case WStype_DISCONNECTED: @@ -381,7 +382,7 @@ void checkForRequests() { } // / ==> Set WS2812 mode. - if ((char)payload[0] == '/') { + if (payload[0] == '/') { handleSetWS2812FXMode(payload); DBG_OUTPUT_PORT.printf("MQTT: Set WS2812 mode [%s]\n", payload); mqtt_client.publish(mqtt_outtopic, "OK"); @@ -402,6 +403,9 @@ void checkForRequests() { mqtt_client.publish(mqtt_outtopic, message); // ... and resubscribe mqtt_client.subscribe(mqtt_intopic); + + DBG_OUTPUT_PORT.printf("MQTT topic in: %s\n", mqtt_intopic); + DBG_OUTPUT_PORT.printf("MQTT topic out: %s\n", mqtt_outtopic); } else { DBG_OUTPUT_PORT.print("failed, rc="); DBG_OUTPUT_PORT.print(mqtt_client.state()); From c160b910d93d6c9de81170997a3a11ce3a7659a6 Mon Sep 17 00:00:00 2001 From: Tobias Blum Date: Sun, 6 Aug 2017 21:20:16 +0200 Subject: [PATCH 4/7] Bugfix: payload parsing --- Arduino/McLighting/definitions.h | 7 +++--- Arduino/McLighting/request_handlers.h | 33 ++++++++++++--------------- 2 files changed, 19 insertions(+), 21 deletions(-) diff --git a/Arduino/McLighting/definitions.h b/Arduino/McLighting/definitions.h index fea7bc7..fd0312e 100644 --- a/Arduino/McLighting/definitions.h +++ b/Arduino/McLighting/definitions.h @@ -10,9 +10,10 @@ const char HOSTNAME[] = "ESP8266_VORONOI"; // Friedly hostname #define ENABLE_MQTT // If defined, enable MQTT client code. #ifdef ENABLE_MQTT #define MQTT_MAX_PACKET_SIZE 256 - char mqtt_intopic[strlen(HOSTNAME) + 3]; // Topic in will be: /in - char mqtt_outtopic[strlen(HOSTNAME) + 4]; // Topic out will be: /out - const char mqtt_server[] = "raspberrypi2"; // Hostname of the MQTT broker + char mqtt_intopic[strlen(HOSTNAME) + 3]; // Topic in will be: /in + char mqtt_outtopic[strlen(HOSTNAME) + 4]; // Topic out will be: /out + const char mqtt_server[] = "raspberrypi2"; // Hostname of the MQTT broker + const char mqtt_clientid[] = "ESP8266Client"; // MQTT ClientID #endif // *************************************************************************** diff --git a/Arduino/McLighting/request_handlers.h b/Arduino/McLighting/request_handlers.h index d66f3ff..6bd5c03 100644 --- a/Arduino/McLighting/request_handlers.h +++ b/Arduino/McLighting/request_handlers.h @@ -306,20 +306,17 @@ void checkForRequests() { // MQTT callback / connection handler // *************************************************************************** #ifdef ENABLE_MQTT - void mqtt_callback(char* topic, byte* payload, unsigned int length) { - DBG_OUTPUT_PORT.print("Message arrived ["); - DBG_OUTPUT_PORT.print(topic); - DBG_OUTPUT_PORT.print("] "); - for (int i = 0; i < length; i++) { - DBG_OUTPUT_PORT.print((char)payload[i]); - } - DBG_OUTPUT_PORT.println(); + void mqtt_callback(char* topic, byte* payload_in, unsigned int length) { + uint8_t * payload = (uint8_t *)malloc(length + 1); + memcpy(payload, payload_in, length); + payload[length] = NULL; + DBG_OUTPUT_PORT.printf("MQTT: Message arrived [%s]\n", payload); // # ==> Set main color if (payload[0] == '#') { handleSetMainColor(payload); DBG_OUTPUT_PORT.printf("MQTT: Set main color to [%u] [%u] [%u]\n", main_color.red, main_color.green, main_color.blue); - mqtt_client.publish(mqtt_outtopic, "OK"); + mqtt_client.publish(mqtt_outtopic, String(String("OK ") + String((char *)payload)).c_str()); } // ? ==> Set speed @@ -328,7 +325,7 @@ void checkForRequests() { ws2812fx_speed = constrain(d, 0, 255); strip.setSpeed(ws2812fx_speed); DBG_OUTPUT_PORT.printf("MQTT: Set speed to [%u]\n", ws2812fx_speed); - mqtt_client.publish(mqtt_outtopic, "OK"); + mqtt_client.publish(mqtt_outtopic, String(String("OK ") + String((char *)payload)).c_str()); } // % ==> Set brightness @@ -337,21 +334,21 @@ void checkForRequests() { brightness = constrain(b, 0, 255); strip.setBrightness(brightness); DBG_OUTPUT_PORT.printf("MQTT: Set brightness to [%u]\n", brightness); - mqtt_client.publish(mqtt_outtopic, "OK"); + mqtt_client.publish(mqtt_outtopic, String(String("OK ") + String((char *)payload)).c_str()); } // * ==> Set main color and light all LEDs (Shortcut) if (payload[0] == '*') { handleSetAllMode(payload); DBG_OUTPUT_PORT.printf("MQTT: Set main color and light all LEDs [%s]\n", payload); - mqtt_client.publish(mqtt_outtopic, "OK"); + mqtt_client.publish(mqtt_outtopic, String(String("OK ") + String((char *)payload)).c_str()); } // ! ==> Set single LED in given color if (payload[0] == '!') { handleSetSingleLED(payload); DBG_OUTPUT_PORT.printf("MQTT: Set single LED in given color [%s]\n", payload); - mqtt_client.publish(mqtt_outtopic, "OK"); + mqtt_client.publish(mqtt_outtopic, String(String("OK ") + String((char *)payload)).c_str()); } // = ==> Activate named mode @@ -359,7 +356,7 @@ void checkForRequests() { String str_mode = String((char *) &payload[0]); handleSetNamedMode(str_mode); DBG_OUTPUT_PORT.printf("MQTT: Activate named mode [%s]\n", payload); - mqtt_client.publish(mqtt_outtopic, "OK"); + mqtt_client.publish(mqtt_outtopic, String(String("OK ") + String((char *)payload)).c_str()); } // $ ==> Get status Info. @@ -385,17 +382,17 @@ void checkForRequests() { if (payload[0] == '/') { handleSetWS2812FXMode(payload); DBG_OUTPUT_PORT.printf("MQTT: Set WS2812 mode [%s]\n", payload); - mqtt_client.publish(mqtt_outtopic, "OK"); + mqtt_client.publish(mqtt_outtopic, String(String("OK ") + String((char *)payload)).c_str()); } } void mqtt_reconnect() { // Loop until we're reconnected while (!mqtt_client.connected()) { - DBG_OUTPUT_PORT.print("Attempting MQTT connection..."); + DBG_OUTPUT_PORT.print("Attempting MQTT connection... "); // Attempt to connect - if (mqtt_client.connect("ESP8266Client")) { - DBG_OUTPUT_PORT.println("connected"); + if (mqtt_client.connect(mqtt_clientid)) { + DBG_OUTPUT_PORT.println("connected!"); // Once connected, publish an announcement... char * message = new char[18 + strlen(HOSTNAME) + 1]; strcpy(message, "McLighting ready: "); From 20941906e18a9deba4aafebed615a9dff1ce72b1 Mon Sep 17 00:00:00 2001 From: Tobias Blum Date: Sun, 6 Aug 2017 21:36:30 +0200 Subject: [PATCH 5/7] Add: MQTT user / pass --- Arduino/McLighting/McLighting.ino | 9 +++++---- Arduino/McLighting/colormodes.h | 5 +++-- Arduino/McLighting/definitions.h | 5 ++++- Arduino/McLighting/request_handlers.h | 2 +- 4 files changed, 13 insertions(+), 8 deletions(-) diff --git a/Arduino/McLighting/McLighting.ino b/Arduino/McLighting/McLighting.ino index 567bcbe..76e504a 100644 --- a/Arduino/McLighting/McLighting.ino +++ b/Arduino/McLighting/McLighting.ino @@ -31,10 +31,11 @@ PubSubClient mqtt_client(espClient); #endif + // *************************************************************************** // Instanciate HTTP(80) / WebSockets(81) Server // *************************************************************************** -ESP8266WebServer server ( 80 ); +ESP8266WebServer server(80); WebSocketsServer webSocket = WebSocketsServer(81); @@ -93,7 +94,6 @@ void configModeCallback (WiFiManager *myWiFiManager) { } - // *************************************************************************** // Include: Webserver // *************************************************************************** @@ -207,8 +207,8 @@ void setup() { // Configure MQTT // *************************************************************************** #ifdef ENABLE_MQTT - String(String(HOSTNAME) + "/in").toCharArray(mqtt_intopic, 32); - String(String(HOSTNAME) + "/out").toCharArray(mqtt_outtopic, 32); + String(String(HOSTNAME) + "/in").toCharArray(mqtt_intopic, strlen(HOSTNAME) + 3); + String(String(HOSTNAME) + "/out").toCharArray(mqtt_outtopic, strlen(HOSTNAME) + 4); mqtt_client.setServer(mqtt_server, 1883); mqtt_client.setCallback(mqtt_callback); @@ -232,6 +232,7 @@ void setup() { DBG_OUTPUT_PORT.println("/upload to upload the webpages first."); DBG_OUTPUT_PORT.println(""); + // *************************************************************************** // Setup: WebSocket server diff --git a/Arduino/McLighting/colormodes.h b/Arduino/McLighting/colormodes.h index 33d7bf9..aa7c48b 100644 --- a/Arduino/McLighting/colormodes.h +++ b/Arduino/McLighting/colormodes.h @@ -17,12 +17,13 @@ int analogLevel = 100; boolean timeToDip = false; int ledStates[NUMLEDS]; + void hsb2rgbAN1(uint16_t index, uint8_t sat, uint8_t bright, uint8_t myled) { // Source: https://blog.adafruit.com/2012/03/14/constant-brightness-hsb-to-rgb-algorithm/ uint8_t temp[5], n = (index >> 8) % 3; - temp[0] = temp[3] = (uint8_t)(( (sat ^ 255) * bright) / 255); + temp[0] = temp[3] = (uint8_t)(( (sat ^ 255) * bright) / 255); temp[1] = temp[4] = (uint8_t)((((( (index & 255) * sat) / 255) + (sat ^ 255)) * bright) / 255); - temp[2] = (uint8_t)(((((((index & 255) ^ 255) * sat) / 255) + (sat ^ 255)) * bright) / 255); + temp[2] = (uint8_t)(((((((index & 255) ^ 255) * sat) / 255) + (sat ^ 255)) * bright) / 255); strip.setPixelColor(myled, temp[n + 2], temp[n + 1], temp[n]); } diff --git a/Arduino/McLighting/definitions.h b/Arduino/McLighting/definitions.h index fd0312e..6e3cc1f 100644 --- a/Arduino/McLighting/definitions.h +++ b/Arduino/McLighting/definitions.h @@ -12,8 +12,11 @@ const char HOSTNAME[] = "ESP8266_VORONOI"; // Friedly hostname #define MQTT_MAX_PACKET_SIZE 256 char mqtt_intopic[strlen(HOSTNAME) + 3]; // Topic in will be: /in char mqtt_outtopic[strlen(HOSTNAME) + 4]; // Topic out will be: /out - const char mqtt_server[] = "raspberrypi2"; // Hostname of the MQTT broker + const char mqtt_clientid[] = "ESP8266Client"; // MQTT ClientID + const char mqtt_server[] = "raspberrypi2"; // Hostname of the MQTT broker + const char mqtt_username[] = ""; // MQTT Username + const char mqtt_password[] = ""; // MQTT Password #endif // *************************************************************************** diff --git a/Arduino/McLighting/request_handlers.h b/Arduino/McLighting/request_handlers.h index 6bd5c03..f1061e4 100644 --- a/Arduino/McLighting/request_handlers.h +++ b/Arduino/McLighting/request_handlers.h @@ -391,7 +391,7 @@ void checkForRequests() { while (!mqtt_client.connected()) { DBG_OUTPUT_PORT.print("Attempting MQTT connection... "); // Attempt to connect - if (mqtt_client.connect(mqtt_clientid)) { + if (mqtt_client.connect(mqtt_clientid, mqtt_username, mqtt_password)) { DBG_OUTPUT_PORT.println("connected!"); // Once connected, publish an announcement... char * message = new char[18 + strlen(HOSTNAME) + 1]; From 0895d542a3c85f13222f7d5d0f5f4e12aa3232c3 Mon Sep 17 00:00:00 2001 From: Tobias Blum Date: Sun, 6 Aug 2017 23:34:59 +0200 Subject: [PATCH 6/7] Make MQTT params configurable Saving into EEPROM --- Arduino/McLighting/McLighting.ino | 98 ++++++++++++++++++++++++++- Arduino/McLighting/definitions.h | 11 ++- Arduino/McLighting/request_handlers.h | 2 +- 3 files changed, 105 insertions(+), 6 deletions(-) diff --git a/Arduino/McLighting/McLighting.ino b/Arduino/McLighting/McLighting.ino index 76e504a..935b6f2 100644 --- a/Arduino/McLighting/McLighting.ino +++ b/Arduino/McLighting/McLighting.ino @@ -13,6 +13,7 @@ #include #include #include +#include #include //https://github.com/Links2004/arduinoWebSockets #include @@ -74,6 +75,38 @@ void tick() } +// *************************************************************************** +// EEPROM helper +// *************************************************************************** +String readEEPROM(int offset, int len) { + String res = ""; + for (int i = 0; i < len; ++i) + { + res += char(EEPROM.read(i + offset)); + //DBG_OUTPUT_PORT.println(char(EEPROM.read(i + offset))); + } + + DBG_OUTPUT_PORT.print("Read EEPROM: ["); + DBG_OUTPUT_PORT.print(res); + DBG_OUTPUT_PORT.println("]"); + return res; +} + +void writeEEPROM(int offset, int len, String value) { + for (int i = 0; i < len; ++i) + { + if (i < value.length()) { + EEPROM.write(i + offset, value[i]); + } else { + EEPROM.write(i + offset, NULL); + } + + DBG_OUTPUT_PORT.print("Wrote EEPROM: "); + DBG_OUTPUT_PORT.println(value[i]); + } +} + + // *************************************************************************** // Callback for WiFiManager library when config mode is entered // *************************************************************************** @@ -93,6 +126,11 @@ void configModeCallback (WiFiManager *myWiFiManager) { strip.show(); } +//callback notifying us of the need to save config +void saveConfigCallback () { + DBG_OUTPUT_PORT.println("Should save config"); + shouldSaveConfig = true; +} // *************************************************************************** // Include: Webserver @@ -116,6 +154,7 @@ void configModeCallback (WiFiManager *myWiFiManager) { // *************************************************************************** void setup() { DBG_OUTPUT_PORT.begin(115200); + EEPROM.begin(512); // set builtin led pin as output pinMode(BUILTIN_LED, OUTPUT); @@ -135,6 +174,28 @@ void setup() { // *************************************************************************** // Setup: WiFiManager // *************************************************************************** + // The extra parameters to be configured (can be either global or just in the setup) + // After connecting, parameter.getValue() will get you the configured value + // id/name placeholder/prompt default length + #ifdef ENABLE_MQTT + String settings_available = readEEPROM(134, 1); + if (settings_available == "1") { + readEEPROM(0, 64).toCharArray(mqtt_host, 64); // 0-63 + readEEPROM(64, 6).toCharArray(mqtt_port, 6); // 64-69 + readEEPROM(70, 32).toCharArray(mqtt_user, 32); // 70-101 + readEEPROM(102, 32).toCharArray(mqtt_pass, 32); // 102-133 + DBG_OUTPUT_PORT.printf("MQTT host: %s\n", mqtt_host); + DBG_OUTPUT_PORT.printf("MQTT port: %s\n", mqtt_port); + DBG_OUTPUT_PORT.printf("MQTT user: %s\n", mqtt_user); + DBG_OUTPUT_PORT.printf("MQTT pass: %s\n", mqtt_pass); + } + + WiFiManagerParameter custom_mqtt_host("host", "MQTT hostname", mqtt_host, 64); + WiFiManagerParameter custom_mqtt_port("port", "MQTT port", mqtt_port, 6); + WiFiManagerParameter custom_mqtt_user("user", "MQTT user", mqtt_user, 32); + WiFiManagerParameter custom_mqtt_pass("pass", "MQTT pass", mqtt_pass, 32); + #endif + //Local intialization. Once its business is done, there is no need to keep it around WiFiManager wifiManager; //reset settings - for testing @@ -143,6 +204,17 @@ void setup() { //set callback that gets called when connecting to previous WiFi fails, and enters Access Point mode wifiManager.setAPCallback(configModeCallback); + #ifdef ENABLE_MQTT + //set config save notify callback + wifiManager.setSaveConfigCallback(saveConfigCallback); + + //add all your parameters here + wifiManager.addParameter(&custom_mqtt_host); + wifiManager.addParameter(&custom_mqtt_port); + wifiManager.addParameter(&custom_mqtt_user); + wifiManager.addParameter(&custom_mqtt_pass); + #endif + //fetches ssid and pass and tries to connect //if it does not connect it starts an access point with the specified name //here "AutoConnectAP" @@ -154,6 +226,26 @@ void setup() { delay(1000); } + #ifdef ENABLE_MQTT + //read updated parameters + strcpy(mqtt_host, custom_mqtt_host.getValue()); + strcpy(mqtt_port, custom_mqtt_port.getValue()); + strcpy(mqtt_user, custom_mqtt_user.getValue()); + strcpy(mqtt_pass, custom_mqtt_pass.getValue()); + + //save the custom parameters to FS + if (shouldSaveConfig) { + DBG_OUTPUT_PORT.println("Saving WiFiManager config"); + + writeEEPROM(0, 64, mqtt_host); // 0-63 + writeEEPROM(64, 6, mqtt_port); // 64-69 + writeEEPROM(70, 32, mqtt_user); // 70-101 + writeEEPROM(102, 32, mqtt_pass); // 102-133 + writeEEPROM(134, 1, "1"); // 134 --> alwasy "1" + EEPROM.commit(); + } + #endif + //if you get here you have connected to the WiFi DBG_OUTPUT_PORT.println("connected...yeey :)"); ticker.detach(); @@ -209,8 +301,10 @@ void setup() { #ifdef ENABLE_MQTT String(String(HOSTNAME) + "/in").toCharArray(mqtt_intopic, strlen(HOSTNAME) + 3); String(String(HOSTNAME) + "/out").toCharArray(mqtt_outtopic, strlen(HOSTNAME) + 4); + + DBG_OUTPUT_PORT.printf("Connect %s %d\n", mqtt_host, String(mqtt_port).toInt()); - mqtt_client.setServer(mqtt_server, 1883); + mqtt_client.setServer(mqtt_host, String(mqtt_port).toInt()); mqtt_client.setCallback(mqtt_callback); #endif @@ -225,7 +319,7 @@ void setup() { DBG_OUTPUT_PORT.print("Use http://"); DBG_OUTPUT_PORT.print(HOSTNAME); - DBG_OUTPUT_PORT.println(".local/ when you have Bobjour installed."); + DBG_OUTPUT_PORT.println(".local/ when you have Bonjour installed."); DBG_OUTPUT_PORT.print("New users: Open http://"); DBG_OUTPUT_PORT.print(WiFi.localIP()); diff --git a/Arduino/McLighting/definitions.h b/Arduino/McLighting/definitions.h index 6e3cc1f..97c6cdd 100644 --- a/Arduino/McLighting/definitions.h +++ b/Arduino/McLighting/definitions.h @@ -14,11 +14,14 @@ const char HOSTNAME[] = "ESP8266_VORONOI"; // Friedly hostname char mqtt_outtopic[strlen(HOSTNAME) + 4]; // Topic out will be: /out const char mqtt_clientid[] = "ESP8266Client"; // MQTT ClientID - const char mqtt_server[] = "raspberrypi2"; // Hostname of the MQTT broker - const char mqtt_username[] = ""; // MQTT Username - const char mqtt_password[] = ""; // MQTT Password + + char mqtt_host[64] = ""; + char mqtt_port[6] = ""; + char mqtt_user[32] = ""; + char mqtt_pass[32] = ""; #endif + // *************************************************************************** // Global variables / definitions // *************************************************************************** @@ -36,6 +39,8 @@ int ws2812fx_mode = 0; // Helper variable to set WS2812FX modes bool exit_func = false; // Global helper variable to get out of the color modes when mode changes +bool shouldSaveConfig = false; // For WiFiManger custom config + struct ledstate // Data structure to store a state of a single led { uint8_t red; diff --git a/Arduino/McLighting/request_handlers.h b/Arduino/McLighting/request_handlers.h index f1061e4..eb399ab 100644 --- a/Arduino/McLighting/request_handlers.h +++ b/Arduino/McLighting/request_handlers.h @@ -391,7 +391,7 @@ void checkForRequests() { while (!mqtt_client.connected()) { DBG_OUTPUT_PORT.print("Attempting MQTT connection... "); // Attempt to connect - if (mqtt_client.connect(mqtt_clientid, mqtt_username, mqtt_password)) { + if (mqtt_client.connect(mqtt_clientid, mqtt_user, mqtt_pass)) { DBG_OUTPUT_PORT.println("connected!"); // Once connected, publish an announcement... char * message = new char[18 + strlen(HOSTNAME) + 1]; From 065ae09cea38e3aaa5e16c1927bde4b3292f672a Mon Sep 17 00:00:00 2001 From: Tobias Blum Date: Sun, 6 Aug 2017 23:36:34 +0200 Subject: [PATCH 7/7] Cleanup --- Arduino/McLighting/McLighting.ino | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Arduino/McLighting/McLighting.ino b/Arduino/McLighting/McLighting.ino index 935b6f2..0b877de 100644 --- a/Arduino/McLighting/McLighting.ino +++ b/Arduino/McLighting/McLighting.ino @@ -86,9 +86,9 @@ String readEEPROM(int offset, int len) { //DBG_OUTPUT_PORT.println(char(EEPROM.read(i + offset))); } - DBG_OUTPUT_PORT.print("Read EEPROM: ["); - DBG_OUTPUT_PORT.print(res); - DBG_OUTPUT_PORT.println("]"); + //DBG_OUTPUT_PORT.print("Read EEPROM: ["); + //DBG_OUTPUT_PORT.print(res); + //DBG_OUTPUT_PORT.println("]"); return res; }