use esp8266mqttboilerplate

This commit is contained in:
2019-04-26 13:46:33 +02:00
parent 48b07ec406
commit 14a0251806
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/libraries/ESP8266WiFi/src}&quot;"/>
<listOptionValue builtIn="false" value="&quot;${workspace_loc:/rgbled/pubsubclient}&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/esp8266boilerplate}&quot;"/>
<listOptionValue builtIn="false" value="&quot;${workspace_loc:/rgbled/esp8266mqttboilerplate}&quot;"/>
</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="&quot;${workspace_loc:/rgbled/libraries/ESP8266WiFi/src}&quot;"/> <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/pubsubclient}&quot;"/>
<listOptionValue builtIn="false" value="&quot;${workspace_loc:/rgbled/esp8266boilerplate}&quot;"/> <listOptionValue builtIn="false" value="&quot;${workspace_loc:/rgbled/esp8266boilerplate}&quot;"/>
<listOptionValue builtIn="false" value="&quot;${workspace_loc:/rgbled/esp8266mqttboilerplate}&quot;"/>
</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="&quot;${workspace_loc:/rgbled/libraries/ESP8266WiFi/src}&quot;"/> <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/pubsubclient}&quot;"/>
<listOptionValue builtIn="false" value="&quot;${workspace_loc:/rgbled/esp8266boilerplate}&quot;"/> <listOptionValue builtIn="false" value="&quot;${workspace_loc:/rgbled/esp8266boilerplate}&quot;"/>
<listOptionValue builtIn="false" value="&quot;${workspace_loc:/rgbled/esp8266mqttboilerplate}&quot;"/>
</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
View File

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

View File

@ -9,7 +9,6 @@
#include "defines.h" #include "defines.h"
#define MQTT_MAX_PACKET_SIZE 256
#include <stdio.h> #include <stdio.h>
#include <stdint.h> #include <stdint.h>
@ -18,6 +17,8 @@
#include <ESP8266WiFi.h> #include <ESP8266WiFi.h>
// #include <ESP8266WebServer.h> // #include <ESP8266WebServer.h>
#include <PubSubClient.h> #include <PubSubClient.h>
#include <mqttHandling.h>
#ifdef WS2811 #ifdef WS2811
#define FASTLED_ESP8266_NODEMCU_PIN_ORDER #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 #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; void callbackApplication(char *topic, uint8_t tokenCnt, char **tokens) {
const uint8_t BUFSIZE = 128; if (! strcmp(topic, configBlock.mqttTopicCommand)) {
if ((length + 1) >= BUFSIZE) { // 1 for terminating NUL int32_t n, b;
#ifdef DEBUG CRGB pseudoColors;
Serial.println("Received message too long, ignore it"); 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 #endif
} else { #ifdef PL9823
char buffer[BUFSIZE]; pixels.setPixelColor(i, pixels.Color(b, b, b));
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 #endif
} }
} while (tk); #ifdef WS2811
FastLED.show();
#endif
#ifdef PL9823
pixels.show();
#endif
#ifdef DEBUG break;
for (uint8_t i = 0; i < tokenCnt; i++) { case 2:
Serial.print("Token "); // set brightness for one LED
Serial.print(i); n = strtol(tokens[0], NULL, 10);
Serial.print(": "); b = strtol(tokens[1], NULL, 10);
Serial.println(tokens[i]); 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)) { } else if (! strcmp(topic, configBlock.mqttTopicColorCommand)) {
int32_t n, b; int32_t n, red, green, blue;
CRGB pseudoColors; CRGB colors;
switch (tokenCnt) { switch (tokenCnt) {
case 1: case 1:
// set brightness // on, off, color word for all LEDs
b = strtol(tokens[0], NULL, 10); colors = evaluationColorWord(tokens[0]);
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 = b; leds[i] = colors;
leds[i].g = b;
leds[i].b = b;
#endif #endif
#ifdef PL9823 #ifdef PL9823
pixels.setPixelColor(i, pixels.Color(b, b, b)); pixels.setPixelColor(i, pixels.Color(colors.r, colors.g, colors.b));
#endif #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 #ifdef WS2811
leds[i] = colors; FastLED.show();
#endif #endif
#ifdef PL9823 #ifdef PL9823
pixels.setPixelColor(i, pixels.Color(colors.r, colors.g, colors.b)); pixels.show();
#endif #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 #ifdef WS2811
leds[n] = colors;
FastLED.show(); FastLED.show();
#endif #endif
#ifdef PL9823 #ifdef PL9823
pixels.show(); pixels.setPixelColor(n, pixels.Color(colors.r, colors.g, colors.b));
pixels.show();
#endif #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() { 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);
#endif #endif
#ifdef PL9823 #ifdef PL9823
pixels.begin(); pixels.begin();
for (uint8_t i = 0; i < NUM_OF_LEDs; i++) { 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 #endif
} }
void loopApplication() { void loopApplication() {
if (!client.connected()) { mqttLoop();
reconnect();
} else {
client.loop();
}
} }

View File

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