simplify wifi and make it more generic
This commit is contained in:
parent
c9e1192804
commit
2465dbfea7
38
src/wifi.h
38
src/wifi.h
@ -7,23 +7,33 @@ namespace Wifi {
|
|||||||
|
|
||||||
WiFiEventHandler stationConnectedHandler;
|
WiFiEventHandler stationConnectedHandler;
|
||||||
WiFiEventHandler stationDisconnectedHandler;
|
WiFiEventHandler stationDisconnectedHandler;
|
||||||
void reconnect();
|
|
||||||
|
|
||||||
Task tWifiReconnect(1 * TASK_MINUTE, TASK_FOREVER, reconnect, &ts);
|
|
||||||
|
|
||||||
String currentSSID;
|
String currentSSID;
|
||||||
String currentPsk;
|
String currentPsk;
|
||||||
|
|
||||||
void setup() {
|
void printStatus();
|
||||||
stationConnectedHandler = WiFi.onStationModeGotIP([](const WiFiEventStationModeGotIP& e) {
|
|
||||||
Serial.println("Reconnected to network.");
|
Task tReconnect(1 * TASK_MINUTE, TASK_FOREVER, []{
|
||||||
tWifiConnected.restart();
|
if (WiFi.status() != WL_CONNECTED) {
|
||||||
tWifiReconnect.cancel();
|
Serial.println("Reconnecting to WiFi netowrk...");
|
||||||
|
WiFi.forceSleepWake();
|
||||||
|
WiFi.begin(currentSSID.c_str(), currentPsk.c_str());
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
void setup(Scheduler& ts, void(*onConnected)() = nullptr) {
|
||||||
|
ts.addTask(tReconnect);
|
||||||
|
|
||||||
|
stationConnectedHandler = WiFi.onStationModeGotIP([onConnected](const WiFiEventStationModeGotIP& e) {
|
||||||
|
Serial.println("Connected to network.");
|
||||||
|
printStatus();
|
||||||
|
tReconnect.cancel();
|
||||||
|
if (onConnected) onConnected();
|
||||||
});
|
});
|
||||||
|
|
||||||
stationDisconnectedHandler = WiFi.onStationModeDisconnected([](const WiFiEventStationModeDisconnected& e) {
|
stationDisconnectedHandler = WiFi.onStationModeDisconnected([](const WiFiEventStationModeDisconnected& e) {
|
||||||
Serial.println("Disconnected from network.");
|
Serial.println("Disconnected from network.");
|
||||||
tWifiReconnect.restartDelayed();
|
tReconnect.restartDelayed();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
@ -36,17 +46,11 @@ namespace Wifi {
|
|||||||
while (wifiMulti.run() != WL_CONNECTED) {
|
while (wifiMulti.run() != WL_CONNECTED) {
|
||||||
delay(500);
|
delay(500);
|
||||||
}
|
}
|
||||||
WiFi.setHostname("esp-clock");
|
WiFi.setHostname(MAIN_DEVICE_ID);
|
||||||
currentSSID = WiFi.SSID();
|
currentSSID = WiFi.SSID();
|
||||||
currentPsk = WiFi.psk();
|
currentPsk = WiFi.psk();
|
||||||
}
|
|
||||||
|
|
||||||
void reconnect() {
|
tReconnect.enable();
|
||||||
if (WiFi.status() != WL_CONNECTED) {
|
|
||||||
WiFi.forceSleepWake();
|
|
||||||
WiFi.begin(currentSSID.c_str(), currentPsk.c_str());
|
|
||||||
Serial.println("Reconnecting to WiFi netowrk...");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void disconnect() {
|
void disconnect() {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user