New WiFi logic

This commit is contained in:
cschwinne 2019-10-25 00:14:58 +02:00
parent 31e4e7c709
commit d13d60d752
8 changed files with 49 additions and 42 deletions

View File

@ -20,7 +20,7 @@ const char PAGE_msg[] PROGMEM = R"=====(<!DOCTYPE html>
const char PAGE_update[] PROGMEM = R"=====(<!DOCTYPE html>
<html><head><meta content='width=device-width' name='viewport'><title>WLED Update</title><script>function B(){window.history.back()}</script>
%CSS%.bt{background:var(--bCol);color:var(--tCol);font-family:var(--cFn),sans-serif;border:.3ch solid var(--bCol);display:inline-block;filter:drop-shadow(-5px -5px 5px var(--sCol));font-size:20px;margin:8px;margin-top:12px}input[type=file]{font-size:16px}body{font-family:var(--cFn),sans-serif;text-align:center;background:var(--cCol);color:var(--tCol);line-height:200%%}</style></head>
<body><h2>WLED Software Update</h2>Installed version: 0.8.5<br>Download the latest binary: <a href="https://github.com/Aircoookie/WLED/releases"><img src="https://img.shields.io/github/release/Aircoookie/WLED.svg?style=flat-square"></a><br><form method='POST' action='/update' enctype='multipart/form-data'><input type='file' class="bt" name='update' required><br><input type='submit' class="bt" value='Update!'></form><button type="button" class="bt" onclick="B()">Back</button></body></html>)=====";
<body><h2>WLED Software Update</h2>Installed version: 0.8.6<br>Download the latest binary: <a href="https://github.com/Aircoookie/WLED/releases"><img src="https://img.shields.io/github/release/Aircoookie/WLED.svg?style=flat-square"></a><br><form method='POST' action='/update' enctype='multipart/form-data'><input type='file' class="bt" name='update' required><br><input type='submit' class="bt" value='Update!'></form><button type="button" class="bt" onclick="B()">Back</button></body></html>)=====";
//new user welcome page

View File

@ -28,7 +28,7 @@ const char PAGE_settings_wifi[] PROGMEM = R"=====(<!DOCTYPE html>
%CSS%%SCSS%</head><body onload="GetV()">
<form id="form_s" name="Sf" method="post">
<div class="helpB"><button type="button" onclick="H()">?</button></div>
<button type="button" onclick="B()">Back</button><button type="submit">Save & Reboot</button><hr>
<button type="button" onclick="B()">Back</button><button type="submit">Save & Connect</button><hr>
<h2>WiFi setup</h2>
<h3>Connect to existing network</h3>
Network name (SSID, empty to not connect): <br><input name="CS" maxlength="32"><br>
@ -50,15 +50,20 @@ Static subnet mask:<br>
<input name="S3" type="number" min="0" max="255" required><br>
mDNS address (leave empty for no mDNS):<br/>
http:// <input name="CM" maxlength="32"> .local<br>
Try connecting before opening AP for: <input name="AT" type="number" min="0" max="255" required> s <br>
Client IP: <span class="sip"> Not connected </span><br>
<h3>Configure Access Point</h3>
AP SSID (leave empty for no AP):<br><input name="AS" maxlength="32"><br>
Hide AP name: <input type="checkbox" name="AH"><br>
AP password (leave empty for open):<br> <input type="password" name="AP" maxlength="63"><br>
Access Point WiFi channel: <input name="AC" type="number" min="1" max="13" required><br>
AP opens:
<select name="AB">
<option value="0">No connection after boot</option>
<option value="1">Disconnected</option>
<option value="2">Always</option>
<option value="3">Never (not recommended)</option></select><br>
AP IP: <span class="sip"> Not active </span><hr>
<button type="button" onclick="B()">Back</button><button type="submit">Save & Reboot</button>
<button type="button" onclick="B()">Back</button><button type="submit">Save & Connect</button>
</form>
</body>
</html>)=====";
@ -390,9 +395,6 @@ The password should be changed when OTA is enabled.<br>
<b>Disable OTA when not in use, otherwise an attacker can reflash device software!</b><br>
<i>Settings on this page are only changable if OTA lock is disabled!</i><br>
Deny access to WiFi settings if locked: <input type="checkbox" name="OW"><br><br>
Disable recovery AP: <input type="checkbox" name="NA"><br>
In case of an error there will be no wireless recovery possible!<br>
Completely disables all Access Point functions.<br><br>
Factory reset: <input type="checkbox" name="RS"><br>
All EEPROM content (settings) will be erased.<br><br>
HTTP traffic is unencrypted. An attacker in the same network can intercept form data!
@ -400,7 +402,7 @@ HTTP traffic is unencrypted. An attacker in the same network can intercept form
<button type="button" onclick="U()">Manual OTA Update</button><br>
Enable ArduinoOTA: <input type="checkbox" name="AO"><br>
<h3>About</h3>
<a href="https://github.com/Aircoookie/WLED" target="_blank">WLED</a> version 0.8.5<br><br>
<a href="https://github.com/Aircoookie/WLED" target="_blank">WLED</a> version 0.8.6<br><br>
<a href="https://github.com/Aircoookie/WLED/wiki/Contributors-&-About" target="_blank">Contributors, dependencies and special thanks</a><br>
A huge thank you to everyone who helped me create WLED!<br><br>
(c) 2016-2019 Christian Schwinne <br>

