From 547080acf5005e706c281f809268db64709c0c7e Mon Sep 17 00:00:00 2001 From: Fabian Schlenz Date: Fri, 29 Nov 2019 21:24:10 +0100 Subject: [PATCH] Main: Fix WiFi connection stuff for multiple WiFis. --- src/main.cpp | 52 +++++++++++++++++++++++----------------------------- 1 file changed, 23 insertions(+), 29 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 8b195e9..55e2e22 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -2,6 +2,7 @@ #include #include #include +#include #include #include "main.h" #include "config.h" @@ -19,34 +20,22 @@ HTTPServer* http_server; uint8_t SPIMaster::state = 0; -bool connect_to_wifi(String ssid, String pass) { - TRACE("Connecting to wifi \"%s\"...\n", ssid.c_str()); - WiFi.mode(WIFI_AP_STA); - WiFi.begin(ssid.c_str(), pass.c_str()); - if (WiFi.waitForConnectResult() != WL_CONNECTED) { - DEBUG("Could not connect to wifi \"%s\".\n", ssid.c_str()); - return false; - } else { - INFO("Connected to \"%s\". IP address: %s\n", ssid.c_str(), WiFi.localIP().toString().c_str()); - } - return true; -} - void wifi_connect() { - bool connected = false; INFO("Connecting to WiFi...\n"); + WiFiMulti wifi; SPIMaster::select_sd(); if (SD.exists("/_wifis.txt")) { DEBUG("Reading /_wifis.txt\n"); File f = SD.open("/_wifis.txt", "r"); while (String line = f.readStringUntil('\n')) { - if (line.length()==0 || line.startsWith("#") || line.indexOf('=')==-1) { + if (line.length()==0) { + break; + } else if (line.startsWith("#") || line.indexOf('=')==-1) { continue; } String ssid = line.substring(0, line.indexOf('=')); String pass = line.substring(line.indexOf('=')+1); - connected = connect_to_wifi(ssid, pass); - if (connected) break; + wifi.addAP(ssid.c_str(), pass.c_str()); } f.close(); } else { @@ -55,22 +44,21 @@ void wifi_connect() { f.close(); } SPIMaster::select_sd(false); - - if (!connected) { - #if defined(WIFI_SSID) and defined(WIFI_PASS) - DEBUG("Trying hardcoded WiFi data...\n"); - connected = connect_to_wifi(WIFI_SSID, WIFI_PASS); - #else - DEBUG("No hardcoded WiFi data set.\n"); - #endif - } - if (!connected) { - INFO("No WiFi connection!\n"); + + #if defined(WIFI_SSID) and defined(WIFI_PASS) + wifi.addAP(WIFI_SSID, WIFI_PASS); + #endif + + if (wifi.run() == WL_CONNECTED) { + DEBUG("Connected to WiFi \"%s\".\n", WiFi.SSID().c_str()); + } else { + DEBUG("No WiFi connection!\n"); } } void setup() { - delay(500); + // Small delay to give the Serial console a bit of time to connect. + delay(1000); Serial.begin(115200); Serial.println("Starting..."); Serial.println("Started."); @@ -125,6 +113,12 @@ void setup() { } else { INFO("Could not fetch current time via NTP.\n"); } + + #ifdef VERSION + INFO("ESMP3 version %s (OTA_VERSION %d)\n", VERSION, OTA_VERSION); + #else + INFO("ESMP3, version unknown (OTA_VERSION %d)\n", OTA_VERSION); + #endif INFO("Initialization completed.\n"); }