new command tested and config mode switch fixed

This commit is contained in:
Wolfgang Hottgenroth 2019-04-10 12:23:25 +02:00
parent a45806aacf
commit 15e33778a7
Signed by: wn
GPG Key ID: B586EAFCDF2F65F4
6 changed files with 144 additions and 96 deletions

View File

@ -184,10 +184,10 @@ environment/project/io.sloeber.core.toolChain.release.1583508753/A.EXTRA.TIME.DT
environment/project/io.sloeber.core.toolChain.release.1583508753/A.EXTRA.TIME.DTS/value=3600 environment/project/io.sloeber.core.toolChain.release.1583508753/A.EXTRA.TIME.DTS/value=3600
environment/project/io.sloeber.core.toolChain.release.1583508753/A.EXTRA.TIME.LOCAL/delimiter=\: environment/project/io.sloeber.core.toolChain.release.1583508753/A.EXTRA.TIME.LOCAL/delimiter=\:
environment/project/io.sloeber.core.toolChain.release.1583508753/A.EXTRA.TIME.LOCAL/operation=replace environment/project/io.sloeber.core.toolChain.release.1583508753/A.EXTRA.TIME.LOCAL/operation=replace
environment/project/io.sloeber.core.toolChain.release.1583508753/A.EXTRA.TIME.LOCAL/value=1554657771 environment/project/io.sloeber.core.toolChain.release.1583508753/A.EXTRA.TIME.LOCAL/value=1554895981
environment/project/io.sloeber.core.toolChain.release.1583508753/A.EXTRA.TIME.UTC/delimiter=\: environment/project/io.sloeber.core.toolChain.release.1583508753/A.EXTRA.TIME.UTC/delimiter=\:
environment/project/io.sloeber.core.toolChain.release.1583508753/A.EXTRA.TIME.UTC/operation=replace environment/project/io.sloeber.core.toolChain.release.1583508753/A.EXTRA.TIME.UTC/operation=replace
environment/project/io.sloeber.core.toolChain.release.1583508753/A.EXTRA.TIME.UTC/value=1554650571 environment/project/io.sloeber.core.toolChain.release.1583508753/A.EXTRA.TIME.UTC/value=1554888781
environment/project/io.sloeber.core.toolChain.release.1583508753/A.EXTRA.TIME.ZONE/delimiter=\: environment/project/io.sloeber.core.toolChain.release.1583508753/A.EXTRA.TIME.ZONE/delimiter=\:
environment/project/io.sloeber.core.toolChain.release.1583508753/A.EXTRA.TIME.ZONE/operation=replace environment/project/io.sloeber.core.toolChain.release.1583508753/A.EXTRA.TIME.ZONE/operation=replace
environment/project/io.sloeber.core.toolChain.release.1583508753/A.EXTRA.TIME.ZONE/value=3600 environment/project/io.sloeber.core.toolChain.release.1583508753/A.EXTRA.TIME.ZONE/value=3600
@ -466,10 +466,10 @@ environment/project/io.sloeber.core.toolChain.release.1583508753/JANTJE.BOARD_NA
environment/project/io.sloeber.core.toolChain.release.1583508753/JANTJE.BOARD_NAME/value=NodeMCU 1.0 (ESP-12E Module) environment/project/io.sloeber.core.toolChain.release.1583508753/JANTJE.BOARD_NAME/value=NodeMCU 1.0 (ESP-12E Module)
environment/project/io.sloeber.core.toolChain.release.1583508753/JANTJE.COM_PORT/delimiter=\: environment/project/io.sloeber.core.toolChain.release.1583508753/JANTJE.COM_PORT/delimiter=\:
environment/project/io.sloeber.core.toolChain.release.1583508753/JANTJE.COM_PORT/operation=replace environment/project/io.sloeber.core.toolChain.release.1583508753/JANTJE.COM_PORT/operation=replace
environment/project/io.sloeber.core.toolChain.release.1583508753/JANTJE.COM_PORT/value=/dev/ttyUSB5 environment/project/io.sloeber.core.toolChain.release.1583508753/JANTJE.COM_PORT/value=/dev/ttyUSB4
environment/project/io.sloeber.core.toolChain.release.1583508753/JANTJE.ECLIPSE_LOCATION/delimiter=\: environment/project/io.sloeber.core.toolChain.release.1583508753/JANTJE.ECLIPSE_LOCATION/delimiter=\:
environment/project/io.sloeber.core.toolChain.release.1583508753/JANTJE.ECLIPSE_LOCATION/operation=replace environment/project/io.sloeber.core.toolChain.release.1583508753/JANTJE.ECLIPSE_LOCATION/operation=replace
environment/project/io.sloeber.core.toolChain.release.1583508753/JANTJE.ECLIPSE_LOCATION/value=${eclipse_home}///// environment/project/io.sloeber.core.toolChain.release.1583508753/JANTJE.ECLIPSE_LOCATION/value=${eclipse_home}//////////
environment/project/io.sloeber.core.toolChain.release.1583508753/JANTJE.EXTRA.ALL/delimiter=\: environment/project/io.sloeber.core.toolChain.release.1583508753/JANTJE.EXTRA.ALL/delimiter=\:
environment/project/io.sloeber.core.toolChain.release.1583508753/JANTJE.EXTRA.ALL/operation=replace environment/project/io.sloeber.core.toolChain.release.1583508753/JANTJE.EXTRA.ALL/operation=replace
environment/project/io.sloeber.core.toolChain.release.1583508753/JANTJE.EXTRA.ALL/value= environment/project/io.sloeber.core.toolChain.release.1583508753/JANTJE.EXTRA.ALL/value=

