use task for WifiMulti: everything should be exectued inside loop
This commit is contained in:
parent
2465dbfea7
commit
f8aa7dfabd
26
src/wifi.h
26
src/wifi.h
@ -5,6 +5,7 @@
|
|||||||
|
|
||||||
namespace Wifi {
|
namespace Wifi {
|
||||||
|
|
||||||
|
ESP8266WiFiMulti wifiMulti;
|
||||||
WiFiEventHandler stationConnectedHandler;
|
WiFiEventHandler stationConnectedHandler;
|
||||||
WiFiEventHandler stationDisconnectedHandler;
|
WiFiEventHandler stationDisconnectedHandler;
|
||||||
|
|
||||||
@ -21,9 +22,16 @@ namespace Wifi {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
void setup(Scheduler& ts, void(*onConnected)() = nullptr) {
|
Task tInitialConnect(500 * TASK_MILLISECOND, 120, []{
|
||||||
ts.addTask(tReconnect);
|
Serial.println("Finding WiFi netowrk...");
|
||||||
|
if (wifiMulti.run() == WL_CONNECTED) tInitialConnect.disable();
|
||||||
|
}, nullptr, false, nullptr, []{
|
||||||
|
currentSSID = WiFi.SSID();
|
||||||
|
currentPsk = WiFi.psk();
|
||||||
|
tReconnect.enable();
|
||||||
|
});
|
||||||
|
|
||||||
|
void setup(Scheduler& ts, void(*onConnected)() = nullptr) {
|
||||||
stationConnectedHandler = WiFi.onStationModeGotIP([onConnected](const WiFiEventStationModeGotIP& e) {
|
stationConnectedHandler = WiFi.onStationModeGotIP([onConnected](const WiFiEventStationModeGotIP& e) {
|
||||||
Serial.println("Connected to network.");
|
Serial.println("Connected to network.");
|
||||||
printStatus();
|
printStatus();
|
||||||
@ -37,20 +45,14 @@ namespace Wifi {
|
|||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
ESP8266WiFiMulti wifiMulti;
|
WiFi.setHostname(MAIN_DEVICE_ID);
|
||||||
for (uint32_t i = 0; i < sizeof(credentials) / sizeof(WifiCredentials); i++) {
|
for (uint32_t i = 0; i < sizeof(credentials) / sizeof(WifiCredentials); i++) {
|
||||||
wifiMulti.addAP(credentials[i].ssid, credentials[i].password);
|
wifiMulti.addAP(credentials[i].ssid, credentials[i].password);
|
||||||
}
|
}
|
||||||
|
|
||||||
Serial.println("Connecting to WiFi netowrk.");
|
ts.addTask(tInitialConnect);
|
||||||
while (wifiMulti.run() != WL_CONNECTED) {
|
ts.addTask(tReconnect);
|
||||||
delay(500);
|
tInitialConnect.enable();
|
||||||
}
|
|
||||||
WiFi.setHostname(MAIN_DEVICE_ID);
|
|
||||||
currentSSID = WiFi.SSID();
|
|
||||||
currentPsk = WiFi.psk();
|
|
||||||
|
|
||||||
tReconnect.enable();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void disconnect() {
|
void disconnect() {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user