Added MQTT auto reconnect

This commit is contained in:
cschwinne 2019-05-21 18:50:56 +02:00
parent 315987b2f6
commit 793f919d59
6 changed files with 21 additions and 17 deletions

View File

@ -979,7 +979,7 @@ uint16_t WS2812FX::mode_fire_flicker(void) {
setPixelColor(i, color_from_palette(i, true, PALETTE_SOLID_WRAP, 0, 255 - flicker)); setPixelColor(i, color_from_palette(i, true, PALETTE_SOLID_WRAP, 0, 255 - flicker));
} }
} }
return 10 + (2 * (uint16_t)(255 - SEGMENT.speed)); return 20 + random((255 - SEGMENT.speed),(2 * (uint16_t)(255 - SEGMENT.speed)));
} }

View File

@ -446,7 +446,7 @@ void WS2812FX::lock(uint16_t i)
void WS2812FX::lockRange(uint16_t i, uint16_t i2) void WS2812FX::lockRange(uint16_t i, uint16_t i2)
{ {
if (modeUsesLock(SEGMENT.mode)) return; if (modeUsesLock(SEGMENT.mode)) return;
for (uint16_t x = i; x <= i2; x++) for (uint16_t x = i; x < i2; x++)
{ {
if (i >= 0 && i < _length) _locked[i] = true; if (i >= 0 && i < _length) _locked[i] = true;
} }

View File

@ -8,7 +8,7 @@
*/ */
//ESP8266-01 (blue) got too little storage space to work with all features of WLED. To use it, you must use ESP8266 Arduino Core v2.3.0 and the setting 512K(64K SPIFFS). //ESP8266-01 (blue) got too little storage space to work with all features of WLED. To use it, you must use ESP8266 Arduino Core v2.4.2 and the setting 512K(No SPIFFS).
//ESP8266-01 (black) has 1MB flash and can thus fit the whole program. Use 1M(64K SPIFFS). //ESP8266-01 (black) has 1MB flash and can thus fit the whole program. Use 1M(64K SPIFFS).
//Uncomment some of the following lines to disable features to compile for ESP8266-01 (max flash size 434kB): //Uncomment some of the following lines to disable features to compile for ESP8266-01 (max flash size 434kB):
@ -30,7 +30,7 @@
//#define WLED_ENABLE_FS_EDITOR //enable /edit page for editing SPIFFS content. Will also be disabled with OTA lock //#define WLED_ENABLE_FS_EDITOR //enable /edit page for editing SPIFFS content. Will also be disabled with OTA lock
//to toggle usb serial debug (un)comment the following line //to toggle usb serial debug (un)comment the following line
//#define WLED_DEBUG #define WLED_DEBUG
//library inclusions //library inclusions
@ -98,7 +98,7 @@
//version code in format yymmddb (b = daily build) //version code in format yymmddb (b = daily build)
#define VERSION 1904141 #define VERSION 1905091
char versionString[] = "0.8.4"; char versionString[] = "0.8.4";
@ -373,10 +373,9 @@ IPAddress realtimeIP = (0,0,0,0);
unsigned long realtimeTimeout = 0; unsigned long realtimeTimeout = 0;
//mqtt //mqtt
long lastMQTTReconnectAttempt = 0; long nextMQTTReconnectAttempt = 0;
long lastInterfaceUpdate = 0; long lastInterfaceUpdate = 0;
byte interfaceUpdateCallMode = 0; byte interfaceUpdateCallMode = 0;
uint32_t mqttFailedConAttempts = 0;
#if AUXPIN >= 0 #if AUXPIN >= 0
//auxiliary debug pin //auxiliary debug pin
@ -544,6 +543,13 @@ void loop() {
if (!onlyAP) { if (!onlyAP) {
handleHue(); handleHue();
handleBlynk(); handleBlynk();
yield();
if (millis() > nextMQTTReconnectAttempt)
{
yield();
initMqtt();
nextMQTTReconnectAttempt = millis() + 30000;
}
} }
yield(); yield();
if (!offMode) strip.service(); if (!offMode) strip.service();

View File

@ -86,13 +86,6 @@ void wledInit()
strcat(mqttDeviceTopic, escapedMac.c_str()); strcat(mqttDeviceTopic, escapedMac.c_str());
} }
//smartInit, we only init some resources when connected
if (!onlyAP && WiFi.status() == WL_CONNECTED)
{
mqtt = new AsyncMqttClient();
initMqtt();
}
strip.service(); strip.service();
//HTTP server page init //HTTP server page init

View File

@ -47,6 +47,7 @@ void onMqttConnect(bool sessionPresent)
sendHADiscoveryMQTT(); sendHADiscoveryMQTT();
publishMqtt(); publishMqtt();
DEBUG_PRINTLN("MQTT ready");
} }
@ -100,6 +101,8 @@ void publishMqtt()
const char HA_static_JSON[] PROGMEM = R"=====(,"bri_val_tpl":"{{value}}","rgb_cmd_tpl":"{{'#%02x%02x%02x' | format(red, green, blue)}}","rgb_val_tpl":"{{value[1:3]|int(base=16)}},{{value[3:5]|int(base=16)}},{{value[5:7]|int(base=16)}}","qos":0,"opt":true,"pl_on":"ON","pl_off":"OFF","fx_val_tpl":"{{value}}","fx_list":[)====="; const char HA_static_JSON[] PROGMEM = R"=====(,"bri_val_tpl":"{{value}}","rgb_cmd_tpl":"{{'#%02x%02x%02x' | format(red, green, blue)}}","rgb_val_tpl":"{{value[1:3]|int(base=16)}},{{value[3:5]|int(base=16)}},{{value[5:7]|int(base=16)}}","qos":0,"opt":true,"pl_on":"ON","pl_off":"OFF","fx_val_tpl":"{{value}}","fx_list":[)=====";
void sendHADiscoveryMQTT(){ void sendHADiscoveryMQTT(){
#if ARDUINO_ARCH_ESP32 || LEDPIN != 3
/* /*
YYYY is discovery tipic YYYY is discovery tipic
@ -207,12 +210,15 @@ Send out HA MQTT Discovery message on MQTT connect (~2.4kB):
strcat(pubt, escapedMac.c_str()); strcat(pubt, escapedMac.c_str());
strcat(pubt, "/config"); strcat(pubt, "/config");
mqtt->publish(pubt, 0, true, buffer); mqtt->publish(pubt, 0, true, buffer);
#endif
} }
bool initMqtt() bool initMqtt()
{ {
if (WiFi.status() != WL_CONNECTED) return false;
if (mqttServer[0] == 0) return false; if (mqttServer[0] == 0) return false;
if (WiFi.status() != WL_CONNECTED) return false;
if (!mqtt) mqtt = new AsyncMqttClient();
if (mqtt->connected()) return true;
IPAddress mqttIP; IPAddress mqttIP;
if (mqttIP.fromString(mqttServer)) //see if server is IP or domain if (mqttIP.fromString(mqttServer)) //see if server is IP or domain
@ -225,6 +231,5 @@ bool initMqtt()
mqtt->onMessage(onMqttMessage); mqtt->onMessage(onMqttMessage);
mqtt->onConnect(onMqttConnect); mqtt->onConnect(onMqttConnect);
mqtt->connect(); mqtt->connect();
DEBUG_PRINTLN("MQTT ready");
return true; return true;
} }