background wifi scan/connected/reconnect. partial code, incomplete, for testing station mode only (wifi=1)
This commit is contained in:
parent
9f822df519
commit
2959dfad4f
|
|
@ -57,7 +57,9 @@ const String sondeTypeSelect(int activeType) {
|
|||
sts += "<option value=\"";
|
||||
sts += sondeTypeStr[i];
|
||||
sts += "\"";
|
||||
if(activeType==i) { sts += " selected"; }
|
||||
if (activeType == i) {
|
||||
sts += " selected";
|
||||
}
|
||||
sts += ">";
|
||||
sts += sondeTypeStr[i];
|
||||
sts += "</option>";
|
||||
|
|
@ -91,9 +93,15 @@ void setupChannelList() {
|
|||
*space = 0;
|
||||
float freq = atof(line.c_str());
|
||||
SondeType type;
|
||||
if(space[1]=='4') { type=STYPE_RS41; }
|
||||
else if (space[1]=='9') { type=STYPE_DFM09; }
|
||||
else if (space[1]=='6') { type=STYPE_DFM06; }
|
||||
if (space[1] == '4') {
|
||||
type = STYPE_RS41;
|
||||
}
|
||||
else if (space[1] == '9') {
|
||||
type = STYPE_DFM09;
|
||||
}
|
||||
else if (space[1] == '6') {
|
||||
type = STYPE_DFM06;
|
||||
}
|
||||
else continue;
|
||||
int active = space[3] == '+' ? 1 : 0;
|
||||
char *launchsite = strchr(line.c_str(), ' ');
|
||||
|
|
@ -164,7 +172,10 @@ const char *handleQRGPost(AsyncWebServerRequest *request) {
|
|||
|
||||
#define MAX_WIFI 10
|
||||
int nNetworks;
|
||||
struct { String id; String pw; } networks[MAX_WIFI];
|
||||
struct {
|
||||
String id;
|
||||
String pw;
|
||||
} networks[MAX_WIFI];
|
||||
|
||||
// FIXME: For now, we don't uspport wifi networks that contain newline or null characters
|
||||
// ... would require a more sophisicated file format (currently one line SSID; one line Password
|
||||
|
|
@ -185,7 +196,11 @@ void setupWifiList() {
|
|||
}
|
||||
nNetworks = i;
|
||||
Serial.print(i); Serial.println(" networks in networks.txt\n");
|
||||
for(int j=0; j<i; j++) { Serial.print(networks[j].id); Serial.print(": "); Serial.println(networks[j].pw); }
|
||||
for (int j = 0; j < i; j++) {
|
||||
Serial.print(networks[j].id);
|
||||
Serial.print(": ");
|
||||
Serial.println(networks[j].pw);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
|
@ -359,6 +374,7 @@ const char *createConfigForm() {
|
|||
|
||||
const char* PARAM_MESSAGE = "message";
|
||||
void SetupAsyncServer() {
|
||||
server.reset();
|
||||
// Route for root / web page
|
||||
server.on("/", HTTP_GET, [](AsyncWebServerRequest * request) {
|
||||
request->send(SPIFFS, "/index.html", String(), false, processor);
|
||||
|
|
@ -411,6 +427,19 @@ void SetupAsyncServer() {
|
|||
server.begin();
|
||||
}
|
||||
|
||||
int fetchWifiIndex(const char *id) {
|
||||
for (int i = 0; i < nNetworks; i++) {
|
||||
if (strcmp(id, networks[i].id.c_str()) == 0) return i;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
const char *fetchWifiSSID(int i) {
|
||||
return networks[i].id.c_str();
|
||||
}
|
||||
const char *fetchWifiPw(int i) {
|
||||
return networks[i].pw.c_str();
|
||||
}
|
||||
|
||||
const char *fetchWifiPw(const char *id) {
|
||||
for (int i = 0; i < nNetworks; i++) {
|
||||
|
|
@ -448,8 +477,12 @@ void IRAM_ATTR buttonISR() {
|
|||
} else { //Button up
|
||||
unsigned int elapsed = millis() - button1.press_ts;
|
||||
if (elapsed > 1500) {
|
||||
if(elapsed<4000) { button1.pressed=KP_MID; }
|
||||
else { button1.pressed=KP_LONG; }
|
||||
if (elapsed < 4000) {
|
||||
button1.pressed = KP_MID;
|
||||
}
|
||||
else {
|
||||
button1.pressed = KP_LONG;
|
||||
}
|
||||
} else {
|
||||
if (button1.doublepress) button1.pressed = KP_DOUBLE;
|
||||
else button1.pressed = KP_SHORT;
|
||||
|
|
@ -593,11 +626,12 @@ void setup()
|
|||
// == setup default channel list if qrg.txt read fails =========== //
|
||||
|
||||
sonde.setup();
|
||||
|
||||
WiFi.onEvent(WiFiEvent);
|
||||
}
|
||||
|
||||
enum MainState { ST_DECODER, ST_SCANNER, ST_SPECTRUM, ST_WIFISCAN };
|
||||
|
||||
static MainState mainState = ST_WIFISCAN;
|
||||
static MainState mainState = ST_DECODER; // ST_WIFISCAN;
|
||||
|
||||
void enterMode(int mode) {
|
||||
mainState = (MainState)mode;
|
||||
|
|
@ -707,27 +741,210 @@ String translateEncryptionType(wifi_auth_mode_t encryptionType) {
|
|||
}
|
||||
}
|
||||
|
||||
//wifi event handler
|
||||
void WiFiEvent(WiFiEvent_t event){
|
||||
switch(event) {
|
||||
case SYSTEM_EVENT_STA_GOT_IP:
|
||||
//When connected set
|
||||
Serial.print("WiFi connected! IP address: ");
|
||||
Serial.println(WiFi.localIP());
|
||||
//initializes the UDP state
|
||||
//This initializes the transfer buffer
|
||||
void enableNetwork(bool enable) {
|
||||
if(enable) {
|
||||
SetupAsyncServer();
|
||||
udp.begin(WiFi.localIP(), LOCALUDPPORT);
|
||||
connected = true;
|
||||
} else {
|
||||
connected = false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
enum t_wifi_state { WIFI_DISABLED, WIFI_SCAN, WIFI_CONNECT, WIFI_CONNECTED };
|
||||
|
||||
static t_wifi_state wifi_state = WIFI_DISABLED;
|
||||
|
||||
// Events used only for debug output right now
|
||||
void WiFiEvent(WiFiEvent_t event)
|
||||
{
|
||||
Serial.printf("[WiFi-event] event: %d\n", event);
|
||||
|
||||
switch (event) {
|
||||
case SYSTEM_EVENT_WIFI_READY:
|
||||
Serial.println("WiFi interface ready");
|
||||
break;
|
||||
case SYSTEM_EVENT_SCAN_DONE:
|
||||
Serial.println("Completed scan for access points");
|
||||
break;
|
||||
case SYSTEM_EVENT_STA_START:
|
||||
Serial.println("WiFi client started");
|
||||
break;
|
||||
case SYSTEM_EVENT_STA_STOP:
|
||||
Serial.println("WiFi clients stopped");
|
||||
break;
|
||||
case SYSTEM_EVENT_STA_CONNECTED:
|
||||
Serial.println("Connected to access point");
|
||||
break;
|
||||
case SYSTEM_EVENT_STA_DISCONNECTED:
|
||||
Serial.println("WiFi lost connection");
|
||||
connected = false;
|
||||
Serial.println("Disconnected from WiFi access point");
|
||||
if(wifi_state==WIFI_CONNECT){
|
||||
// If we get a disconnect event while waiting for connection (as I do sometimes with my FritzBox),
|
||||
// just start from scratch with WiFi scan
|
||||
wifi_state = WIFI_DISABLED;
|
||||
WiFi.disconnect(true);
|
||||
}
|
||||
break;
|
||||
case SYSTEM_EVENT_STA_AUTHMODE_CHANGE:
|
||||
Serial.println("Authentication mode of access point has changed");
|
||||
break;
|
||||
case SYSTEM_EVENT_STA_GOT_IP:
|
||||
Serial.print("Obtained IP address: ");
|
||||
Serial.println(WiFi.localIP());
|
||||
break;
|
||||
case SYSTEM_EVENT_STA_LOST_IP:
|
||||
Serial.println("Lost IP address and IP address is reset to 0");
|
||||
break;
|
||||
case SYSTEM_EVENT_STA_WPS_ER_SUCCESS:
|
||||
Serial.println("WiFi Protected Setup (WPS): succeeded in enrollee mode");
|
||||
break;
|
||||
case SYSTEM_EVENT_STA_WPS_ER_FAILED:
|
||||
Serial.println("WiFi Protected Setup (WPS): failed in enrollee mode");
|
||||
break;
|
||||
case SYSTEM_EVENT_STA_WPS_ER_TIMEOUT:
|
||||
Serial.println("WiFi Protected Setup (WPS): timeout in enrollee mode");
|
||||
break;
|
||||
case SYSTEM_EVENT_STA_WPS_ER_PIN:
|
||||
Serial.println("WiFi Protected Setup (WPS): pin code in enrollee mode");
|
||||
break;
|
||||
case SYSTEM_EVENT_AP_START:
|
||||
Serial.println("WiFi access point started");
|
||||
break;
|
||||
case SYSTEM_EVENT_AP_STOP:
|
||||
Serial.println("WiFi access point stopped");
|
||||
break;
|
||||
case SYSTEM_EVENT_AP_STACONNECTED:
|
||||
Serial.println("Client connected");
|
||||
break;
|
||||
case SYSTEM_EVENT_AP_STADISCONNECTED:
|
||||
Serial.println("Client disconnected");
|
||||
break;
|
||||
case SYSTEM_EVENT_AP_STAIPASSIGNED:
|
||||
Serial.println("Assigned IP address to client");
|
||||
break;
|
||||
case SYSTEM_EVENT_AP_PROBEREQRECVED:
|
||||
Serial.println("Received probe request");
|
||||
break;
|
||||
case SYSTEM_EVENT_GOT_IP6:
|
||||
Serial.println("IPv6 is preferred");
|
||||
break;
|
||||
case SYSTEM_EVENT_ETH_START:
|
||||
Serial.println("Ethernet started");
|
||||
break;
|
||||
case SYSTEM_EVENT_ETH_STOP:
|
||||
Serial.println("Ethernet stopped");
|
||||
break;
|
||||
case SYSTEM_EVENT_ETH_CONNECTED:
|
||||
Serial.println("Ethernet connected");
|
||||
break;
|
||||
case SYSTEM_EVENT_ETH_DISCONNECTED:
|
||||
Serial.println("Ethernet disconnected");
|
||||
break;
|
||||
case SYSTEM_EVENT_ETH_GOT_IP:
|
||||
Serial.println("Obtained IP address");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int wifiConnect(int16_t res) {
|
||||
Serial.printf("WLAN scan result: found %d networks\n", res);
|
||||
|
||||
// pick best network
|
||||
int bestEntry = -1;
|
||||
int bestRSSI = INT_MIN;
|
||||
uint8_t bestBSSID[6];
|
||||
int32_t bestChannel = 0;
|
||||
|
||||
for (int8_t i = 0; i < res; i++) {
|
||||
String ssid_scan;
|
||||
int32_t rssi_scan;
|
||||
uint8_t sec_scan;
|
||||
uint8_t* BSSID_scan;
|
||||
int32_t chan_scan;
|
||||
WiFi.getNetworkInfo(i, ssid_scan, sec_scan, rssi_scan, BSSID_scan, chan_scan);
|
||||
int networkEntry = fetchWifiIndex(ssid_scan.c_str());
|
||||
if (networkEntry < 0) continue;
|
||||
if (rssi_scan <= bestRSSI) continue;
|
||||
bestEntry = networkEntry;
|
||||
bestRSSI = rssi_scan;
|
||||
bestChannel = chan_scan;
|
||||
memcpy((void*) &bestBSSID, (void*) BSSID_scan, sizeof(bestBSSID));
|
||||
}
|
||||
WiFi.scanDelete();
|
||||
if (bestEntry >= 0) {
|
||||
Serial.printf("WiFi Connecting BSSID: %02X:%02X:%02X:%02X:%02X:%02X SSID: %s PW %s Channel: %d (RSSI %d)\n", bestBSSID[0], bestBSSID[1], bestBSSID[2], bestBSSID[3], bestBSSID[4], bestBSSID[5], fetchWifiSSID(bestEntry), fetchWifiPw(bestEntry), bestChannel, bestRSSI);
|
||||
WiFi.begin(fetchWifiSSID(bestEntry), fetchWifiPw(bestEntry), bestChannel, bestBSSID);
|
||||
wifi_state = WIFI_CONNECT;
|
||||
} else {
|
||||
// rescan
|
||||
// wifiStart();
|
||||
WiFi.disconnect(true);
|
||||
wifi_state = WIFI_DISABLED;
|
||||
}
|
||||
}
|
||||
|
||||
static int wifi_cto;
|
||||
|
||||
void loopWifiBackground() {
|
||||
Serial.printf("WifiBackground: state %d\n", wifi_state);
|
||||
// handle Wifi station mode in background
|
||||
if (sonde.config.wifi == 0 || sonde.config.wifi == 2) return; // nothing to do if disabled or access point mode
|
||||
|
||||
if (wifi_state == WIFI_DISABLED) { // stopped => start can
|
||||
wifi_state = WIFI_SCAN;
|
||||
Serial.println("WLAN start scan");
|
||||
WiFi.scanNetworks(true); // scan in async mode
|
||||
} else if (wifi_state == WIFI_SCAN) {
|
||||
int16_t res = WiFi.scanComplete();
|
||||
if (res == 0 || res == WIFI_SCAN_FAILED) {
|
||||
// retry
|
||||
Serial.println("WLAN restart scan");
|
||||
WiFi.disconnect(true);
|
||||
wifi_state = WIFI_DISABLED;
|
||||
return;
|
||||
}
|
||||
if (res == WIFI_SCAN_RUNNING) {
|
||||
return;
|
||||
}
|
||||
// Scan finished, try to connect
|
||||
wifiConnect(res);
|
||||
wifi_cto = 0;
|
||||
} else if (wifi_state == WIFI_CONNECT) {
|
||||
wifi_cto++;
|
||||
if (WiFi.isConnected()) {
|
||||
wifi_state = WIFI_CONNECTED;
|
||||
// update IP in display
|
||||
sonde.setIP(WiFi.localIP().toString().c_str(), false);
|
||||
sonde.updateDisplayIP();
|
||||
enableNetwork(true);
|
||||
}
|
||||
if (wifi_cto > 20) { // failed, restart scanning
|
||||
wifi_state = WIFI_DISABLED;
|
||||
WiFi.disconnect(true);
|
||||
}
|
||||
} else if (wifi_state == WIFI_CONNECTED) {
|
||||
if (!WiFi.isConnected()) {
|
||||
sonde.clearIP();
|
||||
sonde.updateDisplayIP();
|
||||
wifi_state = WIFI_DISABLED; // restart scan
|
||||
enableNetwork(false);
|
||||
WiFi.disconnect(true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static char* _scan[2] = {"/", "\\"};
|
||||
void loopWifiScan() {
|
||||
if (sonde.config.wifi == 1) { // station mode, setup in background
|
||||
wifi_state = WIFI_DISABLED; // will start scanning in wifiLoopBackgroiund
|
||||
enterMode(ST_SCANNER);
|
||||
}
|
||||
if (sonde.config.wifi == 2) { // AP mode, setup in background
|
||||
// TODO
|
||||
}
|
||||
u8x8->setFont(u8x8_font_chroma48medium8_r);
|
||||
if (sonde.config.wifi != 0) {
|
||||
u8x8->drawString(0, 0, "WiFi Scan...");
|
||||
|
|
@ -763,9 +980,13 @@ void loopWifiScan() {
|
|||
Serial.println("-----------------------");
|
||||
id = WiFi.SSID(i).c_str();
|
||||
pw = fetchWifiPw(id);
|
||||
if(pw) { strncpy(idstr, id, 63); }
|
||||
if (pw) {
|
||||
strncpy(idstr, id, 63);
|
||||
}
|
||||
}
|
||||
if (!pw) {
|
||||
pw = "test";
|
||||
}
|
||||
if(!pw) { pw="test"; }
|
||||
Serial.print("Connecting to: "); Serial.println(idstr);
|
||||
u8x8->drawString(0, 6, "Conn:");
|
||||
u8x8->drawString(6, 6, idstr);
|
||||
|
|
@ -873,4 +1094,5 @@ void loop() {
|
|||
Serial.print(" LNA Gain: "),
|
||||
Serial.println(gain);
|
||||
#endif
|
||||
loopWifiBackground();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -153,6 +153,10 @@ void Sonde::setConfig(const char *cfg) {
|
|||
}
|
||||
}
|
||||
|
||||
void Sonde::clearIP() {
|
||||
memset(myIP_tiles, 0, 11*8);
|
||||
}
|
||||
|
||||
void Sonde::setIP(const char *ip, bool AP) {
|
||||
memset(myIP_tiles, 0, 11*8);
|
||||
int len = strlen(ip);
|
||||
|
|
|
|||
|
|
@ -92,6 +92,7 @@ public:
|
|||
void updateDisplayScanner();
|
||||
void clearDisplay();
|
||||
void setIP(const char *ip, bool isAP);
|
||||
void clearIP();
|
||||
};
|
||||
|
||||
extern Sonde sonde;
|
||||
|
|
|
|||
Loading…
Reference in New Issue