From 100a89a92f6049e7365bf8fc06f86959bd877fa9 Mon Sep 17 00:00:00 2001 From: Nicu Hodos Date: Fri, 26 Apr 2024 13:25:10 +0200 Subject: [PATCH] add huzzah with support for ota and wifi --- gateway/.gitignore | 1 + gateway/include/credentials.h.tpl | 4 ++ gateway/include/ota.h | 32 +++++++++++++ gateway/include/wifi.h | 74 +++++++++++++++++++++++++++++++ gateway/platformio.ini | 19 ++++++++ gateway/src/gateway.cpp | 24 ++++++++++ 6 files changed, 154 insertions(+) create mode 100644 gateway/include/credentials.h.tpl create mode 100644 gateway/include/ota.h create mode 100644 gateway/include/wifi.h diff --git a/gateway/.gitignore b/gateway/.gitignore index 89cc49c..3074d78 100644 --- a/gateway/.gitignore +++ b/gateway/.gitignore @@ -3,3 +3,4 @@ .vscode/c_cpp_properties.json .vscode/launch.json .vscode/ipch +include/credentials.h diff --git a/gateway/include/credentials.h.tpl b/gateway/include/credentials.h.tpl new file mode 100644 index 0000000..c5637e4 --- /dev/null +++ b/gateway/include/credentials.h.tpl @@ -0,0 +1,4 @@ +struct WifiCredentials { + const char* ssid; + const char* password; +} credentials[] = {"foo", "bar"}; diff --git a/gateway/include/ota.h b/gateway/include/ota.h new file mode 100644 index 0000000..b834a8a --- /dev/null +++ b/gateway/include/ota.h @@ -0,0 +1,32 @@ +#include + +namespace Ota { + + void loop(); + Task tLoop(TASK_IMMEDIATE, TASK_FOREVER, loop, &ts, true); + + void setup() { + ArduinoOTA.onStart([]() { + Serial.println("Start"); + }); + ArduinoOTA.onEnd([]() { + Serial.println("\nEnd"); + }); + 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(); + } +} diff --git a/gateway/include/wifi.h b/gateway/include/wifi.h new file mode 100644 index 0000000..c00d88d --- /dev/null +++ b/gateway/include/wifi.h @@ -0,0 +1,74 @@ +#include +#include +#include +#include "credentials.h" + +namespace Wifi { + + WiFiEventHandler stationConnectedHandler; + WiFiEventHandler stationDisconnectedHandler; + void reconnect(); + + Task tWifiReconnect(1 * TASK_MINUTE, TASK_FOREVER, reconnect, &ts); + + String currentSSID; + String currentPsk; + + void setup() { + stationConnectedHandler = WiFi.onStationModeGotIP([](const WiFiEventStationModeGotIP& e) { + Serial.println("Reconnected to network."); + tWifiConnected.restart(); + tWifiReconnect.cancel(); + }); + + stationDisconnectedHandler = WiFi.onStationModeDisconnected([](const WiFiEventStationModeDisconnected& e) { + Serial.println("Disconnected from network."); + tWifiReconnect.restartDelayed(); + }); + + + ESP8266WiFiMulti wifiMulti; + for (uint32_t i = 0; i < sizeof(credentials) / sizeof(WifiCredentials); i++) { + wifiMulti.addAP(credentials[i].ssid, credentials[i].password); + } + + Serial.println("Connecting to WiFi netowrk."); + while (wifiMulti.run() != WL_CONNECTED) { + delay(500); + } + WiFi.setHostname("esp-clock"); + currentSSID = WiFi.SSID(); + currentPsk = WiFi.psk(); + } + + void reconnect() { + if (WiFi.status() != WL_CONNECTED) { + WiFi.forceSleepWake(); + WiFi.begin(currentSSID.c_str(), currentPsk.c_str()); + Serial.println("Reconnecting to WiFi netowrk..."); + } + } + + 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"); + } +} diff --git a/gateway/platformio.ini b/gateway/platformio.ini index 12f2a27..4828ef1 100644 --- a/gateway/platformio.ini +++ b/gateway/platformio.ini @@ -11,6 +11,25 @@ [platformio] default_envs = pro-mini +[env:huzzah] +platform = espressif8266 +board = huzzah +framework = arduino +lib_extra_dirs = + ../libraries +lib_deps = + sui77/rc-switch@^2.6.4 + bblanchon/ArduinoJson@6.21.5 + adafruit/Adafruit Unified Sensor@^1.1.4 + arkhipenko/TaskScheduler@^3.7.0 + https://git.hodos.ro/arduino/lib_serial-reader.git@^1.0.0 +build_flags = -D DHT_SENSOR=0 -D DEBUG_RAW=0 +upload_port = /dev/ttyUSB0 +check_tool = cppcheck +check_flags = --enable=all +check_skip_packages = yes +check_severity = medium, high + [env:pro-mini] platform = atmelavr board = pro16MHzatmega328 diff --git a/gateway/src/gateway.cpp b/gateway/src/gateway.cpp index 4a8e019..defd07a 100644 --- a/gateway/src/gateway.cpp +++ b/gateway/src/gateway.cpp @@ -10,6 +10,15 @@ #define SEND_PIN 11 #define RECEIVE_PIN 2 +#if defined(ESP8266) +void onWifiConnected(); +#include +Scheduler ts; +Task tWifiConnected(TASK_IMMEDIATE, TASK_ONCE, onWifiConnected, &ts); + +#include "wifi.h" +#include "ota.h" +#endif RCSwitch mySwitch; SerialReader<200> serialReader; @@ -24,6 +33,10 @@ void setup() { mySwitch.setRepeatTransmit(10); Dht::setup(); +#if defined(ESP8266) + Wifi::setup(); + Ota::setup(); +#endif Serial.begin(9600); @@ -113,4 +126,15 @@ void loop() { readCommand(); readRcSwitch(); Dht::read(); +#if defined(ESP8266) + ts.execute(); +#endif } + +#if defined(ESP8266) +void onWifiConnected() { + Serial.println("Wifi connected event"); + Wifi::printStatus(); + Ota::tLoop.enable(); +} +#endif \ No newline at end of file