From 90578a4b33c5ec22b423dc7fa097302beb742700 Mon Sep 17 00:00:00 2001 From: Tobias Blum Date: Sun, 6 Aug 2017 18:11:01 +0200 Subject: [PATCH] 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());