View File

@ -34,6 +34,9 @@ void setup() {
Serial.print("Magic: "); Serial.print("Magic: ");
Serial.println(configBlock.magic); Serial.println(configBlock.magic);
Serial.print("ConfigSwitch: ");
Serial.println(digitalRead(CONFIG_SWITCH));
configMode = ((LOW == digitalRead(CONFIG_SWITCH)) || (configBlock.magic != MAGIC)); configMode = ((LOW == digitalRead(CONFIG_SWITCH)) || (configBlock.magic != MAGIC));

View File

@ -36,6 +36,7 @@ void configServeIndex() {
strcpy(configBlock.mqttPass, "geheim123"); strcpy(configBlock.mqttPass, "geheim123");
strcpy(configBlock.mqttClientId, "RgbLed1"); strcpy(configBlock.mqttClientId, "RgbLed1");
configBlock.mqttPort = 8883; configBlock.mqttPort = 8883;
strcpy(configBlock.mqttTopicColorCommand, "IoT/RgbLed1/ColorCommand");
strcpy(configBlock.mqttTopicCommand, "IoT/RgbLed1/Command"); strcpy(configBlock.mqttTopicCommand, "IoT/RgbLed1/Command");
strcpy(configBlock.mqttDebugTopic, "IoT/RgbLed1/Debug"); strcpy(configBlock.mqttDebugTopic, "IoT/RgbLed1/Debug");
configBlock.debugMode = 0; configBlock.debugMode = 0;
@ -193,6 +194,21 @@ void configServeIndex() {
buffer += configBlock.mqttPort; buffer += configBlock.mqttPort;
buffer += "\""; buffer += "\"";
buffer +=
" />"
" </td>"
" </tr>"
" <tr>"
" <td>"
" <label for\"mqttTopicColorCommand\">MQTT Topic Color Command</label>"
" </td><td>"
" <input type=\"text\" name=\"mqttTopicColorCommand\" id=\"mqttTopicColorCommand\" ";
buffer += " size=\"64\" ";
buffer += " value=\"";
buffer += configBlock.mqttTopicColorCommand;
buffer += "\"";
buffer += buffer +=
" />" " />"
" </td>" " </td>"
@ -284,6 +300,8 @@ void configServeGetConfiguration() {
strcpy(configBlock.mqttClientId, arg.c_str()); strcpy(configBlock.mqttClientId, arg.c_str());
arg = webServer.arg("mqttPort"); arg = webServer.arg("mqttPort");
configBlock.mqttPort = atoi(arg.c_str()); configBlock.mqttPort = atoi(arg.c_str());
arg = webServer.arg("mqttTopicColorCommand");
strcpy(configBlock.mqttTopicColorCommand, arg.c_str());
arg = webServer.arg("mqttTopicCommand"); arg = webServer.arg("mqttTopicCommand");
strcpy(configBlock.mqttTopicCommand, arg.c_str()); strcpy(configBlock.mqttTopicCommand, arg.c_str());
arg = webServer.arg("mqttDebugTopic"); arg = webServer.arg("mqttDebugTopic");
@ -350,6 +368,10 @@ void showConfiguration() {
Serial.print(configBlock.mqttPort); Serial.print(configBlock.mqttPort);
Serial.println(">"); Serial.println(">");
Serial.print("mqttTopicColorCommand = <");
Serial.print(configBlock.mqttTopicColorCommand);
Serial.println(">");
Serial.print("mqttTopicCommand = <"); Serial.print("mqttTopicCommand = <");
Serial.print(configBlock.mqttTopicCommand); Serial.print(configBlock.mqttTopicCommand);
Serial.println(">"); Serial.println(">");

View File

@ -9,6 +9,7 @@ typedef struct {
char mqttPass[32]; char mqttPass[32];
char mqttClientId[32]; char mqttClientId[32];
uint32_t mqttPort; uint32_t mqttPort;
char mqttTopicColorCommand[64];
char mqttTopicCommand[64]; char mqttTopicCommand[64];
char mqttDebugTopic[64]; char mqttDebugTopic[64];
uint32_t debugMode; uint32_t debugMode;

View File

@ -12,9 +12,9 @@
#define EEPROM_ADDR 0 #define EEPROM_ADDR 0
#define CONFIG_SWITCH 2 #define CONFIG_SWITCH 4 // Arduino numbering
#define PIXEL_PIN 1 #define PIXEL_PIN 1 // NODEMCU numbering
#define NUM_OF_LEDs 64 #define NUM_OF_LEDs 64
#endif /* DEFINES_H_ */ #endif /* DEFINES_H_ */

View File

@ -72,7 +72,7 @@ void setup_wifi() {
static CRGB evaluationColorWord(char* cmd) { static CRGB evaluationColorWord(char* cmd) {
uint8_t red = 0, green = 0, blue = 0; uint8_t red = 0, green = 0, blue = 0;
if ((! strcmp(cmd, "on")) || (! strcmp(cmd, "white")) { if ((! strcmp(cmd, "on")) || (! strcmp(cmd, "white"))) {
red = 255; red = 255;
green = 255; green = 255;
blue = 255; blue = 255;
@ -112,106 +112,128 @@ static CRGB evaluationColorWord(char* cmd) {
void callback(char* topic, byte* payload, unsigned int length) { void callback(char* topic, byte* payload, unsigned int length) {
const uint8_t BUFSIZE = 128; const uint8_t BUFSIZE = 128;
if ((length + 1) >= BUFSIZE) { // 1 for terminating NUL if ((length + 1) >= BUFSIZE) { // 1 for terminating NUL
#ifdef DEBUG #ifdef DEBUG
Serial.println("Received message too long, ignore it"); Serial.println("Received message too long, ignore it");
#endif #endif
} else { } else {
char buffer[BUFSIZE]; char buffer[BUFSIZE];
memcpy(buffer, payload, length); memcpy(buffer, payload, length);
*(buffer + length) = 0; *(buffer + length) = 0;
#ifdef DEBUG #ifdef DEBUG
Serial.print("Received message: "); Serial.print("Received message: ");
Serial.print(length); Serial.print(length);
Serial.print(" "); Serial.print(" ");
Serial.print(topic); Serial.print(topic);
Serial.print(" "); Serial.print(" ");
Serial.println(buffer); Serial.println(buffer);
#endif #endif
char *inbuf = buffer; char *inbuf = buffer;
char *tk = NULL; char *tk = NULL;
uint8_t tokenCnt = 0; uint8_t tokenCnt = 0;
char *tokens[MAX_TOKENS]; char *tokens[MAX_TOKENS];
do { do {
if (tokenCnt >= MAX_TOKENS) { if (tokenCnt >= MAX_TOKENS) {
break; break;
} }
tk = strtok(inbuf, " "); tk = strtok(inbuf, " ");
inbuf = NULL; inbuf = NULL;
tokens[tokenCnt] = tk; tokens[tokenCnt] = tk;
if (tk) { if (tk) {
tokenCnt++; tokenCnt++;
Serial.print("TokenCnt: "); Serial.print("TokenCnt: ");
Serial.print(tokenCnt); Serial.print(tokenCnt);
Serial.println(); Serial.println();
} }
} while (tk); } while (tk);
for (uint8_t i = 0; i < tokenCnt; i++) { for (uint8_t i = 0; i < tokenCnt; i++) {
Serial.print("Token "); Serial.print("Token ");
Serial.print(i); Serial.print(i);
Serial.print(": "); Serial.print(": ");
Serial.println(tokens[i]); Serial.println(tokens[i]);
} }
if (! strcmp(topic, configBlock.mqttTopicCommand)) { if (! strcmp(topic, configBlock.mqttTopicCommand)) {
int32_t n; 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++) {
leds[i].r = b;
leds[i].g = b;
leds[i].b = b;
}
FastLED.show();
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) {
leds[n] = b;
FastLED.show();
}
break;
}
} else if (! strcmp(topic, configBlock.mqttTopicColorCommand)) { } else if (! strcmp(topic, configBlock.mqttTopicColorCommand)) {
int32_t n, red, green, blue; int32_t n, red, green, blue;
CRGB colors; CRGB colors;
switch (tokenCnt) { switch (tokenCnt) {
case 1: case 1:
// on, off, color word for all LEDs // on, off, color word for all LEDs
colors = evaluationColorWord(tokens[0]); colors = evaluationColorWord(tokens[0]);
for (uint8_t i = 0; i < NUM_OF_LEDs; i++) { for (uint8_t i = 0; i < NUM_OF_LEDs; i++) {
leds[i] = colors; leds[i] = colors;
} }
FastLED.show(); FastLED.show();
break; break;
case 2: case 2:
// token0 = LED number, token1 = color // token0 = LED number, token1 = color
n = strtol(tokens[0], NULL, 10); n = strtol(tokens[0], NULL, 10);
if (n >= 0 && n < NUM_OF_LEDs) { if (n >= 0 && n < NUM_OF_LEDs) {
colors = evaluationColorWord(tokens[1]); colors = evaluationColorWord(tokens[1]);
leds[n] = colors; leds[n] = colors;
FastLED.show(); FastLED.show();
} }
break; break;
case 3: case 3:
// token0 = red, token1 = green, token2 = blue // token0 = red, token1 = green, token2 = blue
red = strtol(tokens[0], NULL, 10); red = strtol(tokens[0], NULL, 10);
green = strtol(tokens[1], NULL, 10); green = strtol(tokens[1], NULL, 10);
blue = strtol(tokens[2], NULL, 10); blue = strtol(tokens[2], NULL, 10);
for (uint8_t i = 0; i < NUM_OF_LEDs; i++) { for (uint8_t i = 0; i < NUM_OF_LEDs; i++) {
leds[i].r = red; leds[i].r = red;
leds[i].g = green; leds[i].g = green;
leds[i].b = blue; leds[i].b = blue;
} }
FastLED.show(); FastLED.show();
break; break;
case 4: case 4:
// token0 = LED number, token1 = red, token2 = green, token3 = blue // token0 = LED number, token1 = red, token2 = green, token3 = blue
n = strtol(tokens[0], NULL, 10); n = strtol(tokens[0], NULL, 10);
if (n >= 0 && n < NUM_OF_LEDs) { if (n >= 0 && n < NUM_OF_LEDs) {
red = strtol(tokens[1], NULL, 10); red = strtol(tokens[1], NULL, 10);
green = strtol(tokens[2], NULL, 10); green = strtol(tokens[2], NULL, 10);
blue = strtol(tokens[3], NULL, 10); blue = strtol(tokens[3], NULL, 10);
leds[n].r = red; leds[n].r = red;
leds[n].g = green; leds[n].g = green;
leds[n].b = blue; leds[n].b = blue;
FastLED.show(); FastLED.show();
} }
break; break;
} }
} }
} }
} }
@ -265,7 +287,7 @@ void setupProduction() {
setup_wifi(); setup_wifi();
client.setServer(configBlock.mqttBroker, configBlock.mqttPort); client.setServer(configBlock.mqttBroker, configBlock.mqttPort);
FastLED.addLeds<NEOPIXEL, PIXEL_PIN>(leds, NUM_OF_LEDs); FastLED.addLeds<NEOPIXEL, PIXEL_PIN>(leds, NUM_OF_LEDs);
} }
@ -320,7 +342,7 @@ void loop() {
delay(2000); delay(2000);
} }
*/ */