large changes to wifi handling. code cleanup, more efficent connecting, only one connect retry in deepsleep. better serial logging for connections and APmode. always enable ap mode when wifi is disconnected

This commit is contained in:
Edwin Eefting
2017-06-02 02:02:19 +02:00
parent d07bb3574a
commit ea5168c08a
6 changed files with 174 additions and 104 deletions
+8 -1
View File
@@ -440,7 +440,7 @@ void ExecuteCommand(byte source, const char *Line)
if (strcasecmp_P(Command, PSTR("WifiConnect")) == 0)
{
success = true;
WifiConnect(true, 1);
WifiConnect(1);
}
if (strcasecmp_P(Command, PSTR("WifiDisconnect")) == 0)
@@ -449,6 +449,13 @@ void ExecuteCommand(byte source, const char *Line)
WifiDisconnect();
}
if (strcasecmp_P(Command, PSTR("WifiAPMode")) == 0)
{
WifiAPMode(true);
success = true;
}
if (strcasecmp_P(Command, PSTR("Reboot")) == 0)
{
success = true;
+7 -4
View File
@@ -591,7 +591,6 @@ unsigned long timerwd;
unsigned long lastSend;
unsigned int NC_Count = 0;
unsigned int C_Count = 0;
boolean AP_Mode = false;
byte cmd_within_mainloop = 0;
unsigned long connectionFailures;
unsigned long wdcounter = 0;
@@ -710,8 +709,12 @@ void setup()
WiFi.persistent(false); // Do not use SDK storage of SSID/WPA parameters
WifiAPconfig();
if (!WifiConnect(true,3))
WifiConnect(false,3);
//only one attempt in deepsleep, to conserve battery
if (Settings.deepSleep)
WifiConnect(1);
else
WifiConnect(3);
#ifdef FEATURE_REPORTING
ReportStatus();
@@ -816,7 +819,7 @@ void loop()
if (wifiSetupConnect)
{
// try to connect for setup wizard
WifiConnect(true,1);
WifiConnect(1);
wifiSetupConnect = false;
}
+12 -9
View File
@@ -378,15 +378,8 @@ void statusLED(boolean traffic)
}
else
{
if (AP_Mode) //apmode is active
{
nStatusValue = ((millis()>>1) & PWMRANGE) - (PWMRANGE>>2); //ramp up for 2 sec, 3/4 luminosity
}
else if (WiFi.status() != WL_CONNECTED)
{
nStatusValue = (millis()>>1) & (PWMRANGE>>2); //ramp up for 1/2 sec, 1/4 luminosity
}
else //connected
if (WiFi.status() == WL_CONNECTED)
{
long int delta=millis()-gnLastUpdate;
if (delta>0 || delta<0 )
@@ -396,6 +389,16 @@ void statusLED(boolean traffic)
gnLastUpdate=millis();
}
}
//AP mode is active
else if (WifiIsAP())
{
nStatusValue = ((millis()>>1) & PWMRANGE) - (PWMRANGE>>2); //ramp up for 2 sec, 3/4 luminosity
}
//Disconnected
else
{
nStatusValue = (millis()>>1) & (PWMRANGE>>2); //ramp up for 1/2 sec, 1/4 luminosity
}
}
nStatusValue = constrain(nStatusValue, 0, PWMRANGE);
+115 -90
View File
@@ -11,35 +11,52 @@ void WifiAPconfig()
// setup ssid for AP Mode when needed
WiFi.softAP(ap_ssid, SecuritySettings.WifiAPKey);
// We start in STA mode
WiFi.mode(WIFI_STA);
WifiAPMode(false);
}
bool WifiIsAP()
{
byte wifimode = wifi_get_opmode();
return(wifimode == 2 || wifimode == 3); //apmode is enabled
}
//********************************************************************************
// Set Wifi AP Mode
//********************************************************************************
void WifiAPMode(boolean state)
{
if (state)
if (WifiIsAP())
{
AP_Mode = true;
WiFi.mode(WIFI_AP_STA);
//want to disable?
if (!state)
{
WiFi.mode(WIFI_STA);
addLog(LOG_LEVEL_INFO, F("WIFI : AP Mode disabled"));
}
}
else
{
AP_Mode = false;
WiFi.mode(WIFI_STA);
//want to enable?
if (state)
{
WiFi.mode(WIFI_AP_STA);
String log=F("WIFI : AP Mode enabled, reachable on ");
log=log+apIP.toString();
addLog(LOG_LEVEL_INFO, log);
}
}
}
//********************************************************************************
// Connect to Wifi AP
// Configure network and connect to Wifi SSID and SSID2
//********************************************************************************
boolean WifiConnect(boolean primary, byte connectAttempts)
boolean WifiConnect(byte connectAttempts)
{
String log = "";
//replace spaces in hostname with dashes, and set station hostname (for dhcp)
char hostName[sizeof(Settings.Name)];
strcpy(hostName,Settings.Name);
for(byte x=0; x< sizeof(hostName); x++)
@@ -47,6 +64,7 @@ boolean WifiConnect(boolean primary, byte connectAttempts)
hostName[x] = '-';
wifi_station_set_hostname(hostName);
//use static ip?
if (Settings.IP[0] != 0 && Settings.IP[0] != 255)
{
char str[20];
@@ -61,87 +79,101 @@ boolean WifiConnect(boolean primary, byte connectAttempts)
WiFi.config(ip, gw, subnet, dns);
}
if (WiFi.status() != WL_CONNECTED)
//try to connect to one of the access points
if (WifiConnectSSID(SecuritySettings.WifiSSID, SecuritySettings.WifiKey, connectAttempts) ||
WifiConnectSSID(SecuritySettings.WifiSSID2, SecuritySettings.WifiKey2, connectAttempts))
{
if ((SecuritySettings.WifiSSID[0] != 0) && (strcasecmp(SecuritySettings.WifiSSID, "ssid") != 0))
// fix octet?
if (Settings.IP_Octet != 0 && Settings.IP_Octet != 255)
{
for (byte tryConnect = 1; tryConnect <= connectAttempts; tryConnect++)
IPAddress ip = WiFi.localIP();
IPAddress gw = WiFi.gatewayIP();
IPAddress subnet = WiFi.subnetMask();
ip[3] = Settings.IP_Octet;
log = F("IP : Fixed IP octet:");
log += ip;
addLog(LOG_LEVEL_INFO, log);
WiFi.config(ip, gw, subnet);
}
return(true);
}
addLog(LOG_LEVEL_ERROR, F("WIFI : Could not connect to AP!"));
//everything failed, activate AP mode (will deactivate automaticly after a while if its connected again)
WifiAPMode(true);
return(false);
}
//********************************************************************************
// Connect to Wifi specific SSID
//********************************************************************************
boolean WifiConnectSSID(char WifiSSID[], char WifiKey[], byte connectAttempts)
{
String log;
//already connected, need to disconnect first
if (WiFi.status() == WL_CONNECTED)
return(true);
//no ssid specified
if ((WifiSSID[0] == 0) || (strcasecmp(WifiSSID, "ssid") == 0))
return(false);
for (byte tryConnect = 1; tryConnect <= connectAttempts; tryConnect++)
{
log = F("WIFI : Connecting ");
log += WifiSSID;
log += F(" attempt #");
log += tryConnect;
addLog(LOG_LEVEL_INFO, log);
if (tryConnect == 1)
WiFi.begin(WifiSSID, WifiKey);
else
WiFi.begin();
//wait until it connects
for (byte x = 0; x < 200; x++)
{
if (WiFi.status() != WL_CONNECTED)
{
log = F("WIFI : Connecting... ");
log += tryConnect;
addLog(LOG_LEVEL_INFO, log);
if (tryConnect == 1)
{
if (primary)
WiFi.begin(SecuritySettings.WifiSSID, SecuritySettings.WifiKey);
else
WiFi.begin(SecuritySettings.WifiSSID2, SecuritySettings.WifiKey2);
}
else
WiFi.begin();
for (byte x = 0; x < 200; x++)
{
if (WiFi.status() != WL_CONNECTED)
{
statusLED(false);
delay(50);
}
else
break;
}
if (WiFi.status() == WL_CONNECTED)
{
log = F("WIFI : Connected! IP: ");
IPAddress ip = WiFi.localIP();
char str[20];
sprintf_P(str, PSTR("%u.%u.%u.%u"), ip[0], ip[1], ip[2], ip[3]);
log += str;
addLog(LOG_LEVEL_INFO, log);
statusLED(true);
break;
}
else
{
log = F("WIFI : Disconnecting!");
addLog(LOG_LEVEL_INFO, log);
ETS_UART_INTR_DISABLE();
wifi_station_disconnect();
ETS_UART_INTR_ENABLE();
for (byte x = 0; x < 20; x++)
{
statusLED(true);
delay(50);
}
}
statusLED(false);
delay(50);
}
else
break;
}
// fix ip if last octet is set
if (Settings.IP_Octet != 0 && Settings.IP_Octet != 255)
{
IPAddress ip = WiFi.localIP();
IPAddress gw = WiFi.gatewayIP();
IPAddress subnet = WiFi.subnetMask();
ip[3] = Settings.IP_Octet;
log = F("IP : Fixed IP :");
log += ip;
addLog(LOG_LEVEL_INFO, log);
WiFi.config(ip, gw, subnet);
}
if (WiFi.status() == WL_CONNECTED)
{
log = F("WIFI : Connected! IP: ");
IPAddress ip = WiFi.localIP();
char str[20];
sprintf_P(str, PSTR("%u.%u.%u.%u"), ip[0], ip[1], ip[2], ip[3]);
log += str;
addLog(LOG_LEVEL_INFO, log);
statusLED(true);
return(true);
}
else
{
log = F("WIFI : No SSID!");
addLog(LOG_LEVEL_INFO, log);
NC_Count = 1;
WifiAPMode(true);
// log = F("WIFI : Disconnecting!");
// addLog(LOG_LEVEL_INFO, log);
ETS_UART_INTR_DISABLE();
wifi_station_disconnect();
ETS_UART_INTR_ENABLE();
for (byte x = 0; x < 20; x++)
{
statusLED(true);
delay(50);
}
}
}
if (WiFi.status() == WL_CONNECTED)
return true;
return false;
}
@@ -203,30 +235,23 @@ void WifiCheck()
if (WiFi.status() != WL_CONNECTED)
{
NC_Count++;
//give it time to automaticly reconnect
if (NC_Count > 2)
{
if (!WifiConnect(true,2))
WifiConnect(false,2);
WifiConnect(2);
C_Count=0;
if (WiFi.status() != WL_CONNECTED)
WifiAPMode(true);
NC_Count = 0;
}
}
//connected
else
{
C_Count++;
NC_Count = 0;
if (C_Count > 2) // close AP after timeout if a Wifi connection is established...
if (C_Count > 2) // disable AP after timeout if a Wifi connection is established...
{
byte wifimode = wifi_get_opmode();
if (wifimode == 2 || wifimode == 3) //apmode is active
{
WifiAPMode(false);
log = F("WIFI : AP Mode inactive");
addLog(LOG_LEVEL_INFO, log);
}
WifiAPMode(false);
}
}
}
+5
View File
@@ -0,0 +1,5 @@
#!/bin/bash -e
source configlib
./pingserial.py $SERIAL
+27
View File
@@ -0,0 +1,27 @@
#!/usr/bin/env python
# from __future__ import print_function
import serial
import sys
import time
#wait until ESPEasy is booted and ready
s=serial.Serial(port=sys.argv[1], baudrate=115200, timeout=1, write_timeout=1)
s.reset_input_buffer();
sys.stdout.write("Waiting for serial response from ESPEASY: ")
sys.stdout.flush()
while 1:
s.write("\n");
a=True
while a!="":
a=s.readline()
if a=="Unknown command!\r\n":
print("OK")
s.reset_input_buffer();
sys.exit(0)
else:
sys.stdout.write(".")
sys.stdout.flush()