diff --git a/.cproject b/.cproject index 3f15115..32fba40 100644 --- a/.cproject +++ b/.cproject @@ -29,6 +29,7 @@ + @@ -44,6 +45,7 @@ + @@ -59,6 +61,7 @@ + diff --git a/.gitmodules b/.gitmodules index 26dbf14..7a0a430 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,3 +1,6 @@ [submodule "esp8266boilerplate"] path = esp8266boilerplate url = ../esp8266boilerplate.git +[submodule "esp8266mqttboilerplate"] + path = esp8266mqttboilerplate + url = ../esp8266mqttboilerplate diff --git a/application.cpp b/application.cpp index aea85da..8270198 100644 --- a/application.cpp +++ b/application.cpp @@ -9,7 +9,6 @@ #include "defines.h" -#define MQTT_MAX_PACKET_SIZE 256 #include #include @@ -18,6 +17,8 @@ #include // #include #include +#include + #ifdef WS2811 #define FASTLED_ESP8266_NODEMCU_PIN_ORDER @@ -34,13 +35,10 @@ -void callback(char* topic, byte* payload, unsigned int length); -WiFiClientSecure espClient; -PubSubClient client(espClient); #ifdef WS2811 @@ -100,247 +98,162 @@ static CRGB evaluationColorWord(char* cmd) { +void subscribeApplication() { + mqttClient.subscribe(configBlock.mqttTopicColorCommand); + mqttClient.subscribe(configBlock.mqttTopicCommand); +} -void callback(char* topic, byte* payload, unsigned int length) { - const uint8_t MAX_TOKENS = 5; - const uint8_t BUFSIZE = 128; - if ((length + 1) >= BUFSIZE) { // 1 for terminating NUL -#ifdef DEBUG - Serial.println("Received message too long, ignore it"); + +void callbackApplication(char *topic, uint8_t tokenCnt, char **tokens) { + if (! strcmp(topic, configBlock.mqttTopicCommand)) { + int32_t n, b; + CRGB pseudoColors; + switch (tokenCnt) { + case 1: + // set brightness + b = strtol(tokens[0], NULL, 10); + for (uint8_t i = 0; i < NUM_OF_LEDs; i++) { +#ifdef WS2811 + leds[i].r = b; + leds[i].g = b; + leds[i].b = b; #endif - } else { - char buffer[BUFSIZE]; - memcpy(buffer, payload, length); - *(buffer + length) = 0; -#ifdef DEBUG - Serial.print("Received message: "); - Serial.print(length); - Serial.print(" "); - Serial.print(topic); - Serial.print(" "); - Serial.println(buffer); -#endif - - - char *inbuf = buffer; - char *tk = NULL; - uint8_t tokenCnt = 0; - char *tokens[MAX_TOKENS]; - do { - if (tokenCnt >= MAX_TOKENS) { - break; - } - tk = strtok(inbuf, " "); - inbuf = NULL; - tokens[tokenCnt] = tk; - if (tk) { - tokenCnt++; -#ifdef DEBUG - Serial.print("TokenCnt: "); - Serial.print(tokenCnt); - Serial.println(); +#ifdef PL9823 + pixels.setPixelColor(i, pixels.Color(b, b, b)); #endif } - } while (tk); +#ifdef WS2811 + FastLED.show(); +#endif +#ifdef PL9823 + pixels.show(); +#endif -#ifdef DEBUG - for (uint8_t i = 0; i < tokenCnt; i++) { - Serial.print("Token "); - Serial.print(i); - Serial.print(": "); - Serial.println(tokens[i]); + break; + case 2: + // set brightness for one LED + n = strtol(tokens[0], NULL, 10); + b = strtol(tokens[1], NULL, 10); + if (n >= 0 && n < NUM_OF_LEDs) { +#ifdef WS2811 + leds[n].r = b; + leds[n].g = b; + leds[n].b = b; + FastLED.show(); +#endif +#ifdef PL9823 + pixels.setPixelColor(n, pixels.Color(b, b, b)); + pixels.show(); +#endif + } + break; } -#endif - if (! strcmp(topic, configBlock.mqttTopicCommand)) { - int32_t n, b; - CRGB pseudoColors; - switch (tokenCnt) { - case 1: - // set brightness - b = strtol(tokens[0], NULL, 10); - for (uint8_t i = 0; i < NUM_OF_LEDs; i++) { + } else if (! strcmp(topic, configBlock.mqttTopicColorCommand)) { + int32_t n, red, green, blue; + CRGB colors; + switch (tokenCnt) { + case 1: + // on, off, color word for all LEDs + colors = evaluationColorWord(tokens[0]); + for (uint8_t i = 0; i < NUM_OF_LEDs; i++) { #ifdef WS2811 - leds[i].r = b; - leds[i].g = b; - leds[i].b = b; + leds[i] = colors; #endif #ifdef PL9823 - pixels.setPixelColor(i, pixels.Color(b, b, b)); + pixels.setPixelColor(i, pixels.Color(colors.r, colors.g, colors.b)); #endif - } -#ifdef WS2811 - FastLED.show(); -#endif -#ifdef PL9823 - pixels.show(); -#endif - - break; - case 2: - // set brightness for one LED - n = strtol(tokens[0], NULL, 10); - b = strtol(tokens[1], NULL, 10); - if (n >= 0 && n < NUM_OF_LEDs) { -#ifdef WS2811 - leds[n].r = b; - leds[n].g = b; - leds[n].b = b; - FastLED.show(); -#endif -#ifdef PL9823 - pixels.setPixelColor(n, pixels.Color(b, b, b)); - pixels.show(); -#endif - } - break; } - - } else if (! strcmp(topic, configBlock.mqttTopicColorCommand)) { - int32_t n, red, green, blue; - CRGB colors; - switch (tokenCnt) { - case 1: - // on, off, color word for all LEDs - colors = evaluationColorWord(tokens[0]); - for (uint8_t i = 0; i < NUM_OF_LEDs; i++) { #ifdef WS2811 - leds[i] = colors; + FastLED.show(); #endif #ifdef PL9823 - pixels.setPixelColor(i, pixels.Color(colors.r, colors.g, colors.b)); + pixels.show(); #endif - } + break; + case 2: + // token0 = LED number, token1 = color + n = strtol(tokens[0], NULL, 10); + if (n >= 0 && n < NUM_OF_LEDs) { + colors = evaluationColorWord(tokens[1]); #ifdef WS2811 + leds[n] = colors; FastLED.show(); #endif #ifdef PL9823 - pixels.show(); + pixels.setPixelColor(n, pixels.Color(colors.r, colors.g, colors.b)); + pixels.show(); #endif - break; - case 2: - // token0 = LED number, token1 = color - n = strtol(tokens[0], NULL, 10); - if (n >= 0 && n < NUM_OF_LEDs) { - colors = evaluationColorWord(tokens[1]); -#ifdef WS2811 - leds[n] = colors; - FastLED.show(); -#endif -#ifdef PL9823 - pixels.setPixelColor(n, pixels.Color(colors.r, colors.g, colors.b)); - pixels.show(); -#endif - } - break; - case 3: - // token0 = red, token1 = green, token2 = blue - red = strtol(tokens[0], NULL, 10); - green = strtol(tokens[1], NULL, 10); - blue = strtol(tokens[2], NULL, 10); - for (uint8_t i = 0; i < NUM_OF_LEDs; i++) { -#ifdef WS2811 - leds[i].r = red; - leds[i].g = green; - leds[i].b = blue; -#endif -#ifdef PL9823 - pixels.setPixelColor(i, pixels.Color(red, green, blue)); -#endif - } -#ifdef WS2811 - FastLED.show(); -#endif -#ifdef PL9823 - pixels.show(); -#endif - break; - case 4: - // token0 = LED number, token1 = red, token2 = green, token3 = blue - n = strtol(tokens[0], NULL, 10); - if (n >= 0 && n < NUM_OF_LEDs) { - red = strtol(tokens[1], NULL, 10); - green = strtol(tokens[2], NULL, 10); - blue = strtol(tokens[3], NULL, 10); -#ifdef WS2811 - leds[n].r = red; - leds[n].g = green; - leds[n].b = blue; - FastLED.show(); -#endif -#ifdef PL9823 - pixels.setPixelColor(n, pixels.Color(red, green, blue)); - pixels.show(); -#endif - } - break; } + break; + case 3: + // token0 = red, token1 = green, token2 = blue + red = strtol(tokens[0], NULL, 10); + green = strtol(tokens[1], NULL, 10); + blue = strtol(tokens[2], NULL, 10); + for (uint8_t i = 0; i < NUM_OF_LEDs; i++) { +#ifdef WS2811 + leds[i].r = red; + leds[i].g = green; + leds[i].b = blue; +#endif +#ifdef PL9823 + pixels.setPixelColor(i, pixels.Color(red, green, blue)); +#endif + } +#ifdef WS2811 + FastLED.show(); +#endif +#ifdef PL9823 + pixels.show(); +#endif + break; + case 4: + // token0 = LED number, token1 = red, token2 = green, token3 = blue + n = strtol(tokens[0], NULL, 10); + if (n >= 0 && n < NUM_OF_LEDs) { + red = strtol(tokens[1], NULL, 10); + green = strtol(tokens[2], NULL, 10); + blue = strtol(tokens[3], NULL, 10); +#ifdef WS2811 + leds[n].r = red; + leds[n].g = green; + leds[n].b = blue; + FastLED.show(); +#endif +#ifdef PL9823 + pixels.setPixelColor(n, pixels.Color(red, green, blue)); + pixels.show(); +#endif + } + break; } } } -void reconnect() { - uint32_t currentMillis = millis(); - static uint32_t lastMillis = 0; - - // Loop until we're reconnected - if (!client.connected() && (currentMillis > (lastMillis + RECONNECT_DELAY))) { - lastMillis = currentMillis; -#ifdef DEBUG - Serial.print("Attempting MQTT connection..."); -#endif - // Attempt to connect - //char clientId[128]; - //snprintf(clientId, 127, "esp%s", WiFi.macAddress().c_str()); - if (client.connect(configBlock.mqttClientId, configBlock.mqttUser, configBlock.mqttPass)) { -#ifdef DEBUG - Serial.println("connected"); -#endif - client.setCallback(callback); - - // Once connected, publish an announcement... - client.publish(configBlock.mqttDebugTopic, "hello world"); - client.publish(configBlock.mqttDebugTopic, WiFi.localIP().toString().c_str()); - - client.subscribe(configBlock.mqttTopicColorCommand); - client.subscribe(configBlock.mqttTopicCommand); - } else { -#ifdef DEBUG - Serial.print("failed, rc="); - Serial.print(client.state()); - Serial.println(" try again in 5 seconds"); -#endif - } - } -} - void setupApplication() { - client.setServer(configBlock.mqttBroker, configBlock.mqttPort); + mqttSetup(); #ifdef WS2811 - FastLED.addLeds(leds, NUM_OF_LEDs); + FastLED.addLeds(leds, NUM_OF_LEDs); #endif #ifdef PL9823 pixels.begin(); for (uint8_t i = 0; i < NUM_OF_LEDs; i++) { - pixels.setPixelColor(i, pixels.Color(0,0,0)); + pixels.setPixelColor(i, pixels.Color(0,0,0)); } - pixels.show(); + pixels.show(); #endif } void loopApplication() { - if (!client.connected()) { - reconnect(); - } else { - client.loop(); - } + mqttLoop(); } diff --git a/defines.h b/defines.h index 245fca5..18542fb 100644 --- a/defines.h +++ b/defines.h @@ -43,6 +43,5 @@ #define NUM_OF_LEDs 16 -#define RECONNECT_DELAY 5000 #endif /* DEFINES_H_ */ diff --git a/esp8266mqttboilerplate b/esp8266mqttboilerplate new file mode 160000 index 0000000..1dca68d --- /dev/null +++ b/esp8266mqttboilerplate @@ -0,0 +1 @@ +Subproject commit 1dca68dbc1c18f2f0b4669579e07adafa6bfba4a