well, seems to be done
This commit is contained in:
parent
cef2749111
commit
6d3692a01e
@ -17,15 +17,12 @@ configItems = [
|
|||||||
{"label":"MQTT ClientId", "key":"mqttClientId", "type":"C", "length":32, "default":"RgbLed1"},
|
{"label":"MQTT ClientId", "key":"mqttClientId", "type":"C", "length":32, "default":"RgbLed1"},
|
||||||
{"label":"MQTT Port", "key":"mqttPort", "type":"I", "default":8883},
|
{"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 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":"MQTT DebugTopic", "key":"mqttDebugTopic", "type":"C", "length":64, "default":"IoT/RgbLed1/Debug"},
|
||||||
{"label":"DebugMode", "key":"debugMode", "type":"I", "default":0}
|
{"label":"DebugMode", "key":"debugMode", "type":"I", "default":0}
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
magic = 0xC0DE0004
|
magic = 0xC0DE0005
|
||||||
appName = "ESP8266 based TwoLedSignal"
|
appName = "ESP8266 based TwoLedSignal"
|
||||||
confWifiSsid = "espconfig"
|
confWifiSsid = "espconfig"
|
||||||
|
|
||||||
|
@ -9,7 +9,7 @@
|
|||||||
|
|
||||||
|
|
||||||
tConfigBlock configBlock;
|
tConfigBlock configBlock;
|
||||||
const uint32_t MAGIC = 3235774468;
|
const uint32_t MAGIC = 3235774470;
|
||||||
const char* CONFIG_SSID = "espconfig";
|
const char* CONFIG_SSID = "espconfig";
|
||||||
extern ESP8266WebServer webServer;
|
extern ESP8266WebServer webServer;
|
||||||
|
|
||||||
@ -37,9 +37,6 @@ void configServeIndex() {
|
|||||||
strcpy(configBlock.mqttClientId, "RgbLed1");
|
strcpy(configBlock.mqttClientId, "RgbLed1");
|
||||||
configBlock.mqttPort = 8883;
|
configBlock.mqttPort = 8883;
|
||||||
strcpy(configBlock.mqttTopicCommand, "IoT/RgbLed1/Command");
|
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");
|
strcpy(configBlock.mqttDebugTopic, "IoT/RgbLed1/Debug");
|
||||||
configBlock.debugMode = 0;
|
configBlock.debugMode = 0;
|
||||||
}
|
}
|
||||||
@ -211,51 +208,6 @@ void configServeIndex() {
|
|||||||
buffer += configBlock.mqttTopicCommand;
|
buffer += configBlock.mqttTopicCommand;
|
||||||
buffer += "\"";
|
buffer += "\"";
|
||||||
|
|
||||||
buffer +=
|
|
||||||
" />"
|
|
||||||
" </td>"
|
|
||||||
" </tr>"
|
|
||||||
" <tr>"
|
|
||||||
" <td>"
|
|
||||||
" <label for\"mqttTopicColorAllLed\">MQTT Topic Color to all LEDs</label>"
|
|
||||||
" </td><td>"
|
|
||||||
" <input type=\"text\" name=\"mqttTopicColorAllLed\" id=\"mqttTopicColorAllLed\" ";
|
|
||||||
|
|
||||||
buffer += " size=\"64\" ";
|
|
||||||
buffer += " value=\"";
|
|
||||||
buffer += configBlock.mqttTopicColorAllLed;
|
|
||||||
buffer += "\"";
|
|
||||||
|
|
||||||
buffer +=
|
|
||||||
" />"
|
|
||||||
" </td>"
|
|
||||||
" </tr>"
|
|
||||||
" <tr>"
|
|
||||||
" <td>"
|
|
||||||
" <label for\"mqttTopicColorOneLed\">MQTT Topic Color to one LED</label>"
|
|
||||||
" </td><td>"
|
|
||||||
" <input type=\"text\" name=\"mqttTopicColorOneLed\" id=\"mqttTopicColorOneLed\" ";
|
|
||||||
|
|
||||||
buffer += " size=\"64\" ";
|
|
||||||
buffer += " value=\"";
|
|
||||||
buffer += configBlock.mqttTopicColorOneLed;
|
|
||||||
buffer += "\"";
|
|
||||||
|
|
||||||
buffer +=
|
|
||||||
" />"
|
|
||||||
" </td>"
|
|
||||||
" </tr>"
|
|
||||||
" <tr>"
|
|
||||||
" <td>"
|
|
||||||
" <label for\"mqttTopicStatusAllLed\">MQTT Topic Status to all LED</label>"
|
|
||||||
" </td><td>"
|
|
||||||
" <input type=\"text\" name=\"mqttTopicStatusAllLed\" id=\"mqttTopicStatusAllLed\" ";
|
|
||||||
|
|
||||||
buffer += " size=\"64\" ";
|
|
||||||
buffer += " value=\"";
|
|
||||||
buffer += configBlock.mqttTopicStatusAllLed;
|
|
||||||
buffer += "\"";
|
|
||||||
|
|
||||||
buffer +=
|
buffer +=
|
||||||
" />"
|
" />"
|
||||||
" </td>"
|
" </td>"
|
||||||
@ -334,12 +286,6 @@ void configServeGetConfiguration() {
|
|||||||
configBlock.mqttPort = atoi(arg.c_str());
|
configBlock.mqttPort = atoi(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("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");
|
arg = webServer.arg("mqttDebugTopic");
|
||||||
strcpy(configBlock.mqttDebugTopic, arg.c_str());
|
strcpy(configBlock.mqttDebugTopic, arg.c_str());
|
||||||
arg = webServer.arg("debugMode");
|
arg = webServer.arg("debugMode");
|
||||||
@ -349,7 +295,7 @@ void configServeGetConfiguration() {
|
|||||||
|
|
||||||
showConfiguration();
|
showConfiguration();
|
||||||
|
|
||||||
EEPROM.begin(1024);
|
EEPROM.begin(512);
|
||||||
EEPROM.put(EEPROM_ADDR, configBlock);
|
EEPROM.put(EEPROM_ADDR, configBlock);
|
||||||
EEPROM.commit();
|
EEPROM.commit();
|
||||||
|
|
||||||
@ -408,18 +354,6 @@ void showConfiguration() {
|
|||||||
Serial.print(configBlock.mqttTopicCommand);
|
Serial.print(configBlock.mqttTopicCommand);
|
||||||
Serial.println(">");
|
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("mqttDebugTopic = <");
|
||||||
Serial.print(configBlock.mqttDebugTopic);
|
Serial.print(configBlock.mqttDebugTopic);
|
||||||
Serial.println(">");
|
Serial.println(">");
|
||||||
|
@ -10,9 +10,6 @@ typedef struct {
|
|||||||
char mqttClientId[32];
|
char mqttClientId[32];
|
||||||
uint32_t mqttPort;
|
uint32_t mqttPort;
|
||||||
char mqttTopicCommand[64];
|
char mqttTopicCommand[64];
|
||||||
char mqttTopicColorAllLed[64];
|
|
||||||
char mqttTopicColorOneLed[64];
|
|
||||||
char mqttTopicStatusAllLed[64];
|
|
||||||
char mqttDebugTopic[64];
|
char mqttDebugTopic[64];
|
||||||
uint32_t debugMode;
|
uint32_t debugMode;
|
||||||
} tConfigBlock;
|
} tConfigBlock;
|
||||||
|
@ -12,6 +12,7 @@
|
|||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
#include <string.h>
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <ESP8266WiFi.h>
|
#include <ESP8266WiFi.h>
|
||||||
#include <ESP8266WebServer.h>
|
#include <ESP8266WebServer.h>
|
||||||
@ -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) {
|
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)) {
|
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)) {
|
for (uint8_t i = 0; i < tokenCnt; i++) {
|
||||||
}
|
Serial.print("Token ");
|
||||||
|
Serial.print(i);
|
||||||
if (! strncmp(topic, configBlock.mqttTopicColorOneLed, strlen(configBlock.mqttTopicColorOneLed))) {
|
Serial.print(": ");
|
||||||
|
Serial.println(tokens[i]);
|
||||||
}
|
|
||||||
|
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
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.publish(configBlock.mqttDebugTopic, WiFi.localIP().toString().c_str());
|
||||||
|
|
||||||
client.subscribe(configBlock.mqttTopicCommand);
|
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 {
|
} else {
|
||||||
#ifdef DEBUG
|
#ifdef DEBUG
|
||||||
Serial.print("failed, rc=");
|
Serial.print("failed, rc=");
|
||||||
|
Loading…
x
Reference in New Issue
Block a user