Attempted to improve AP only stability

This commit is contained in:
cschwinne 2018-10-04 18:17:01 +02:00
parent eeb17b417c
commit c2972786f5
6 changed files with 91 additions and 70 deletions

View File

@ -59,7 +59,7 @@ char otaPass[33] = "wledota";
//to toggle usb serial debug (un)comment following line(s) //to toggle usb serial debug (un)comment following line(s)
//#define DEBUG #define DEBUG
//Hardware CONFIG (only changeble HERE, not at runtime) //Hardware CONFIG (only changeble HERE, not at runtime)
@ -371,16 +371,16 @@ WebServer server(80);
#else #else
ESP8266WebServer server(80); ESP8266WebServer server(80);
#endif #endif
HTTPClient hueClient; HTTPClient* hueClient = NULL;
WiFiClient mqttTCPClient; WiFiClient* mqttTCPClient = NULL;
PubSubClient mqtt(mqttTCPClient); PubSubClient* mqtt = NULL;
ESP8266HTTPUpdateServer httpUpdater; ESP8266HTTPUpdateServer httpUpdater;
//udp interface objects //udp interface objects
WiFiUDP notifierUdp, rgbUdp; WiFiUDP notifierUdp, rgbUdp;
WiFiUDP ntpUdp; WiFiUDP ntpUdp;
E131 e131; E131* e131;
//led fx library object //led fx library object
WS2812FX strip = WS2812FX(); WS2812FX strip = WS2812FX();
@ -477,19 +477,26 @@ void loop() {
yield(); yield();
handleButton(); handleButton();
handleNetworkTime(); handleNetworkTime();
handleAlexa(); if (!onlyAP)
{
handleAlexa();
handleMQTT();
}
handleOverlays(); handleOverlays();
yield(); yield();
handleMQTT();
if (!realtimeActive) //block stuff if WARLS/Adalight is enabled if (!realtimeActive) //block stuff if WARLS/Adalight is enabled
{ {
if (dnsActive) dnsServer.processNextRequest(); if (dnsActive) dnsServer.processNextRequest();
if (aOtaEnabled) ArduinoOTA.handle(); if (aOtaEnabled) ArduinoOTA.handle();
handleHue();
handleNightlight(); handleNightlight();
handleBlynk(); if (!onlyAP) {
handleHue();
handleBlynk();
}
if (briT) strip.service(); //do not update strip if off, prevents flicker on ESP32 if (briT) strip.service(); //do not update strip if off, prevents flicker on ESP32
} }

View File

@ -49,18 +49,24 @@ void wledInit()
if (ntpEnabled && WiFi.status() == WL_CONNECTED) if (ntpEnabled && WiFi.status() == WL_CONNECTED)
ntpConnected = ntpUdp.begin(ntpLocalPort); ntpConnected = ntpUdp.begin(ntpLocalPort);
//start captive portal //start captive portal if AP active
if (onlyAP || strlen(apSSID) > 0) if (onlyAP || strlen(apSSID) > 0)
{ {
//dnsServer.setErrorReplyCode(DNSReplyCode::NoError);
dnsServer.start(53, "*", WiFi.softAPIP()); dnsServer.start(53, "*", WiFi.softAPIP());
dnsActive = true; dnsActive = true;
} }
prepareIds(); //UUID from MAC (for Alexa and MQTT) prepareIds(); //UUID from MAC (for Alexa and MQTT)
if (mqttDeviceTopic[0] == 0) strcpy(mqttDeviceTopic, strcat("wled/", escapedMac.c_str())); if (mqttDeviceTopic[0] == 0) strcpy(mqttDeviceTopic, strcat("wled/", escapedMac.c_str()));
if (!onlyAP) mqttInit = initMQTT();
//smartInit, we only init some resources when connected
if (!onlyAP && WiFi.status() == WL_CONNECTED)
{
mqttTCPClient = new WiFiClient();
mqtt = new PubSubClient(*mqttTCPClient);
mqttInit = initMQTT();
}
if (!initLedsLast) strip.service(); if (!initLedsLast) strip.service();
//HTTP server page init //HTTP server page init
@ -68,38 +74,42 @@ void wledInit()
if (!initLedsLast) strip.service(); if (!initLedsLast) strip.service();
//init Alexa hue emulation //init Alexa hue emulation
if (alexaEnabled) alexaInit(); if (alexaEnabled && !onlyAP) alexaInit();
server.begin(); server.begin();
DEBUG_PRINTLN("HTTP server started"); DEBUG_PRINTLN("HTTP server started");
//init ArduinoOTA //init ArduinoOTA
if (aOtaEnabled) if (!onlyAP) {
{ if (aOtaEnabled)
ArduinoOTA.onStart([]() { {
#ifndef ARDUINO_ARCH_ESP32 ArduinoOTA.onStart([]() {
wifi_set_sleep_type(NONE_SLEEP_T); #ifndef ARDUINO_ARCH_ESP32
#endif wifi_set_sleep_type(NONE_SLEEP_T);
DEBUG_PRINTLN("Start ArduinoOTA"); #endif
}); DEBUG_PRINTLN("Start ArduinoOTA");
if (strlen(cmDNS) > 0) ArduinoOTA.setHostname(cmDNS); });
ArduinoOTA.begin(); if (strlen(cmDNS) > 0) ArduinoOTA.setHostname(cmDNS);
} ArduinoOTA.begin();
}
if (!initLedsLast) strip.service();
// Set up mDNS responder:
if (strlen(cmDNS) > 0 && !onlyAP)
{
MDNS.begin(cmDNS);
DEBUG_PRINTLN("mDNS responder started");
// Add service to MDNS
MDNS.addService("http", "tcp", 80);
}
if (!initLedsLast) strip.service();
if (!initLedsLast) strip.service(); initBlynk(blynkApiKey);
// Set up mDNS responder: initE131();
if (strlen(cmDNS) > 0 && !onlyAP)
{
MDNS.begin(cmDNS);
DEBUG_PRINTLN("mDNS responder started");
// Add service to MDNS
MDNS.addService("http", "tcp", 80);
}
if (!onlyAP) hueClient = new HTTPClient();
{ } else {
initBlynk(blynkApiKey); e131Enabled = false;
initE131();
} }
if (initLedsLast) initStrip(); if (initLedsLast) initStrip();
@ -146,12 +156,12 @@ void initCon()
if (strlen(apSSID)>0) if (strlen(apSSID)>0)
{ {
DEBUG_PRINT("USING AP"); DEBUG_PRINT(" USING AP");
DEBUG_PRINTLN(strlen(apSSID)); DEBUG_PRINTLN(strlen(apSSID));
initAP(); initAP();
} else } else
{ {
DEBUG_PRINTLN("NO AP"); DEBUG_PRINTLN(" NO AP");
WiFi.softAPdisconnect(true); WiFi.softAPdisconnect(true);
} }
int fail_count = 0; int fail_count = 0;