View File

@ -3,7 +3,7 @@
*/
/*
* @title WLED project sketch
* @version 0.8.5-dev
* @version 0.8.6
* @author Christian Schwinne
*/
@ -30,7 +30,7 @@
//#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
#define WLED_DEBUG
//#define WLED_DEBUG
//library inclusions
@ -99,8 +99,8 @@
//version code in format yymmddb (b = daily build)
#define VERSION 1910201
char versionString[] = "0.8.5";
#define VERSION 1910251
char versionString[] = "0.8.6";
//AP and OTA default passwords (for maximum change them!)
@ -123,9 +123,9 @@ char cmDNS[33] = "x"; //mDNS address (placeholder, will
char apSSID[33] = ""; //AP off by default (unless setup)
byte apChannel = 1; //2.4GHz WiFi AP channel (1-13)
byte apHide = 0; //hidden AP SSID
byte apWaitTimeSecs = 32; //time to wait for connection before opening AP
bool apAlwaysOn = false;
bool recoveryAPDisabled = false; //never open AP (not recommended)
//byte apWaitTimeSecs = 32; //time to wait for connection before opening AP
byte apBehavior = 0; //0: Open AP when no connection after boot 1: Open when no connection 2: Always open 3: Only when button pressed for 6 sec
//bool recoveryAPDisabled = false; //never open AP (not recommended)
IPAddress staticIP(0, 0, 0, 0); //static IP of ESP
IPAddress staticGateway(0, 0, 0, 0); //gateway (router) IP
IPAddress staticSubnet(255, 255, 255, 0); //most common subnet in home networks
@ -335,8 +335,6 @@ unsigned long hueLastRequestSent = 0;
bool hueAuthRequired = false;
bool hueReceived = false;
bool hueStoreAllowed = false, hueNewKey = false;
//unsigned long huePollIntervalMsTemp = huePollIntervalMs;
//bool hueAttempt = false;
//overlays
byte overlayCurrent = overlayDefault;

View File

@ -134,8 +134,8 @@ void saveSettingsToEEPROM()
EEPROM.write(372, useRGBW);
EEPROM.write(373, effectPaletteDefault);
EEPROM.write(374, strip.paletteFade);
EEPROM.write(375, apWaitTimeSecs);
EEPROM.write(376, recoveryAPDisabled);
//EEPROM.write(375, apWaitTimeSecs);
EEPROM.write(376, apBehavior);
EEPROM.write(377, EEPVER); //eeprom was updated to latest
@ -365,8 +365,8 @@ void loadSettingsFromEEPROM(bool first)
//374 - strip.paletteFade
if (lastEEPROMversion > 0) {
apWaitTimeSecs = EEPROM.read(375);
recoveryAPDisabled = EEPROM.read(376);
//apWaitTimeSecs = EEPROM.read(375);
apBehavior = EEPROM.read(376);
}
//377 = lastEEPROMversion
if (lastEEPROMversion > 1) {

View File

@ -187,7 +187,7 @@ void getSettingsJS(byte subPage, char* dest)
}
sappends('s',"CM",cmDNS);
sappend('v',"AT",apWaitTimeSecs);
sappend('i',"AB",apBehavior);
sappends('s',"AS",apSSID);
sappend('c',"AH",apHide);
@ -390,7 +390,6 @@ void getSettingsJS(byte subPage, char* dest)
sappend('c',"NO",otaLock);
sappend('c',"OW",wifiLock);
sappend('c',"AO",aOtaEnabled);
sappend('c',"NA",recoveryAPDisabled);
sappends('m',"(\"msg\")[0]","WLED ");
olen -= 2; //delete ";
oappend(versionString);

View File

@ -28,12 +28,12 @@ void handleSettingsSet(AsyncWebServerRequest *request, byte subPage)
strcpy(cmDNS, request->arg("CM").c_str());
int t = request->arg("AT").toInt(); if (t > 9 && t <= 255) apWaitTimeSecs = t;
apBehavior = request->arg("AB").toInt();
strcpy(apSSID, request->arg("AS").c_str());
apHide = request->hasArg("AH");
int passlen = request->arg("AP").length();
if (passlen == 0 || (passlen > 7 && request->arg("AP").charAt(0) != '*')) strcpy(apPass, request->arg("AP").c_str());
t = request->arg("AC").toInt(); if (t > 0 && t < 14) apChannel = t;
int t = request->arg("AC").toInt(); if (t > 0 && t < 14) apChannel = t;
char k[3]; k[2] = 0;
for (int i = 0; i<4; i++)
@ -304,7 +304,6 @@ void handleSettingsSet(AsyncWebServerRequest *request, byte subPage)
{
otaLock = request->hasArg("NO");
wifiLock = request->hasArg("OW");
recoveryAPDisabled = request->hasArg("NA");
aOtaEnabled = request->hasArg("AO");
}
}

