Merge branch 'v2.0.0'
This commit is contained in:
commit
d53184985d
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "Wifi",
|
||||
"version": "1.0.2",
|
||||
"version": "2.0.0",
|
||||
"description": "Helper classes for handling wifi connectiviy and OTA",
|
||||
"repository":
|
||||
{
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
struct WifiCredentials {
|
||||
const char* ssid;
|
||||
const char* password;
|
||||
} credentials[] = {"foo", "bar"};
|
||||
} credentials = {"foo", "bar"};
|
||||
|
||||
37
src/wifi.h
37
src/wifi.h
@ -1,65 +1,42 @@
|
||||
#include <ESP8266WiFi.h>
|
||||
#include <ESP8266WiFiMulti.h>
|
||||
#include <ESP8266mDNS.h>
|
||||
#include "credentials.h"
|
||||
|
||||
namespace Wifi {
|
||||
|
||||
ESP8266WiFiMulti wifiMulti;
|
||||
WiFiEventHandler stationConnectedHandler;
|
||||
WiFiEventHandler stationDisconnectedHandler;
|
||||
|
||||
String currentSSID;
|
||||
String currentPsk;
|
||||
|
||||
void printStatus();
|
||||
|
||||
Task tReconnect(1 * TASK_MINUTE, TASK_FOREVER,
|
||||
Task tConnect(1 * TASK_MINUTE, TASK_FOREVER,
|
||||
[] {
|
||||
if (WiFi.status() != WL_CONNECTED) {
|
||||
Serial.println("Reconnecting to WiFi netowrk...");
|
||||
Serial.println("Connecting to WiFi netowrk...");
|
||||
WiFi.forceSleepWake();
|
||||
WiFi.begin(currentSSID.c_str(), currentPsk.c_str());
|
||||
WiFi.begin(credentials.ssid, credentials.password);
|
||||
}
|
||||
});
|
||||
|
||||
Task tInitialConnect(500 * TASK_MILLISECOND, 120,
|
||||
[] {
|
||||
Serial.println("Finding WiFi netowrk...");
|
||||
if (wifiMulti.run() == WL_CONNECTED) tInitialConnect.disable();
|
||||
}, nullptr, false, nullptr,
|
||||
[] {
|
||||
currentSSID = WiFi.SSID();
|
||||
currentPsk = WiFi.psk();
|
||||
tReconnect.enable();
|
||||
}
|
||||
);
|
||||
},
|
||||
&ts, true);
|
||||
|
||||
void setup(Scheduler& ts, void(*onConnected)() = nullptr, void(*onDisconnected)() = nullptr) {
|
||||
stationConnectedHandler = WiFi.onStationModeGotIP(
|
||||
[onConnected](const WiFiEventStationModeGotIP& e) {
|
||||
Serial.println("Connected to network.");
|
||||
printStatus();
|
||||
tReconnect.cancel();
|
||||
tConnect.cancel();
|
||||
if (onConnected) onConnected();
|
||||
});
|
||||
|
||||
stationDisconnectedHandler = WiFi.onStationModeDisconnected(
|
||||
[onDisconnected](const WiFiEventStationModeDisconnected& e) {
|
||||
Serial.println("Disconnected from network.");
|
||||
tReconnect.enableIfNot();
|
||||
tConnect.enableIfNot();
|
||||
if (onDisconnected) onDisconnected();
|
||||
});
|
||||
|
||||
|
||||
WiFi.setHostname(MAIN_DEVICE_ID);
|
||||
for (uint32_t i = 0; i < sizeof(credentials) / sizeof(WifiCredentials); i++) {
|
||||
wifiMulti.addAP(credentials[i].ssid, credentials[i].password);
|
||||
}
|
||||
|
||||
ts.addTask(tInitialConnect);
|
||||
ts.addTask(tReconnect);
|
||||
tInitialConnect.enable();
|
||||
}
|
||||
|
||||
void disconnect() {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user