View File

@ -69,7 +69,8 @@ void arlsLock(uint32_t timeoutMs)
void initE131(){ void initE131(){
if (WiFi.status() == WL_CONNECTED && e131Enabled) if (WiFi.status() == WL_CONNECTED && e131Enabled)
{ {
e131.begin((e131Multicast) ? E131_MULTICAST : E131_UNICAST , e131Universe); e131 = new E131();
e131->begin((e131Multicast) ? E131_MULTICAST : E131_UNICAST , e131Universe);
} else { } else {
e131Enabled = false; e131Enabled = false;
} }
@ -84,14 +85,14 @@ void handleNotifications()
//E1.31 protocol support //E1.31 protocol support
if(e131Enabled) { if(e131Enabled) {
uint16_t len = e131.parsePacket(); uint16_t len = e131->parsePacket();
if (len && e131.universe == e131Universe) { if (len && e131->universe == e131Universe) {
arlsLock(realtimeTimeoutMs); arlsLock(realtimeTimeoutMs);
if (len > ledCount) len = ledCount; if (len > ledCount) len = ledCount;
for (uint16_t i = 0; i < len; i++) { for (uint16_t i = 0; i < len; i++) {
int j = i * 3; int j = i * 3;
setRealtimePixel(i, e131.data[j], e131.data[j+1], e131.data[j+2], 0); setRealtimePixel(i, e131->data[j], e131->data[j+1], e131->data[j+2], 0);
} }
strip.show(); strip.show();
} }

View File

@ -4,7 +4,7 @@
void handleHue() void handleHue()
{ {
if (huePollingEnabled && WiFi.status() == WL_CONNECTED) if (huePollingEnabled && WiFi.status() == WL_CONNECTED && hueClient != NULL)
{ {
if (millis() - hueLastRequestSent > huePollIntervalMsTemp) if (millis() - hueLastRequestSent > huePollIntervalMsTemp)
{ {
@ -44,8 +44,8 @@ bool setupHue()
bool sendHuePoll(bool sAuth) bool sendHuePoll(bool sAuth)
{ {
bool st; bool st;
hueClient.setReuse(true); hueClient->setReuse(true);
hueClient.setTimeout(450); hueClient->setTimeout(450);
String hueURL = "http://"; String hueURL = "http://";
hueURL += hueIP.toString(); hueURL += hueIP.toString();
hueURL += "/api/"; hueURL += "/api/";
@ -53,12 +53,12 @@ bool sendHuePoll(bool sAuth)
hueURL += hueApiKey; hueURL += hueApiKey;
hueURL += "/lights/" + String(huePollLightId); hueURL += "/lights/" + String(huePollLightId);
} }
hueClient.begin(hueURL); hueClient->begin(hueURL);
int httpCode = (sAuth)? hueClient.POST("{\"devicetype\":\"wled#esp\"}"):hueClient.GET(); int httpCode = (sAuth)? hueClient->POST("{\"devicetype\":\"wled#esp\"}"):hueClient->GET();
//TODO this request may block operation for ages //TODO this request may block operation for ages
if (httpCode>0){ if (httpCode>0){
st = handleHueResponse(hueClient.getString(),sAuth); st = handleHueResponse(hueClient->getString(),sAuth);
} else { } else {
strcpy(hueError,"Request timed out"); strcpy(hueError,"Request timed out");
st = false; st = false;

View File

@ -20,6 +20,7 @@ void handleBlynk()
void updateBlynk() void updateBlynk()
{ {
if (onlyAP) return;
Blynk.virtualWrite(V0,bri); Blynk.virtualWrite(V0,bri);
//we need a RGB -> HSB convert here //we need a RGB -> HSB convert here
Blynk.virtualWrite(V3,bri); Blynk.virtualWrite(V3,bri);

View File

@ -29,7 +29,8 @@ void callbackMQTT(char* topic, byte* payload, unsigned int length) {
} else if (strstr(topic, "/api")) } else if (strstr(topic, "/api"))
{ {
String apireq = "win&"; String apireq = "win&";
handleSet(apireq += (char*)payload)); apireq += (char*)payload;
handleSet(apireq);
} else } else
{ {
parseMQTTBriPayload((char*)payload); parseMQTTBriPayload((char*)payload);
@ -38,7 +39,8 @@ void callbackMQTT(char* topic, byte* payload, unsigned int length) {
void publishMQTT() void publishMQTT()
{ {
if (!mqtt.connected()) return; if (mqtt == NULL) return;
if (!mqtt->connected()) return;
DEBUG_PRINTLN("Publish MQTT"); DEBUG_PRINTLN("Publish MQTT");
char s[10]; char s[10];
@ -47,24 +49,24 @@ void publishMQTT()
sprintf(s, "%ld", bri); sprintf(s, "%ld", bri);
strcpy(subuf, mqttDeviceTopic); strcpy(subuf, mqttDeviceTopic);
strcat(subuf, "/g"); strcat(subuf, "/g");
mqtt.publish(subuf, s); mqtt->publish(subuf, s);
sprintf(s, "#%X", white*16777216 + col[0]*65536 + col[1]*256 + col[2]); sprintf(s, "#%X", white*16777216 + col[0]*65536 + col[1]*256 + col[2]);
strcpy(subuf, mqttDeviceTopic); strcpy(subuf, mqttDeviceTopic);
strcat(subuf, "/c"); strcat(subuf, "/c");
mqtt.publish(subuf, s); mqtt->publish(subuf, s);
//if you want to use this, increase the MQTT buffer in PubSubClient.h to 350+ //if you want to use this, increase the MQTT buffer in PubSubClient.h to 350+
//it will publish the API response to MQTT //it will publish the API response to MQTT
/*XML_response(false); /*XML_response(false);
strcpy(subuf, mqttDeviceTopic); strcpy(subuf, mqttDeviceTopic);
strcat(subuf, "/v"); strcat(subuf, "/v");
mqtt.publish(subuf, obuf);*/ mqtt->publish(subuf, obuf);*/
} }
bool reconnectMQTT() bool reconnectMQTT()
{ {
if (mqtt.connect(escapedMac.c_str())) if (mqtt->connect(escapedMac.c_str()))
{ {
//re-subscribe to required topics //re-subscribe to required topics
char subuf[38]; char subuf[38];
@ -73,26 +75,26 @@ bool reconnectMQTT()
if (mqttDeviceTopic[0] != 0) if (mqttDeviceTopic[0] != 0)
{ {
strcpy(subuf, mqttDeviceTopic); strcpy(subuf, mqttDeviceTopic);
mqtt.subscribe(subuf); mqtt->subscribe(subuf);
strcat(subuf, "/col"); strcat(subuf, "/col");
mqtt.subscribe(subuf); mqtt->subscribe(subuf);
strcpy(subuf, mqttDeviceTopic); strcpy(subuf, mqttDeviceTopic);
strcat(subuf, "/api"); strcat(subuf, "/api");
mqtt.subscribe(subuf); mqtt->subscribe(subuf);
} }
if (mqttGroupTopic[0] != 0) if (mqttGroupTopic[0] != 0)
{ {
strcpy(subuf, mqttGroupTopic); strcpy(subuf, mqttGroupTopic);
mqtt.subscribe(subuf); mqtt->subscribe(subuf);
strcat(subuf, "/col"); strcat(subuf, "/col");
mqtt.subscribe(subuf); mqtt->subscribe(subuf);
strcpy(subuf, mqttGroupTopic); strcpy(subuf, mqttGroupTopic);
strcat(subuf, "/api"); strcat(subuf, "/api");
mqtt.subscribe(subuf); mqtt->subscribe(subuf);
} }
} }
return mqtt.connected(); return mqtt->connected();
} }
bool initMQTT() bool initMQTT()
@ -103,11 +105,11 @@ bool initMQTT()
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
{ {
mqtt.setServer(mqttIP,1883); mqtt->setServer(mqttIP,1883);
} else { } else {
mqtt.setServer(mqttServer,1883); mqtt->setServer(mqttServer,1883);
} }
mqtt.setCallback(callbackMQTT); mqtt->setCallback(callbackMQTT);
DEBUG_PRINTLN("MQTT ready."); DEBUG_PRINTLN("MQTT ready.");
return true; return true;
} }
@ -117,7 +119,7 @@ void handleMQTT()
if (WiFi.status() != WL_CONNECTED || !mqttInit) return; if (WiFi.status() != WL_CONNECTED || !mqttInit) return;
//every time connection is unsuccessful, the attempt interval is increased, since attempt will block program for 7 sec each time //every time connection is unsuccessful, the attempt interval is increased, since attempt will block program for 7 sec each time
if (!mqtt.connected() && millis() - lastMQTTReconnectAttempt > 5000 + (5000 * mqttFailedConAttempts * mqttFailedConAttempts)) if (!mqtt->connected() && millis() - lastMQTTReconnectAttempt > 5000 + (5000 * mqttFailedConAttempts * mqttFailedConAttempts))
{ {
DEBUG_PRINTLN("Attempting to connect MQTT..."); DEBUG_PRINTLN("Attempting to connect MQTT...");
lastMQTTReconnectAttempt = millis(); lastMQTTReconnectAttempt = millis();
@ -130,5 +132,5 @@ void handleMQTT()
DEBUG_PRINTLN("MQTT con!"); DEBUG_PRINTLN("MQTT con!");
mqttFailedConAttempts = 0; mqttFailedConAttempts = 0;
} }
mqtt.loop(); mqtt->loop();
} }