use esp8266mqttboilerplate
This commit is contained in:
parent
48b07ec406
commit
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
|
||||||
|
103
application.cpp
103
application.cpp
@ -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,58 +98,13 @@ 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;
|
|
||||||
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
|
|
||||||
for (uint8_t i = 0; i < tokenCnt; i++) {
|
|
||||||
Serial.print("Token ");
|
|
||||||
Serial.print(i);
|
|
||||||
Serial.print(": ");
|
|
||||||
Serial.println(tokens[i]);
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
|
void callbackApplication(char *topic, uint8_t tokenCnt, char **tokens) {
|
||||||
if (! strcmp(topic, configBlock.mqttTopicCommand)) {
|
if (! strcmp(topic, configBlock.mqttTopicCommand)) {
|
||||||
int32_t n, b;
|
int32_t n, b;
|
||||||
CRGB pseudoColors;
|
CRGB pseudoColors;
|
||||||
@ -277,49 +230,13 @@ void callback(char* topic, byte* payload, unsigned int length) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
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,11 +253,7 @@ void setupApplication() {
|
|||||||
|
|
||||||
|
|
||||||
void loopApplication() {
|
void loopApplication() {
|
||||||
if (!client.connected()) {
|
mqttLoop();
|
||||||
reconnect();
|
|
||||||
} else {
|
|
||||||
client.loop();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -43,6 +43,5 @@
|
|||||||
|
|
||||||
#define NUM_OF_LEDs 16
|
#define NUM_OF_LEDs 16
|
||||||
|
|
||||||
#define RECONNECT_DELAY 5000
|
|
||||||
|
|
||||||
#endif /* DEFINES_H_ */
|
#endif /* DEFINES_H_ */
|
||||||
|
1
esp8266mqttboilerplate
Submodule
1
esp8266mqttboilerplate
Submodule
@ -0,0 +1 @@
|
|||||||
|
Subproject commit 1dca68dbc1c18f2f0b4669579e07adafa6bfba4a
|
Loading…
x
Reference in New Issue
Block a user