Compare commits
4 Commits
use_boiler
...
use_esp826
Author | SHA1 | Date | |
---|---|---|---|
a9e86a3393
|
|||
68f6e5661b
|
|||
14a0251806
|
|||
48b07ec406
|
@ -29,6 +29,7 @@
|
||||
<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/esp8266boilerplate}""/>
|
||||
<listOptionValue builtIn="false" value=""${workspace_loc:/rgbled/esp8266mqttboilerplate}""/>
|
||||
</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=""${workspace_loc:/rgbled/libraries/ESP8266WiFi/src}""/>
|
||||
<listOptionValue builtIn="false" value=""${workspace_loc:/rgbled/pubsubclient}""/>
|
||||
<listOptionValue builtIn="false" value=""${workspace_loc:/rgbled/esp8266boilerplate}""/>
|
||||
<listOptionValue builtIn="false" value=""${workspace_loc:/rgbled/esp8266mqttboilerplate}""/>
|
||||
</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=""${workspace_loc:/rgbled/libraries/ESP8266WiFi/src}""/>
|
||||
<listOptionValue builtIn="false" value=""${workspace_loc:/rgbled/pubsubclient}""/>
|
||||
<listOptionValue builtIn="false" value=""${workspace_loc:/rgbled/esp8266boilerplate}""/>
|
||||
<listOptionValue builtIn="false" value=""${workspace_loc:/rgbled/esp8266mqttboilerplate}""/>
|
||||
</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
3
.gitmodules
vendored
@ -1,3 +1,6 @@
|
||||
[submodule "esp8266boilerplate"]
|
||||
path = esp8266boilerplate
|
||||
url = ../esp8266boilerplate.git
|
||||
[submodule "esp8266mqttboilerplate"]
|
||||
path = esp8266mqttboilerplate
|
||||
url = ../esp8266mqttboilerplate
|
||||
|
114
application.cpp
114
application.cpp
@ -9,15 +9,13 @@
|
||||
|
||||
#include "defines.h"
|
||||
|
||||
#define MQTT_MAX_PACKET_SIZE 256
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
#include <stdbool.h>
|
||||
#include <ESP8266WiFi.h>
|
||||
// #include <ESP8266WebServer.h>
|
||||
#include <PubSubClient.h>
|
||||
#include <mqttHandling.h>
|
||||
|
||||
|
||||
#ifdef WS2811
|
||||
#define FASTLED_ESP8266_NODEMCU_PIN_ORDER
|
||||
@ -34,13 +32,10 @@
|
||||
|
||||
|
||||
|
||||
void callback(char* topic, byte* payload, unsigned int length);
|
||||
|
||||
|
||||
|
||||
|
||||
WiFiClientSecure espClient;
|
||||
PubSubClient client(espClient);
|
||||
|
||||
|
||||
#ifdef WS2811
|
||||
@ -100,58 +95,13 @@ static CRGB evaluationColorWord(char* cmd) {
|
||||
|
||||
|
||||
|
||||
|
||||
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");
|
||||
#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;
|
||||
void subscribeApplication() {
|
||||
mqttClient.subscribe(configBlock.mqttTopicColorCommand);
|
||||
mqttClient.subscribe(configBlock.mqttTopicCommand);
|
||||
}
|
||||
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)) {
|
||||
int32_t n, b;
|
||||
CRGB pseudoColors;
|
||||
@ -277,57 +227,13 @@ void callback(char* topic, byte* payload, unsigned int length) {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
void reconnect() {
|
||||
// Loop until we're reconnected
|
||||
while (!client.connected()) {
|
||||
#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
|
||||
// Wait 5 seconds before retrying
|
||||
delay(5000);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
bool mqtt_connect() {
|
||||
bool reconnected = false;
|
||||
if (!client.connected()) {
|
||||
reconnect();
|
||||
reconnected = true;
|
||||
}
|
||||
client.loop();
|
||||
return reconnected;
|
||||
}
|
||||
|
||||
|
||||
void setupApplication() {
|
||||
client.setServer(configBlock.mqttBroker, configBlock.mqttPort);
|
||||
mqttSetup();
|
||||
|
||||
#ifdef WS2811
|
||||
FastLED.addLeds<NEOPIXEL, PIXEL_PIN>(leds, NUM_OF_LEDs);
|
||||
@ -344,11 +250,7 @@ void setupApplication() {
|
||||
|
||||
|
||||
void loopApplication() {
|
||||
bool reconnected = mqtt_connect();
|
||||
static uint32_t reconnectTime = 0;
|
||||
if (reconnected) {
|
||||
reconnectTime = millis();
|
||||
}
|
||||
mqttLoop();
|
||||
}
|
||||
|
||||
|
||||
|
Submodule esp8266boilerplate updated: 2441590cb8...749b1653a8
1
esp8266mqttboilerplate
Submodule
1
esp8266mqttboilerplate
Submodule
Submodule esp8266mqttboilerplate added at b20b46b2ca
Reference in New Issue
Block a user