move wifi&ota inside a library
This commit is contained in:
parent
15f5ab3ef7
commit
5014fd7f98
@ -1,4 +0,0 @@
|
||||
struct WifiCredentials {
|
||||
const char* ssid;
|
||||
const char* password;
|
||||
} credentials[] = {"foo", "bar"};
|
||||
@ -1,35 +0,0 @@
|
||||
#include <ArduinoOTA.h>
|
||||
|
||||
namespace Ota {
|
||||
|
||||
void loop();
|
||||
Task tLoop(TASK_IMMEDIATE, TASK_FOREVER, loop, &ts, true);
|
||||
|
||||
void setup() {
|
||||
ArduinoOTA.onStart([]() {
|
||||
Serial.println("Starting OTA");
|
||||
Mqtt::publishCleanupConfig();
|
||||
delay(2000);
|
||||
Mqtt::disconnect();
|
||||
});
|
||||
ArduinoOTA.onEnd([]() {
|
||||
Serial.println("\nOTA Finished");
|
||||
});
|
||||
ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
|
||||
Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
|
||||
});
|
||||
ArduinoOTA.onError([](ota_error_t error) {
|
||||
Serial.printf("Error[%u]: ", error);
|
||||
if (error == OTA_AUTH_ERROR) Serial.println("Auth Failed");
|
||||
else if (error == OTA_BEGIN_ERROR) Serial.println("Begin Failed");
|
||||
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() {
|
||||
ArduinoOTA.handle();
|
||||
}
|
||||
}
|
||||
@ -1,80 +0,0 @@
|
||||
#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, []{
|
||||
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, []{
|
||||
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) {
|
||||
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);
|
||||
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() {
|
||||
Serial.println("Disconnecting WiFi");
|
||||
WiFi.disconnect();
|
||||
WiFi.forceSleepBegin();
|
||||
}
|
||||
|
||||
void printStatus() {
|
||||
// print the SSID of the network you're attached to:
|
||||
Serial.print("SSID: ");
|
||||
Serial.println(WiFi.SSID());
|
||||
|
||||
// print your WiFi shield's IP address:
|
||||
IPAddress ip = WiFi.localIP();
|
||||
Serial.print("IP Address: ");
|
||||
Serial.println(ip);
|
||||
|
||||
// print the received signal strength:
|
||||
long rssi = WiFi.RSSI();
|
||||
Serial.print("signal strength (RSSI):");
|
||||
Serial.print(rssi);
|
||||
Serial.println(" dBm");
|
||||
}
|
||||
}
|
||||
@ -20,7 +20,8 @@ lib_deps =
|
||||
jchristensen/Timezone@^1.2.4
|
||||
adafruit/Adafruit Unified Sensor @ ^1.1.4
|
||||
adafruit/Adafruit BME280 Library@^2.2.4
|
||||
https://git.hodos.ro/arduino/ha-mqtt.git@^1.0.0
|
||||
https://git.hodos.ro/libraries/wifi.git@^1.0.0
|
||||
https://git.hodos.ro/libraries/ha-mqtt.git@^1.0.0
|
||||
build_flags = -D WIFI_ALWAYS_ON=1
|
||||
|
||||
[env:laptop_home]
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user