Compare commits
5 Commits
NonBlockin
...
colorFlowE
Author | SHA1 | Date | |
---|---|---|---|
bfc359e554
|
|||
6053ed9bad
|
|||
a9e86a3393
|
|||
68f6e5661b
|
|||
14a0251806
|
@ -29,6 +29,7 @@
|
|||||||
<listOptionValue builtIn="false" value=""${workspace_loc:/rgbled/libraries/ESP8266WiFi/src}""/>
|
<listOptionValue builtIn="false" value=""${workspace_loc:/rgbled/libraries/ESP8266WiFi/src}""/>
|
||||||
<listOptionValue builtIn="false" value=""${workspace_loc:/rgbled/pubsubclient}""/>
|
<listOptionValue builtIn="false" value=""${workspace_loc:/rgbled/pubsubclient}""/>
|
||||||
<listOptionValue builtIn="false" value=""${workspace_loc:/rgbled/esp8266boilerplate}""/>
|
<listOptionValue builtIn="false" value=""${workspace_loc:/rgbled/esp8266boilerplate}""/>
|
||||||
|
<listOptionValue builtIn="false" value=""${workspace_loc:/rgbled/esp8266mqttboilerplate}""/>
|
||||||
</option>
|
</option>
|
||||||
<inputType id="io.sloeber.compiler.cpp.sketch.input.454285658" name="CPP source files" superClass="io.sloeber.compiler.cpp.sketch.input"/>
|
<inputType id="io.sloeber.compiler.cpp.sketch.input.454285658" name="CPP source files" superClass="io.sloeber.compiler.cpp.sketch.input"/>
|
||||||
</tool>
|
</tool>
|
||||||
@ -44,6 +45,7 @@
|
|||||||
<listOptionValue builtIn="false" value=""${workspace_loc:/rgbled/libraries/ESP8266WiFi/src}""/>
|
<listOptionValue builtIn="false" value=""${workspace_loc:/rgbled/libraries/ESP8266WiFi/src}""/>
|
||||||
<listOptionValue builtIn="false" value=""${workspace_loc:/rgbled/pubsubclient}""/>
|
<listOptionValue builtIn="false" value=""${workspace_loc:/rgbled/pubsubclient}""/>
|
||||||
<listOptionValue builtIn="false" value=""${workspace_loc:/rgbled/esp8266boilerplate}""/>
|
<listOptionValue builtIn="false" value=""${workspace_loc:/rgbled/esp8266boilerplate}""/>
|
||||||
|
<listOptionValue builtIn="false" value=""${workspace_loc:/rgbled/esp8266mqttboilerplate}""/>
|
||||||
</option>
|
</option>
|
||||||
<inputType id="io.sloeber.compiler.c.sketch.input.1857532110" name="C Source Files" superClass="io.sloeber.compiler.c.sketch.input"/>
|
<inputType id="io.sloeber.compiler.c.sketch.input.1857532110" name="C Source Files" superClass="io.sloeber.compiler.c.sketch.input"/>
|
||||||
</tool>
|
</tool>
|
||||||
@ -59,6 +61,7 @@
|
|||||||
<listOptionValue builtIn="false" value=""${workspace_loc:/rgbled/libraries/ESP8266WiFi/src}""/>
|
<listOptionValue builtIn="false" value=""${workspace_loc:/rgbled/libraries/ESP8266WiFi/src}""/>
|
||||||
<listOptionValue builtIn="false" value=""${workspace_loc:/rgbled/pubsubclient}""/>
|
<listOptionValue builtIn="false" value=""${workspace_loc:/rgbled/pubsubclient}""/>
|
||||||
<listOptionValue builtIn="false" value=""${workspace_loc:/rgbled/esp8266boilerplate}""/>
|
<listOptionValue builtIn="false" value=""${workspace_loc:/rgbled/esp8266boilerplate}""/>
|
||||||
|
<listOptionValue builtIn="false" value=""${workspace_loc:/rgbled/esp8266mqttboilerplate}""/>
|
||||||
</option>
|
</option>
|
||||||
<inputType id="io.sloeber.compiler.S.sketch.input.2089597409" name="Assembly source files" superClass="io.sloeber.compiler.S.sketch.input"/>
|
<inputType id="io.sloeber.compiler.S.sketch.input.2089597409" name="Assembly source files" superClass="io.sloeber.compiler.S.sketch.input"/>
|
||||||
</tool>
|
</tool>
|
||||||
|
3
.gitmodules
vendored
3
.gitmodules
vendored
@ -1,3 +1,6 @@
|
|||||||
[submodule "esp8266boilerplate"]
|
[submodule "esp8266boilerplate"]
|
||||||
path = esp8266boilerplate
|
path = esp8266boilerplate
|
||||||
url = ../esp8266boilerplate.git
|
url = ../esp8266boilerplate.git
|
||||||
|
[submodule "esp8266mqttboilerplate"]
|
||||||
|
path = esp8266mqttboilerplate
|
||||||
|
url = ../esp8266mqttboilerplate
|
||||||
|
@ -10,6 +10,7 @@ configItems = [
|
|||||||
{"label":"MQTT Password", "key":"mqttPass", "type":"C", "length":32, "default":"geheim123"},
|
{"label":"MQTT Password", "key":"mqttPass", "type":"C", "length":32, "default":"geheim123"},
|
||||||
{"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 Flow Command", "key":"mqttTopicFlowCommand", "type":"C", "length":64, "default":"IoT/RgbLed1/FlowCommand"},
|
||||||
{"label":"MQTT Topic Color Command", "key":"mqttTopicColorCommand", "type":"C", "length":64, "default":"IoT/RgbLed1/ColorCommand"},
|
{"label":"MQTT Topic Color Command", "key":"mqttTopicColorCommand", "type":"C", "length":64, "default":"IoT/RgbLed1/ColorCommand"},
|
||||||
{"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 DebugTopic", "key":"mqttDebugTopic", "type":"C", "length":64, "default":"IoT/RgbLed1/Debug"},
|
{"label":"MQTT DebugTopic", "key":"mqttDebugTopic", "type":"C", "length":64, "default":"IoT/RgbLed1/Debug"},
|
||||||
|
355
application.cpp
355
application.cpp
@ -9,15 +9,13 @@
|
|||||||
|
|
||||||
#include "defines.h"
|
#include "defines.h"
|
||||||
|
|
||||||
#define MQTT_MAX_PACKET_SIZE 256
|
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <ESP8266WiFi.h>
|
#include <mqttHandling.h>
|
||||||
// #include <ESP8266WebServer.h>
|
|
||||||
#include <PubSubClient.h>
|
|
||||||
|
|
||||||
#ifdef WS2811
|
#ifdef WS2811
|
||||||
#define FASTLED_ESP8266_NODEMCU_PIN_ORDER
|
#define FASTLED_ESP8266_NODEMCU_PIN_ORDER
|
||||||
@ -32,17 +30,6 @@
|
|||||||
#include "configuration.h"
|
#include "configuration.h"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
void callback(char* topic, byte* payload, unsigned int length);
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
WiFiClientSecure espClient;
|
|
||||||
PubSubClient client(espClient);
|
|
||||||
|
|
||||||
|
|
||||||
#ifdef WS2811
|
#ifdef WS2811
|
||||||
CRGB leds[NUM_OF_LEDs];
|
CRGB leds[NUM_OF_LEDs];
|
||||||
#endif
|
#endif
|
||||||
@ -58,6 +45,61 @@ typedef struct {
|
|||||||
Adafruit_NeoPixel pixels(NUM_OF_LEDs, PIXEL_PIN, NEO_RGB + NEO_KHZ400);
|
Adafruit_NeoPixel pixels(NUM_OF_LEDs, PIXEL_PIN, NEO_RGB + NEO_KHZ400);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
bool show = false;
|
||||||
|
|
||||||
|
typedef enum {
|
||||||
|
E_FC_IDLE,
|
||||||
|
E_FC_ABORT,
|
||||||
|
E_FC_FADE_ON,
|
||||||
|
E_FC_FADE_OFF,
|
||||||
|
} e_colorFlowCmds;
|
||||||
|
|
||||||
|
e_colorFlowCmds colorFlowCmd = E_FC_IDLE;
|
||||||
|
|
||||||
|
typedef enum {
|
||||||
|
E_FE_IDLE,
|
||||||
|
E_FE_INCREASE_BRIGHTNESS,
|
||||||
|
E_FE_DECREASE_BRIGHTNESS,
|
||||||
|
} e_colorFlowStates;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
static void colorFlowEngine() {
|
||||||
|
e_colorFlowStates colorFlowState = E_FE_IDLE;
|
||||||
|
|
||||||
|
if (colorFlowCmd == E_FC_ABORT) {
|
||||||
|
colorFlowState = E_FE_IDLE;
|
||||||
|
colorFlowCmd = E_FC_IDLE;
|
||||||
|
}
|
||||||
|
|
||||||
|
switch (colorFlowState) {
|
||||||
|
case E_FE_IDLE:
|
||||||
|
switch (colorFlowCmd) {
|
||||||
|
case E_FC_FADE_ON:
|
||||||
|
colorFlowState = E_FE_INCREASE_BRIGHTNESS;
|
||||||
|
break;
|
||||||
|
case E_FC_FADE_OFF:
|
||||||
|
colorFlowState = E_FE_DECREASE_BRIGHTNESS;
|
||||||
|
break;
|
||||||
|
case E_FC_IDLE:
|
||||||
|
case E_FC_ABORT:
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
colorFlowCmd = E_FC_IDLE;
|
||||||
|
break;
|
||||||
|
case E_FE_INCREASE_BRIGHTNESS:
|
||||||
|
// fade up
|
||||||
|
show = true;
|
||||||
|
break;
|
||||||
|
case E_FE_DECREASE_BRIGHTNESS:
|
||||||
|
// fade down
|
||||||
|
show = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
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;
|
||||||
@ -69,6 +111,10 @@ static CRGB evaluationColorWord(char* cmd) {
|
|||||||
red = 0;
|
red = 0;
|
||||||
green = 0;
|
green = 0;
|
||||||
blue = 0;
|
blue = 0;
|
||||||
|
} else if (! strcmp(cmd, "warmwhite")) {
|
||||||
|
red = 0;
|
||||||
|
green = 0;
|
||||||
|
blue = 0;
|
||||||
} else if (! strcmp(cmd, "red")) {
|
} else if (! strcmp(cmd, "red")) {
|
||||||
red = 255;
|
red = 255;
|
||||||
green = 0;
|
green = 0;
|
||||||
@ -100,144 +146,16 @@ static CRGB evaluationColorWord(char* cmd) {
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
void subscribeApplication() {
|
||||||
void callback(char* topic, byte* payload, unsigned int length) {
|
mqttClient.subscribe(configBlock.mqttTopicColorCommand);
|
||||||
const uint8_t MAX_TOKENS = 5;
|
mqttClient.subscribe(configBlock.mqttTopicCommand);
|
||||||
const uint8_t BUFSIZE = 128;
|
mqttClient.subscribe(configBlock.mqttTopicFlowCommand);
|
||||||
if ((length + 1) >= BUFSIZE) { // 1 for terminating NUL
|
}
|
||||||
#ifdef DEBUG
|
|
||||||
Serial.println("Received message too long, ignore it");
|
|
||||||
#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();
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
} while (tk);
|
|
||||||
|
|
||||||
#ifdef DEBUG
|
static void setColor(int8_t ledNumber, uint8_t red, uint8_t green, uint8_t blue) {
|
||||||
for (uint8_t i = 0; i < tokenCnt; i++) {
|
if (ledNumber == -1) {
|
||||||
Serial.print("Token ");
|
|
||||||
Serial.print(i);
|
|
||||||
Serial.print(": ");
|
|
||||||
Serial.println(tokens[i]);
|
|
||||||
}
|
|
||||||
#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++) {
|
|
||||||
#ifdef WS2811
|
|
||||||
leds[i].r = b;
|
|
||||||
leds[i].g = b;
|
|
||||||
leds[i].b = b;
|
|
||||||
#endif
|
|
||||||
#ifdef PL9823
|
|
||||||
pixels.setPixelColor(i, pixels.Color(b, b, 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;
|
|
||||||
#endif
|
|
||||||
#ifdef PL9823
|
|
||||||
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:
|
|
||||||
// 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++) {
|
for (uint8_t i = 0; i < NUM_OF_LEDs; i++) {
|
||||||
#ifdef WS2811
|
#ifdef WS2811
|
||||||
leds[i].r = red;
|
leds[i].r = red;
|
||||||
@ -248,78 +166,91 @@ void callback(char* topic, byte* payload, unsigned int length) {
|
|||||||
pixels.setPixelColor(i, pixels.Color(red, green, blue));
|
pixels.setPixelColor(i, pixels.Color(red, green, blue));
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
show = true;
|
||||||
|
} else {
|
||||||
|
if (ledNumber >= 0 && ledNumber < NUM_OF_LEDs) {
|
||||||
|
|
||||||
#ifdef WS2811
|
#ifdef WS2811
|
||||||
FastLED.show();
|
leds[ledNumber].r = red;
|
||||||
|
leds[ledNumber].g = green;
|
||||||
|
leds[ledNumber].b = blue;
|
||||||
#endif
|
#endif
|
||||||
#ifdef PL9823
|
#ifdef PL9823
|
||||||
pixels.show();
|
pixels.setPixelColor(ledNumber, pixels.Color(red, green, blue));
|
||||||
#endif
|
#endif
|
||||||
|
show = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
void callbackApplication(char *topic, uint8_t tokenCnt, char **tokens) {
|
||||||
|
if (! strcmp(topic, configBlock.mqttTopicFlowCommand)) {
|
||||||
|
switch (tokenCnt) {
|
||||||
|
case 1:
|
||||||
|
if (! strcmp(tokens[0], "up")) {
|
||||||
|
colorFlowCmd = E_FC_FADE_ON;
|
||||||
|
} else if (! strcmp(tokens[0], "down")) {
|
||||||
|
colorFlowCmd = E_FC_FADE_OFF;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
} else if (! strcmp(topic, configBlock.mqttTopicCommand)) {
|
||||||
|
int32_t n, b;
|
||||||
|
CRGB pseudoColors;
|
||||||
|
switch (tokenCnt) {
|
||||||
|
case 1:
|
||||||
|
// set brightness
|
||||||
|
b = strtol(tokens[0], NULL, 10);
|
||||||
|
setColor(-1, b, b, b);
|
||||||
|
break;
|
||||||
|
case 2:
|
||||||
|
// set brightness for one LED
|
||||||
|
n = strtol(tokens[0], NULL, 10);
|
||||||
|
b = strtol(tokens[1], NULL, 10);
|
||||||
|
setColor(n, b, b, b);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
colorFlowCmd = E_FC_ABORT;
|
||||||
|
} 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]);
|
||||||
|
setColor(-1, colors.r, colors.g, colors.b);
|
||||||
|
break;
|
||||||
|
case 2:
|
||||||
|
// token0 = LED number, token1 = color
|
||||||
|
n = strtol(tokens[0], NULL, 10);
|
||||||
|
colors = evaluationColorWord(tokens[1]);
|
||||||
|
setColor(n, colors.r, colors.g, colors.b);
|
||||||
|
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);
|
||||||
|
setColor(-1, red, green, blue);
|
||||||
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) {
|
|
||||||
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);
|
||||||
#ifdef WS2811
|
setColor(n, red, green, blue);
|
||||||
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;
|
||||||
}
|
}
|
||||||
}
|
colorFlowCmd = E_FC_ABORT;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
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() {
|
void setupApplication() {
|
||||||
client.setServer(configBlock.mqttBroker, configBlock.mqttPort);
|
mqttSetup();
|
||||||
|
|
||||||
#ifdef WS2811
|
#ifdef WS2811
|
||||||
FastLED.addLeds<NEOPIXEL, PIXEL_PIN>(leds, NUM_OF_LEDs);
|
FastLED.addLeds<NEOPIXEL, PIXEL_PIN>(leds, NUM_OF_LEDs);
|
||||||
@ -336,10 +267,18 @@ void setupApplication() {
|
|||||||
|
|
||||||
|
|
||||||
void loopApplication() {
|
void loopApplication() {
|
||||||
if (!client.connected()) {
|
mqttLoop();
|
||||||
reconnect();
|
|
||||||
} else {
|
colorFlowEngine();
|
||||||
client.loop();
|
|
||||||
|
if (show) {
|
||||||
|
show = false;
|
||||||
|
#ifdef WS2811
|
||||||
|
FastLED.show();
|
||||||
|
#endif
|
||||||
|
#ifdef PL9823
|
||||||
|
pixels.show();
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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.mqttTopicFlowCommand, "IoT/RgbLed1/FlowCommand");
|
||||||
strcpy(configBlock.mqttTopicColorCommand, "IoT/RgbLed1/ColorCommand");
|
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");
|
||||||
@ -194,6 +195,21 @@ void configServeIndex() {
|
|||||||
buffer += configBlock.mqttPort;
|
buffer += configBlock.mqttPort;
|
||||||
buffer += "\"";
|
buffer += "\"";
|
||||||
|
|
||||||
|
buffer +=
|
||||||
|
" />"
|
||||||
|
" </td>"
|
||||||
|
" </tr>"
|
||||||
|
" <tr>"
|
||||||
|
" <td>"
|
||||||
|
" <label for\"mqttTopicFlowCommand\">MQTT Topic Flow Command</label>"
|
||||||
|
" </td><td>"
|
||||||
|
" <input type=\"text\" name=\"mqttTopicFlowCommand\" id=\"mqttTopicFlowCommand\" ";
|
||||||
|
|
||||||
|
buffer += " size=\"64\" ";
|
||||||
|
buffer += " value=\"";
|
||||||
|
buffer += configBlock.mqttTopicFlowCommand;
|
||||||
|
buffer += "\"";
|
||||||
|
|
||||||
buffer +=
|
buffer +=
|
||||||
" />"
|
" />"
|
||||||
" </td>"
|
" </td>"
|
||||||
@ -300,6 +316,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("mqttTopicFlowCommand");
|
||||||
|
strcpy(configBlock.mqttTopicFlowCommand, arg.c_str());
|
||||||
arg = webServer.arg("mqttTopicColorCommand");
|
arg = webServer.arg("mqttTopicColorCommand");
|
||||||
strcpy(configBlock.mqttTopicColorCommand, arg.c_str());
|
strcpy(configBlock.mqttTopicColorCommand, arg.c_str());
|
||||||
arg = webServer.arg("mqttTopicCommand");
|
arg = webServer.arg("mqttTopicCommand");
|
||||||
@ -368,6 +386,10 @@ void showConfiguration() {
|
|||||||
Serial.print(configBlock.mqttPort);
|
Serial.print(configBlock.mqttPort);
|
||||||
Serial.println(">");
|
Serial.println(">");
|
||||||
|
|
||||||
|
Serial.print("mqttTopicFlowCommand = <");
|
||||||
|
Serial.print(configBlock.mqttTopicFlowCommand);
|
||||||
|
Serial.println(">");
|
||||||
|
|
||||||
Serial.print("mqttTopicColorCommand = <");
|
Serial.print("mqttTopicColorCommand = <");
|
||||||
Serial.print(configBlock.mqttTopicColorCommand);
|
Serial.print(configBlock.mqttTopicColorCommand);
|
||||||
Serial.println(">");
|
Serial.println(">");
|
||||||
|
@ -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 mqttTopicFlowCommand[64];
|
||||||
char mqttTopicColorCommand[64];
|
char mqttTopicColorCommand[64];
|
||||||
char mqttTopicCommand[64];
|
char mqttTopicCommand[64];
|
||||||
char mqttDebugTopic[64];
|
char mqttDebugTopic[64];
|
||||||
|
@ -43,6 +43,5 @@
|
|||||||
|
|
||||||
#define NUM_OF_LEDs 16
|
#define NUM_OF_LEDs 16
|
||||||
|
|
||||||
#define RECONNECT_DELAY 5000
|
|
||||||
|
|
||||||
#endif /* DEFINES_H_ */
|
#endif /* DEFINES_H_ */
|
||||||
|
Submodule esp8266boilerplate updated: 2441590cb8...749b1653a8
1
esp8266mqttboilerplate
Submodule
1
esp8266mqttboilerplate
Submodule
Submodule esp8266mqttboilerplate added at b20b46b2ca
Reference in New Issue
Block a user