View File

@ -122,15 +122,14 @@ void beginStrip()
void initAP(bool resetAP=false){
if (recoveryAPDisabled) return;
bool set = apSSID[0];
if (!set || resetAP) strcpy(apSSID, "WLED-AP");
if (apBehavior == 3 && !resetAP) return;
if (!apSSID[0] || resetAP) strcpy(apSSID, "WLED-AP");
if (resetAP) strcpy(apPass,"wled1234");
DEBUG_PRINT("Opening access point ");
DEBUG_PRINTLN(apSSID);
WiFi.softAPConfig(IPAddress(4, 3, 2, 1), IPAddress(4, 3, 2, 1), IPAddress(255,255,255,0));
WiFi.softAP(apSSID, apPass, apChannel, apHide);
if (!set) apSSID[0] = 0;
if (!apActive) //start captive portal if AP active
{
@ -168,7 +167,7 @@ void initConnection()
if (!apActive) initAP(); //instantly go to ap mode
return;
} else if (!apActive) {
if (apAlwaysOn)
if (apBehavior == 2)
{
initAP();
} else
@ -194,7 +193,6 @@ void initConnection()
void initInterfaces() {
DEBUG_PRINTLN("Init STA interfaces");
server.begin();
if (hueIP[0] == 0)
{
@ -207,7 +205,7 @@ void initInterfaces() {
if (alexaEnabled) alexaInit();
#ifndef WLED_DISABLE_OTA
if (aOtaEnabled) ArduinoOTA.begin();
if (aOtaEnabled) ArduinoOTA.begin(false);
#endif
strip.service();
@ -216,16 +214,21 @@ void initInterfaces() {
{
if (MDNS.begin(cmDNS))
{
DEBUG_PRINTLN("mDNS started");
MDNS.addService("http", "tcp", 80);
MDNS.addService("wled", "tcp", 80);
DEBUG_PRINTLN("mDNS started");
if (aOtaEnabled) MDNS.enableArduino(8266);
} else {
DEBUG_PRINTLN("mDNS failed!");
}
DEBUG_PRINTLN("mDNS started");
}
strip.service();
server.begin();
if (udpPort > 0 && udpPort != ntpLocalPort)
{
udpConnected = notifierUdp.begin(udpPort);
if (udpConnected && udpRgbPort != udpPort) udpRgbConnected = rgbUdp.begin(udpRgbPort);
}
if (ntpEnabled && WLED_CONNECTED)
ntpConnected = ntpUdp.begin(ntpLocalPort);
@ -239,9 +242,14 @@ void initInterfaces() {
byte stacO = 0;
void handleConnection() {
//TODO: reconnect if heap <8000
byte stac = 0;
#ifdef ESP8266
stac = wifi_softap_get_station_num();
#else
wifi_sta_list_t stationList;
esp_wifi_ap_get_sta_list(&stationList);
stac = stationList.num;
#endif
if (stac != stacO)
{
@ -267,7 +275,7 @@ void handleConnection() {
initConnection();
}
if (millis() - lastReconnectAttempt > 300000 && WLED_WIFI_CONFIGURED) initConnection();
if (!apActive && millis() - lastReconnectAttempt > apWaitTimeSecs*1000) initAP();
if (!apActive && millis() - lastReconnectAttempt > 12000) initAP();
} else if (!interfacesInited) { //newly connected
DEBUG_PRINTLN("");
DEBUG_PRINT("Connected! IP address: ");
@ -276,12 +284,12 @@ void handleConnection() {
userConnected();
//shut down AP
if (!apAlwaysOn && apActive)
if (apBehavior != 2 && apActive)
{
dnsServer.stop();
DEBUG_PRINTLN("Access point disabled.");
WiFi.softAPdisconnect(true);
apActive = false;
DEBUG_PRINTLN("Access point disabled.");
}
}
}

View File

@ -44,7 +44,7 @@ void onMqttConnect(bool sessionPresent)
}
doSendHADiscovery = true;
//doPublishMqtt = true;
doPublishMqtt = true;
DEBUG_PRINTLN("MQTT ready");
}
@ -98,6 +98,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":[)=====";
char buffer[2400]; //TODO: this is a TERRIBLE waste of precious memory, local var leads to exception though. Maybe dynamic allocation, but it is unclear when to free
void sendHADiscoveryMQTT()
{
@ -138,7 +140,7 @@ Send out HA MQTT Discovery message on MQTT connect (~2.4kB):
doSendHADiscovery = false;
if (mqtt == nullptr || !mqtt->connected()) return;
char bufc[36], bufcol[38], bufg[36], bufapi[38], buffer[2500];
char bufc[36], bufcol[38], bufg[36], bufapi[38];
strcpy(bufc, mqttDeviceTopic);
strcpy(bufcol, mqttDeviceTopic);
@ -211,7 +213,6 @@ Send out HA MQTT Discovery message on MQTT connect (~2.4kB):
strcat(pubt, mqttClientID);
strcat(pubt, "/config");
DEBUG_PRINTLN(mqtt->publish(pubt, 0, true, buffer));
yield();
#endif
}