From 6d3692a01e7473a9f447a47dd483fcf5139830d8 Mon Sep 17 00:00:00 2001 From: Wolfgang Hottgenroth Date: Sat, 6 Apr 2019 22:49:23 +0200 Subject: [PATCH] well, seems to be done --- ConfigGenerator/configGen.py | 5 +- configuration.cpp | 70 +------------------ configuration.h | 3 - productionMode.cpp | 132 +++++++++++++++++++++++++++++------ 4 files changed, 113 insertions(+), 97 deletions(-) diff --git a/ConfigGenerator/configGen.py b/ConfigGenerator/configGen.py index 4efc30d..d4f79ad 100644 --- a/ConfigGenerator/configGen.py +++ b/ConfigGenerator/configGen.py @@ -17,15 +17,12 @@ configItems = [ {"label":"MQTT ClientId", "key":"mqttClientId", "type":"C", "length":32, "default":"RgbLed1"}, {"label":"MQTT Port", "key":"mqttPort", "type":"I", "default":8883}, {"label":"MQTT Topic Command", "key":"mqttTopicCommand", "type":"C", "length":64, "default":"IoT/RgbLed1/Command"}, - {"label":"MQTT Topic Color to all LEDs", "key":"mqttTopicColorAllLed", "type":"C", "length":64, "default":"IoT/RgbLed1/ColorAllLed"}, - {"label":"MQTT Topic Color to one LED", "key":"mqttTopicColorOneLed", "type":"C", "length":64, "default":"IoT/RgbLed1/ColorLed"}, - {"label":"MQTT Topic Status to all LED", "key":"mqttTopicStatusAllLed", "type":"C", "length":64, "default":"IoT/RgbLed1/StatusAllLed"}, {"label":"MQTT DebugTopic", "key":"mqttDebugTopic", "type":"C", "length":64, "default":"IoT/RgbLed1/Debug"}, {"label":"DebugMode", "key":"debugMode", "type":"I", "default":0} ] -magic = 0xC0DE0004 +magic = 0xC0DE0005 appName = "ESP8266 based TwoLedSignal" confWifiSsid = "espconfig" diff --git a/configuration.cpp b/configuration.cpp index 3de632e..fcb6f01 100644 --- a/configuration.cpp +++ b/configuration.cpp @@ -9,7 +9,7 @@ tConfigBlock configBlock; -const uint32_t MAGIC = 3235774468; +const uint32_t MAGIC = 3235774470; const char* CONFIG_SSID = "espconfig"; extern ESP8266WebServer webServer; @@ -37,9 +37,6 @@ void configServeIndex() { strcpy(configBlock.mqttClientId, "RgbLed1"); configBlock.mqttPort = 8883; strcpy(configBlock.mqttTopicCommand, "IoT/RgbLed1/Command"); - strcpy(configBlock.mqttTopicColorAllLed, "IoT/RgbLed1/ColorAllLed"); - strcpy(configBlock.mqttTopicColorOneLed, "IoT/RgbLed1/ColorLed"); - strcpy(configBlock.mqttTopicStatusAllLed, "IoT/RgbLed1/StatusAllLed"); strcpy(configBlock.mqttDebugTopic, "IoT/RgbLed1/Debug"); configBlock.debugMode = 0; } @@ -211,51 +208,6 @@ void configServeIndex() { buffer += configBlock.mqttTopicCommand; buffer += "\""; - buffer += - " />" - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " @@ -334,12 +286,6 @@ void configServeGetConfiguration() { configBlock.mqttPort = atoi(arg.c_str()); arg = webServer.arg("mqttTopicCommand"); strcpy(configBlock.mqttTopicCommand, arg.c_str()); - arg = webServer.arg("mqttTopicColorAllLed"); - strcpy(configBlock.mqttTopicColorAllLed, arg.c_str()); - arg = webServer.arg("mqttTopicColorOneLed"); - strcpy(configBlock.mqttTopicColorOneLed, arg.c_str()); - arg = webServer.arg("mqttTopicStatusAllLed"); - strcpy(configBlock.mqttTopicStatusAllLed, arg.c_str()); arg = webServer.arg("mqttDebugTopic"); strcpy(configBlock.mqttDebugTopic, arg.c_str()); arg = webServer.arg("debugMode"); @@ -349,7 +295,7 @@ void configServeGetConfiguration() { showConfiguration(); - EEPROM.begin(1024); + EEPROM.begin(512); EEPROM.put(EEPROM_ADDR, configBlock); EEPROM.commit(); @@ -408,18 +354,6 @@ void showConfiguration() { Serial.print(configBlock.mqttTopicCommand); Serial.println(">"); - Serial.print("mqttTopicColorAllLed = <"); - Serial.print(configBlock.mqttTopicColorAllLed); - Serial.println(">"); - - Serial.print("mqttTopicColorOneLed = <"); - Serial.print(configBlock.mqttTopicColorOneLed); - Serial.println(">"); - - Serial.print("mqttTopicStatusAllLed = <"); - Serial.print(configBlock.mqttTopicStatusAllLed); - Serial.println(">"); - Serial.print("mqttDebugTopic = <"); Serial.print(configBlock.mqttDebugTopic); Serial.println(">"); diff --git a/configuration.h b/configuration.h index f62c412..987ac73 100644 --- a/configuration.h +++ b/configuration.h @@ -10,9 +10,6 @@ typedef struct { char mqttClientId[32]; uint32_t mqttPort; char mqttTopicCommand[64]; - char mqttTopicColorAllLed[64]; - char mqttTopicColorOneLed[64]; - char mqttTopicStatusAllLed[64]; char mqttDebugTopic[64]; uint32_t debugMode; } tConfigBlock; diff --git a/productionMode.cpp b/productionMode.cpp index 0a79e14..83b1a25 100644 --- a/productionMode.cpp +++ b/productionMode.cpp @@ -12,6 +12,7 @@ #include #include +#include #include #include #include @@ -67,6 +68,45 @@ void setup_wifi() { } +static CRGB evaluationColorWord(char* cmd) { + uint8_t red = 0, green = 0, blue = 0; + if (! strcmp(cmd, "on")) { + red = 255; + green = 255; + blue = 255; + } else if (! strcmp(cmd, "off")) { + red = 0; + green = 0; + blue = 0; + } else if (! strcmp(cmd, "red")) { + red = 255; + green = 0; + blue = 0; + } else if (! strcmp(cmd, "green")) { + red = 0; + green = 255; + blue = 0; + } else if (! strcmp(cmd, "blue")) { + red = 0; + green = 0; + blue = 255; + } else if (! strcmp(cmd, "purple")) { + red = 255; + green = 0; + blue = 255; + } else if (! strcmp(cmd, "yellow")) { + red = 255; + green = 255; + blue = 0; + } + + CRGB res; + res.r = red; + res.g = green; + res.b = blue; + return res; +} + void callback(char* topic, byte* payload, unsigned int length) { @@ -90,25 +130,79 @@ void callback(char* topic, byte* payload, unsigned int length) { if (! strcmp(topic, configBlock.mqttTopicCommand)) { - } + char *inbuf = buffer; + char *tk = NULL; + uint8_t tokenCnt = 0; + #define MAX_TOKENS 5 + char *tokens[MAX_TOKENS]; + do { + tk = strtok(inbuf, " "); + inbuf = NULL; + tokens[tokenCnt] = tk; + if (tk) { + tokenCnt++; + Serial.print("TokenCnt: "); + Serial.print(tokenCnt); + Serial.println(); + } + } while (tk); - if (! strcmp(topic, configBlock.mqttTopicColorAllLed)) { - } - - if (! strncmp(topic, configBlock.mqttTopicColorOneLed, strlen(configBlock.mqttTopicColorOneLed))) { - - } - - if (! strcmp(topic, configBlock.mqttTopicStatusAllLed)) { - uint8_t v = ! strcmp(buffer, "on") ? 255 : 0; - - for (uint8_t i = 0; i < NUM_OF_LEDs; i++) { - leds[i].r = v; - leds[i].g = v; - leds[i].b = v; + for (uint8_t i = 0; i < tokenCnt; i++) { + Serial.print("Token "); + Serial.print(i); + Serial.print(": "); + Serial.println(tokens[i]); } - FastLED.show(); + + 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++) { + leds[i] = colors; + } + FastLED.show(); + 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]); + leds[n] = colors; + FastLED.show(); + } + 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++) { + leds[i].r = red; + leds[i].g = green; + leds[i].b = blue; + } + FastLED.show(); + 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); + leds[n].r = red; + leds[n].g = green; + leds[n].b = blue; + FastLED.show(); + } + break; + } + } + } } @@ -134,12 +228,6 @@ void reconnect() { client.publish(configBlock.mqttDebugTopic, WiFi.localIP().toString().c_str()); client.subscribe(configBlock.mqttTopicCommand); - client.subscribe(configBlock.mqttTopicColorAllLed); - client.subscribe(configBlock.mqttTopicStatusAllLed); - char buf[66]; - strncpy(buf, configBlock.mqttTopicColorOneLed, sizeof(buf)); - strncat(buf, "/#", sizeof(buf)); - client.subscribe(buf); } else { #ifdef DEBUG Serial.print("failed, rc=");