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)
//#define DEBUG
#define DEBUG
//Hardware CONFIG (only changeble HERE, not at runtime)
@ -371,16 +371,16 @@ WebServer server(80);
#else
ESP8266WebServer server(80);
#endif
HTTPClient hueClient;
WiFiClient mqttTCPClient;
PubSubClient mqtt(mqttTCPClient);
HTTPClient* hueClient = NULL;
WiFiClient* mqttTCPClient = NULL;
PubSubClient* mqtt = NULL;
ESP8266HTTPUpdateServer httpUpdater;
//udp interface objects
WiFiUDP notifierUdp, rgbUdp;
WiFiUDP ntpUdp;
E131 e131;
E131* e131;
//led fx library object
WS2812FX strip = WS2812FX();
@ -477,19 +477,26 @@ void loop() {
yield();
handleButton();
handleNetworkTime();
handleAlexa();
if (!onlyAP)
{
handleAlexa();
handleMQTT();
}
handleOverlays();
yield();
handleMQTT();
if (!realtimeActive) //block stuff if WARLS/Adalight is enabled
{
if (dnsActive) dnsServer.processNextRequest();
if (aOtaEnabled) ArduinoOTA.handle();
handleHue();
handleNightlight();
handleBlynk();
if (!onlyAP) {
handleHue();
handleBlynk();
}
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)
ntpConnected = ntpUdp.begin(ntpLocalPort);
//start captive portal
if (onlyAP || strlen(apSSID) > 0)
//start captive portal if AP active
if (onlyAP || strlen(apSSID) > 0)
{
//dnsServer.setErrorReplyCode(DNSReplyCode::NoError);
dnsServer.start(53, "*", WiFi.softAPIP());
dnsActive = true;
}
prepareIds(); //UUID from MAC (for Alexa and MQTT)
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();
//HTTP server page init
@ -68,38 +74,42 @@ void wledInit()
if (!initLedsLast) strip.service();
//init Alexa hue emulation
if (alexaEnabled) alexaInit();
if (alexaEnabled && !onlyAP) alexaInit();
server.begin();
DEBUG_PRINTLN("HTTP server started");
//init ArduinoOTA
if (aOtaEnabled)
{
ArduinoOTA.onStart([]() {
#ifndef ARDUINO_ARCH_ESP32
wifi_set_sleep_type(NONE_SLEEP_T);
#endif
DEBUG_PRINTLN("Start ArduinoOTA");
});
if (strlen(cmDNS) > 0) ArduinoOTA.setHostname(cmDNS);
ArduinoOTA.begin();
}
if (!onlyAP) {
if (aOtaEnabled)
{
ArduinoOTA.onStart([]() {
#ifndef ARDUINO_ARCH_ESP32
wifi_set_sleep_type(NONE_SLEEP_T);
#endif
DEBUG_PRINTLN("Start ArduinoOTA");
});
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();
// 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);
}
initBlynk(blynkApiKey);
initE131();
if (!onlyAP)
{
initBlynk(blynkApiKey);
initE131();
hueClient = new HTTPClient();
} else {
e131Enabled = false;
}
if (initLedsLast) initStrip();
@ -146,12 +156,12 @@ void initCon()
if (strlen(apSSID)>0)
{
DEBUG_PRINT("USING AP");
DEBUG_PRINT(" USING AP");
DEBUG_PRINTLN(strlen(apSSID));
initAP();
} else
{
DEBUG_PRINTLN("NO AP");
DEBUG_PRINTLN(" NO AP");
WiFi.softAPdisconnect(true);
}
int fail_count = 0;

View File

@ -69,7 +69,8 @@ void arlsLock(uint32_t timeoutMs)
void initE131(){
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 {
e131Enabled = false;
}
@ -84,14 +85,14 @@ void handleNotifications()
//E1.31 protocol support
if(e131Enabled) {
uint16_t len = e131.parsePacket();
if (len && e131.universe == e131Universe) {
uint16_t len = e131->parsePacket();
if (len && e131->universe == e131Universe) {
arlsLock(realtimeTimeoutMs);
if (len > ledCount) len = ledCount;
for (uint16_t i = 0; i < len; i++) {
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();
}

View File

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

View File

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

View File

@ -29,7 +29,8 @@ void callbackMQTT(char* topic, byte* payload, unsigned int length) {
} else if (strstr(topic, "/api"))
{
String apireq = "win&";
handleSet(apireq += (char*)payload));
apireq += (char*)payload;
handleSet(apireq);
} else
{
parseMQTTBriPayload((char*)payload);
@ -38,7 +39,8 @@ void callbackMQTT(char* topic, byte* payload, unsigned int length) {
void publishMQTT()
{
if (!mqtt.connected()) return;
if (mqtt == NULL) return;
if (!mqtt->connected()) return;
DEBUG_PRINTLN("Publish MQTT");
char s[10];
@ -47,24 +49,24 @@ void publishMQTT()
sprintf(s, "%ld", bri);
strcpy(subuf, mqttDeviceTopic);
strcat(subuf, "/g");
mqtt.publish(subuf, s);
mqtt->publish(subuf, s);
sprintf(s, "#%X", white*16777216 + col[0]*65536 + col[1]*256 + col[2]);
strcpy(subuf, mqttDeviceTopic);
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+
//it will publish the API response to MQTT
/*XML_response(false);
strcpy(subuf, mqttDeviceTopic);
strcat(subuf, "/v");
mqtt.publish(subuf, obuf);*/
mqtt->publish(subuf, obuf);*/
}
bool reconnectMQTT()
{
if (mqtt.connect(escapedMac.c_str()))
if (mqtt->connect(escapedMac.c_str()))
{
//re-subscribe to required topics
char subuf[38];
@ -73,26 +75,26 @@ bool reconnectMQTT()
if (mqttDeviceTopic[0] != 0)
{
strcpy(subuf, mqttDeviceTopic);
mqtt.subscribe(subuf);
mqtt->subscribe(subuf);
strcat(subuf, "/col");
mqtt.subscribe(subuf);
mqtt->subscribe(subuf);
strcpy(subuf, mqttDeviceTopic);
strcat(subuf, "/api");
mqtt.subscribe(subuf);
mqtt->subscribe(subuf);
}
if (mqttGroupTopic[0] != 0)
{
strcpy(subuf, mqttGroupTopic);
mqtt.subscribe(subuf);
mqtt->subscribe(subuf);
strcat(subuf, "/col");
mqtt.subscribe(subuf);
mqtt->subscribe(subuf);
strcpy(subuf, mqttGroupTopic);
strcat(subuf, "/api");
mqtt.subscribe(subuf);
mqtt->subscribe(subuf);
}
}
return mqtt.connected();
return mqtt->connected();
}
bool initMQTT()
@ -103,11 +105,11 @@ bool initMQTT()
IPAddress mqttIP;
if (mqttIP.fromString(mqttServer)) //see if server is IP or domain
{
mqtt.setServer(mqttIP,1883);
mqtt->setServer(mqttIP,1883);
} else {
mqtt.setServer(mqttServer,1883);
mqtt->setServer(mqttServer,1883);
}
mqtt.setCallback(callbackMQTT);
mqtt->setCallback(callbackMQTT);
DEBUG_PRINTLN("MQTT ready.");
return true;
}
@ -117,7 +119,7 @@ void handleMQTT()
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
if (!mqtt.connected() && millis() - lastMQTTReconnectAttempt > 5000 + (5000 * mqttFailedConAttempts * mqttFailedConAttempts))
if (!mqtt->connected() && millis() - lastMQTTReconnectAttempt > 5000 + (5000 * mqttFailedConAttempts * mqttFailedConAttempts))
{
DEBUG_PRINTLN("Attempting to connect MQTT...");
lastMQTTReconnectAttempt = millis();
@ -130,5 +132,5 @@ void handleMQTT()
DEBUG_PRINTLN("MQTT con!");
mqttFailedConAttempts = 0;
}
mqtt.loop();
mqtt->loop();
}