Main: Fix WiFi connection stuff for multiple WiFis.
This commit is contained in:
parent
d3c699aefa
commit
547080acf5
50
src/main.cpp
50
src/main.cpp
@ -2,6 +2,7 @@
|
||||
#include <SPI.h>
|
||||
#include <SD.h>
|
||||
#include <WiFi.h>
|
||||
#include <WiFiMulti.h>
|
||||
#include <ESPmDNS.h>
|
||||
#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 {
|
||||
@ -56,21 +45,20 @@ void wifi_connect() {
|
||||
}
|
||||
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.");
|
||||
@ -126,6 +114,12 @@ void setup() {
|
||||
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");
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user