use esp8266mqttboilerplate

This commit is contained in:
Wolfgang Hottgenroth 2019-04-26 13:46:33 +02:00
parent 48b07ec406
commit 14a0251806
Signed by: wn
GPG Key ID: B586EAFCDF2F65F4
5 changed files with 121 additions and 202 deletions

View File

@ -29,6 +29,7 @@
<listOptionValue builtIn="false" value="&quot;${workspace_loc:/rgbled/libraries/ESP8266WiFi/src}&quot;"/>
<listOptionValue builtIn="false" value="&quot;${workspace_loc:/rgbled/pubsubclient}&quot;"/>
<listOptionValue builtIn="false" value="&quot;${workspace_loc:/rgbled/esp8266boilerplate}&quot;"/>
<listOptionValue builtIn="false" value="&quot;${workspace_loc:/rgbled/esp8266mqttboilerplate}&quot;"/>
</option>
<inputType id="io.sloeber.compiler.cpp.sketch.input.454285658" name="CPP source files" superClass="io.sloeber.compiler.cpp.sketch.input"/>
</tool>
@ -44,6 +45,7 @@
<listOptionValue builtIn="false" value="&quot;${workspace_loc:/rgbled/libraries/ESP8266WiFi/src}&quot;"/>
<listOptionValue builtIn="false" value="&quot;${workspace_loc:/rgbled/pubsubclient}&quot;"/>
<listOptionValue builtIn="false" value="&quot;${workspace_loc:/rgbled/esp8266boilerplate}&quot;"/>
<listOptionValue builtIn="false" value="&quot;${workspace_loc:/rgbled/esp8266mqttboilerplate}&quot;"/>
</option>
<inputType id="io.sloeber.compiler.c.sketch.input.1857532110" name="C Source Files" superClass="io.sloeber.compiler.c.sketch.input"/>
</tool>
@ -59,6 +61,7 @@
<listOptionValue builtIn="false" value="&quot;${workspace_loc:/rgbled/libraries/ESP8266WiFi/src}&quot;"/>
<listOptionValue builtIn="false" value="&quot;${workspace_loc:/rgbled/pubsubclient}&quot;"/>
<listOptionValue builtIn="false" value="&quot;${workspace_loc:/rgbled/esp8266boilerplate}&quot;"/>
<listOptionValue builtIn="false" value="&quot;${workspace_loc:/rgbled/esp8266mqttboilerplate}&quot;"/>
</option>
<inputType id="io.sloeber.compiler.S.sketch.input.2089597409" name="Assembly source files" superClass="io.sloeber.compiler.S.sketch.input"/>
</tool>

3
.gitmodules vendored
View File

@ -1,3 +1,6 @@
[submodule "esp8266boilerplate"]
path = esp8266boilerplate
url = ../esp8266boilerplate.git
[submodule "esp8266mqttboilerplate"]
path = esp8266mqttboilerplate
url = ../esp8266mqttboilerplate

View File

@ -9,7 +9,6 @@
#include "defines.h"
#define MQTT_MAX_PACKET_SIZE 256
#include <stdio.h>
#include <stdint.h>
@ -18,6 +17,8 @@
#include <ESP8266WiFi.h>
// #include <ESP8266WebServer.h>
#include <PubSubClient.h>
#include <mqttHandling.h>
#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<NEOPIXEL, PIXEL_PIN>(leds, NUM_OF_LEDs);
FastLED.addLeds<NEOPIXEL, PIXEL_PIN>(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();
}

View File

@ -43,6 +43,5 @@
#define NUM_OF_LEDs 16
#define RECONNECT_DELAY 5000
#endif /* DEFINES_H_ */

@ -0,0 +1 @@
Subproject commit 1dca68dbc1c18f2f0b4669579e07adafa6bfba4a