create the library
This commit is contained in:
parent
f8aa7dfabd
commit
79d16c0098
21
library.json
Normal file
21
library.json
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
{
|
||||||
|
"name": "Wifi",
|
||||||
|
"version": "1.0.0",
|
||||||
|
"description": "Helper classes for handling wifi connectiviy and OTA",
|
||||||
|
"repository":
|
||||||
|
{
|
||||||
|
"type": "git",
|
||||||
|
"url": "https://git.hodos.ro/libraries/wifi.git"
|
||||||
|
},
|
||||||
|
"authors":
|
||||||
|
[
|
||||||
|
{
|
||||||
|
"name": "Nicu Hodos",
|
||||||
|
"email": "nicu@hodos.ro",
|
||||||
|
"maintainer": true
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"license": "MIT",
|
||||||
|
"frameworks": "arduino",
|
||||||
|
"platforms": "*"
|
||||||
|
}
|
||||||
60
src/ota.h
60
src/ota.h
@ -2,34 +2,38 @@
|
|||||||
|
|
||||||
namespace Ota {
|
namespace Ota {
|
||||||
|
|
||||||
void loop();
|
void loop();
|
||||||
Task tLoop(TASK_IMMEDIATE, TASK_FOREVER, loop, &ts, true);
|
Task tLoop(TASK_IMMEDIATE, TASK_FOREVER, loop, &ts, true);
|
||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
ArduinoOTA.onStart([]() {
|
ArduinoOTA.onStart(
|
||||||
Serial.println("Starting OTA");
|
[]() {
|
||||||
Mqtt::publishCleanupConfig();
|
Serial.println("Starting OTA");
|
||||||
delay(2000);
|
Mqtt::publishCleanupConfig();
|
||||||
Mqtt::disconnect();
|
delay(2000);
|
||||||
});
|
Mqtt::disconnect();
|
||||||
ArduinoOTA.onEnd([]() {
|
});
|
||||||
Serial.println("\nOTA Finished");
|
ArduinoOTA.onEnd(
|
||||||
});
|
[]() {
|
||||||
ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
|
Serial.println("\nOTA Finished");
|
||||||
Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
|
});
|
||||||
});
|
ArduinoOTA.onProgress(
|
||||||
ArduinoOTA.onError([](ota_error_t error) {
|
[](unsigned int progress, unsigned int total) {
|
||||||
Serial.printf("Error[%u]: ", error);
|
Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
|
||||||
if (error == OTA_AUTH_ERROR) Serial.println("Auth Failed");
|
});
|
||||||
else if (error == OTA_BEGIN_ERROR) Serial.println("Begin Failed");
|
ArduinoOTA.onError(
|
||||||
else if (error == OTA_CONNECT_ERROR) Serial.println("Connect Failed");
|
[](ota_error_t error) {
|
||||||
else if (error == OTA_RECEIVE_ERROR) Serial.println("Receive Failed");
|
Serial.printf("Error[%u]: ", error);
|
||||||
else if (error == OTA_END_ERROR) Serial.println("End Failed");
|
if (error == OTA_AUTH_ERROR) Serial.println("Auth Failed");
|
||||||
});
|
else if (error == OTA_BEGIN_ERROR) Serial.println("Begin Failed");
|
||||||
ArduinoOTA.begin();
|
else if (error == OTA_CONNECT_ERROR) Serial.println("Connect Failed");
|
||||||
}
|
else if (error == OTA_RECEIVE_ERROR) Serial.println("Receive Failed");
|
||||||
|
else if (error == OTA_END_ERROR) Serial.println("End Failed");
|
||||||
|
});
|
||||||
|
ArduinoOTA.begin();
|
||||||
|
}
|
||||||
|
|
||||||
void loop() {
|
void loop() {
|
||||||
ArduinoOTA.handle();
|
ArduinoOTA.handle();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
126
src/wifi.h
126
src/wifi.h
@ -5,76 +5,82 @@
|
|||||||
|
|
||||||
namespace Wifi {
|
namespace Wifi {
|
||||||
|
|
||||||
ESP8266WiFiMulti wifiMulti;
|
ESP8266WiFiMulti wifiMulti;
|
||||||
WiFiEventHandler stationConnectedHandler;
|
WiFiEventHandler stationConnectedHandler;
|
||||||
WiFiEventHandler stationDisconnectedHandler;
|
WiFiEventHandler stationDisconnectedHandler;
|
||||||
|
|
||||||
String currentSSID;
|
|
||||||
String currentPsk;
|
|
||||||
|
|
||||||
void printStatus();
|
String currentSSID;
|
||||||
|
String currentPsk;
|
||||||
|
|
||||||
Task tReconnect(1 * TASK_MINUTE, TASK_FOREVER, []{
|
void printStatus();
|
||||||
if (WiFi.status() != WL_CONNECTED) {
|
|
||||||
Serial.println("Reconnecting to WiFi netowrk...");
|
|
||||||
WiFi.forceSleepWake();
|
|
||||||
WiFi.begin(currentSSID.c_str(), currentPsk.c_str());
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
Task tInitialConnect(500 * TASK_MILLISECOND, 120, []{
|
Task tReconnect(1 * TASK_MINUTE, TASK_FOREVER,
|
||||||
Serial.println("Finding WiFi netowrk...");
|
[] {
|
||||||
if (wifiMulti.run() == WL_CONNECTED) tInitialConnect.disable();
|
if (WiFi.status() != WL_CONNECTED) {
|
||||||
}, nullptr, false, nullptr, []{
|
Serial.println("Reconnecting to WiFi netowrk...");
|
||||||
currentSSID = WiFi.SSID();
|
WiFi.forceSleepWake();
|
||||||
currentPsk = WiFi.psk();
|
WiFi.begin(currentSSID.c_str(), currentPsk.c_str());
|
||||||
tReconnect.enable();
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
void setup(Scheduler& ts, void(*onConnected)() = nullptr) {
|
Task tInitialConnect(500 * TASK_MILLISECOND, 120,
|
||||||
stationConnectedHandler = WiFi.onStationModeGotIP([onConnected](const WiFiEventStationModeGotIP& e) {
|
[] {
|
||||||
Serial.println("Connected to network.");
|
Serial.println("Finding WiFi netowrk...");
|
||||||
printStatus();
|
if (wifiMulti.run() == WL_CONNECTED) tInitialConnect.disable();
|
||||||
tReconnect.cancel();
|
}, nullptr, false, nullptr,
|
||||||
if (onConnected) onConnected();
|
[] {
|
||||||
});
|
currentSSID = WiFi.SSID();
|
||||||
|
currentPsk = WiFi.psk();
|
||||||
|
tReconnect.enable();
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
stationDisconnectedHandler = WiFi.onStationModeDisconnected([](const WiFiEventStationModeDisconnected& e) {
|
void setup(Scheduler& ts, void(*onConnected)() = nullptr) {
|
||||||
Serial.println("Disconnected from network.");
|
stationConnectedHandler = WiFi.onStationModeGotIP(
|
||||||
tReconnect.restartDelayed();
|
[onConnected](const WiFiEventStationModeGotIP& e) {
|
||||||
});
|
Serial.println("Connected to network.");
|
||||||
|
printStatus();
|
||||||
|
tReconnect.cancel();
|
||||||
|
if (onConnected) onConnected();
|
||||||
|
});
|
||||||
|
|
||||||
|
stationDisconnectedHandler = WiFi.onStationModeDisconnected(
|
||||||
|
[](const WiFiEventStationModeDisconnected& e) {
|
||||||
|
Serial.println("Disconnected from network.");
|
||||||
|
tReconnect.restartDelayed();
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
WiFi.setHostname(MAIN_DEVICE_ID);
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
ts.addTask(tInitialConnect);
|
||||||
|
ts.addTask(tReconnect);
|
||||||
|
tInitialConnect.enable();
|
||||||
}
|
}
|
||||||
|
|
||||||
ts.addTask(tInitialConnect);
|
void disconnect() {
|
||||||
ts.addTask(tReconnect);
|
Serial.println("Disconnecting WiFi");
|
||||||
tInitialConnect.enable();
|
WiFi.disconnect();
|
||||||
}
|
WiFi.forceSleepBegin();
|
||||||
|
}
|
||||||
|
|
||||||
void disconnect() {
|
void printStatus() {
|
||||||
Serial.println("Disconnecting WiFi");
|
// print the SSID of the network you're attached to:
|
||||||
WiFi.disconnect();
|
Serial.print("SSID: ");
|
||||||
WiFi.forceSleepBegin();
|
Serial.println(WiFi.SSID());
|
||||||
}
|
|
||||||
|
|
||||||
void printStatus() {
|
// print your WiFi shield's IP address:
|
||||||
// print the SSID of the network you're attached to:
|
IPAddress ip = WiFi.localIP();
|
||||||
Serial.print("SSID: ");
|
Serial.print("IP Address: ");
|
||||||
Serial.println(WiFi.SSID());
|
Serial.println(ip);
|
||||||
|
|
||||||
// print your WiFi shield's IP address:
|
// print the received signal strength:
|
||||||
IPAddress ip = WiFi.localIP();
|
long rssi = WiFi.RSSI();
|
||||||
Serial.print("IP Address: ");
|
Serial.print("signal strength (RSSI):");
|
||||||
Serial.println(ip);
|
Serial.print(rssi);
|
||||||
|
Serial.println(" dBm");
|
||||||
// print the received signal strength:
|
}
|
||||||
long rssi = WiFi.RSSI();
|
|
||||||
Serial.print("signal strength (RSSI):");
|
|
||||||
Serial.print(rssi);
|
|
||||||
Serial.println(" dBm");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user