diff --git a/gateway/include/huzzah.h b/gateway/include/huzzah.h index 5f9c3d9..8413e73 100644 --- a/gateway/include/huzzah.h +++ b/gateway/include/huzzah.h @@ -37,8 +37,16 @@ namespace Board { [] {turnLed(BLUE_LED, false);}, [] {turnLed(BLUE_LED);} ); - Wifi::setup(); - Ota::setup(); + Wifi::setup(ts, []{ + Ota::tLoop.enable(); + Mqtt::tReConnect.enable(); + }); + Ota::setup( + [] { + Mqtt::publishCleanupConfig(); + delay(2000); + Mqtt::disconnect(); + }); tReadCommand.enable(); } diff --git a/gateway/include/ota.h b/gateway/include/ota.h deleted file mode 100644 index a418da7..0000000 --- a/gateway/include/ota.h +++ /dev/null @@ -1,34 +0,0 @@ -#include - -namespace Ota { - - void loop() { - ArduinoOTA.handle(); - } - - 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(); - } -} diff --git a/gateway/include/wifi.h b/gateway/include/wifi.h deleted file mode 100644 index d09a1cf..0000000 --- a/gateway/include/wifi.h +++ /dev/null @@ -1,68 +0,0 @@ -#include -#include -#include -#include "credentials.h" - -namespace Wifi { - - WiFiEventHandler stationConnectedHandler; - WiFiEventHandler stationDisconnectedHandler; - ESP8266WiFiMulti wifiMulti; - - String currentSSID; - String currentPsk; - - void printStatus(); - - Task tReconnect(1 * TASK_MINUTE, TASK_FOREVER, [](){ - if (WiFi.status() != WL_CONNECTED) { - WiFi.forceSleepWake(); - WiFi.begin(currentSSID.c_str(), currentPsk.c_str()); - Serial.println("Reconnecting to WiFi netowrk..."); - } - }, &ts); - - void setup() { - stationConnectedHandler = WiFi.onStationModeGotIP([](const WiFiEventStationModeGotIP& e) { - Serial.println("Connected to network."); - printStatus(); - tReconnect.cancel(); - Ota::tLoop.enable(); - Mqtt::tReConnect.enable(); - }); - - stationDisconnectedHandler = WiFi.onStationModeDisconnected([](const WiFiEventStationModeDisconnected& e) { - Serial.println("Disconnected from network."); - }); - - - for (uint32_t i = 0; i < sizeof(credentials) / sizeof(WifiCredentials); i++) { - wifiMulti.addAP(credentials[i].ssid, credentials[i].password); - } - - WiFi.setHostname("rc-gateway"); - Serial.println("Connecting to WiFi netowrk."); - while (wifiMulti.run() != WL_CONNECTED) { - delay(500); - } - currentSSID = WiFi.SSID(); - currentPsk = WiFi.psk(); - } - - 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"); - } -} diff --git a/gateway/platformio.ini b/gateway/platformio.ini index de2bedf..7c3370d 100644 --- a/gateway/platformio.ini +++ b/gateway/platformio.ini @@ -19,7 +19,7 @@ lib_deps = bblanchon/ArduinoJson@6.21.5 adafruit/Adafruit Unified Sensor@^1.1.4 adafruit/DHT sensor library@1.3.2 - https://git.hodos.ro/arduino/lib_serial-reader.git@^1.0.0 + https://git.hodos.ro/libraries/serial-reader.git@^1.0.0 build_flags = -D DHT_SENSOR=0 -D DEBUG_RAW=0 check_tool = cppcheck check_flags = --enable=all @@ -32,7 +32,8 @@ board = huzzah framework = arduino lib_deps = ${env.lib_deps} - https://git.hodos.ro/arduino/ha-mqtt.git@^1.0.0 + https://git.hodos.ro/libraries/ha-mqtt.git@^1.0.0 + https://git.hodos.ro/libraries/wifi.git@^1.0.1 upload_port = 192.168.6.161 upload_protocol = espota upload_flags =