Main: Fix WiFi connection stuff for multiple WiFis.

This commit is contained in:
Fabian Schlenz 2019-11-29 21:24:10 +01:00
parent d3c699aefa
commit 547080acf5
1 changed files with 23 additions and 29 deletions

View File

@ -2,6 +2,7 @@
#include <SPI.h> #include <SPI.h>
#include <SD.h> #include <SD.h>
#include <WiFi.h> #include <WiFi.h>
#include <WiFiMulti.h>
#include <ESPmDNS.h> #include <ESPmDNS.h>
#include "main.h" #include "main.h"
#include "config.h" #include "config.h"
@ -19,34 +20,22 @@ HTTPServer* http_server;
uint8_t SPIMaster::state = 0; 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() { void wifi_connect() {
bool connected = false;
INFO("Connecting to WiFi...\n"); INFO("Connecting to WiFi...\n");
WiFiMulti wifi;
SPIMaster::select_sd(); SPIMaster::select_sd();
if (SD.exists("/_wifis.txt")) { if (SD.exists("/_wifis.txt")) {
DEBUG("Reading /_wifis.txt\n"); DEBUG("Reading /_wifis.txt\n");
File f = SD.open("/_wifis.txt", "r"); File f = SD.open("/_wifis.txt", "r");
while (String line = f.readStringUntil('\n')) { 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; continue;
} }
String ssid = line.substring(0, line.indexOf('=')); String ssid = line.substring(0, line.indexOf('='));
String pass = line.substring(line.indexOf('=')+1); String pass = line.substring(line.indexOf('=')+1);
connected = connect_to_wifi(ssid, pass); wifi.addAP(ssid.c_str(), pass.c_str());
if (connected) break;
} }
f.close(); f.close();
} else { } else {
@ -55,22 +44,21 @@ void wifi_connect() {
f.close(); f.close();
} }
SPIMaster::select_sd(false); SPIMaster::select_sd(false);
if (!connected) { #if defined(WIFI_SSID) and defined(WIFI_PASS)
#if defined(WIFI_SSID) and defined(WIFI_PASS) wifi.addAP(WIFI_SSID, WIFI_PASS);
DEBUG("Trying hardcoded WiFi data...\n"); #endif
connected = connect_to_wifi(WIFI_SSID, WIFI_PASS);
#else if (wifi.run() == WL_CONNECTED) {
DEBUG("No hardcoded WiFi data set.\n"); DEBUG("Connected to WiFi \"%s\".\n", WiFi.SSID().c_str());
#endif } else {
} DEBUG("No WiFi connection!\n");
if (!connected) {
INFO("No WiFi connection!\n");
} }
} }
void setup() { void setup() {
delay(500); // Small delay to give the Serial console a bit of time to connect.
delay(1000);
Serial.begin(115200); Serial.begin(115200);
Serial.println("Starting..."); Serial.println("Starting...");
Serial.println("Started."); Serial.println("Started.");
@ -125,6 +113,12 @@ void setup() {
} else { } else {
INFO("Could not fetch current time via NTP.\n"); 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"); INFO("Initialization completed.\n");
} }