From 61a7ca0bde4beae582c2c7e9d2064895d7dff767 Mon Sep 17 00:00:00 2001 From: Nicu Hodos Date: Sat, 27 Apr 2024 00:00:11 +0200 Subject: [PATCH] separate pro-mini and huzzah logic into dedicated files --- gateway/include/huzzah.h | 29 ++++++++++++++++++ gateway/include/ota.h | 9 +++--- gateway/include/pro-mini.h | 37 +++++++++++++++++++++++ gateway/include/wifi.h | 41 ++++++++++++++------------ gateway/platformio.ini | 8 +++-- gateway/src/gateway.cpp | 60 +++++++------------------------------- 6 files changed, 110 insertions(+), 74 deletions(-) create mode 100644 gateway/include/huzzah.h create mode 100644 gateway/include/pro-mini.h diff --git a/gateway/include/huzzah.h b/gateway/include/huzzah.h new file mode 100644 index 0000000..431fcba --- /dev/null +++ b/gateway/include/huzzah.h @@ -0,0 +1,29 @@ +#include + +#define SEND_PIN PIN_SPI_MOSI +#define RECEIVE_PIN PIN_SPI_MISO +#define RED_LED LED_BUILTIN +// #define BLUE_LED 2 + +Scheduler ts; + +#include "wifi.h" + +namespace Board { + void turnOffLed(uint8_t led) { + digitalWrite(led, HIGH); + } + + void setup() { + pinMode(RED_LED, OUTPUT); + turnOffLed(RED_LED); + // pinMode(BLUE_LED, OUTPUT); + // turnOffLed(BLUE_LED); + Wifi::setup(); + Ota::setup(); + } + + void loop() { + ts.execute(); + } +} diff --git a/gateway/include/ota.h b/gateway/include/ota.h index b834a8a..6015b5c 100644 --- a/gateway/include/ota.h +++ b/gateway/include/ota.h @@ -2,7 +2,10 @@ namespace Ota { - void loop(); + void loop() { + ArduinoOTA.handle(); + } + Task tLoop(TASK_IMMEDIATE, TASK_FOREVER, loop, &ts, true); void setup() { @@ -25,8 +28,4 @@ namespace Ota { }); ArduinoOTA.begin(); } - - void loop() { - ArduinoOTA.handle(); - } } diff --git a/gateway/include/pro-mini.h b/gateway/include/pro-mini.h new file mode 100644 index 0000000..6da5bb9 --- /dev/null +++ b/gateway/include/pro-mini.h @@ -0,0 +1,37 @@ +#include + +#define RESET_PIN 10 +#define SEND_PIN 11 +#define RECEIVE_PIN 2 + +namespace Board { + SerialReader<200> serialReader; + + void setup() { + digitalWrite(RESET_PIN, HIGH); + pinMode(RESET_PIN, OUTPUT); + } + + void blink() { + digitalWrite(LED_BUILTIN, HIGH); + delay(200); + digitalWrite(LED_BUILTIN, LOW); + } + + void readCommand() { + if (serialReader.readLine(Serial) > 0) { + char* cmd = serialReader.getBuffer(); + if (strcmp("reset", cmd) == 0) { + Serial.println("resetting..."); + delay(1200); + digitalWrite(RESET_PIN, LOW); + Serial.println("resetting failed"); + } + runJsonCommand(cmd); + } + } + + void loop() { + readCommand(); + } +} diff --git a/gateway/include/wifi.h b/gateway/include/wifi.h index c00d88d..0b9219e 100644 --- a/gateway/include/wifi.h +++ b/gateway/include/wifi.h @@ -1,33 +1,46 @@ #include #include #include +#include "ota.h" #include "credentials.h" namespace Wifi { WiFiEventHandler stationConnectedHandler; WiFiEventHandler stationDisconnectedHandler; - void reconnect(); - - Task tWifiReconnect(1 * TASK_MINUTE, TASK_FOREVER, reconnect, &ts); + 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); + Task tConnected(TASK_IMMEDIATE, TASK_ONCE, [](){ + Serial.println("Wifi connected event"); + printStatus(); + Ota::tLoop.enable(); + }, &ts); + void setup() { stationConnectedHandler = WiFi.onStationModeGotIP([](const WiFiEventStationModeGotIP& e) { Serial.println("Reconnected to network."); - tWifiConnected.restart(); - tWifiReconnect.cancel(); - }); + tConnected.restart(); + tReconnect.cancel(); + }); stationDisconnectedHandler = WiFi.onStationModeDisconnected([](const WiFiEventStationModeDisconnected& e) { Serial.println("Disconnected from network."); - tWifiReconnect.restartDelayed(); - }); + tReconnect.restartDelayed(); + }); - ESP8266WiFiMulti wifiMulti; for (uint32_t i = 0; i < sizeof(credentials) / sizeof(WifiCredentials); i++) { wifiMulti.addAP(credentials[i].ssid, credentials[i].password); } @@ -36,19 +49,11 @@ namespace Wifi { while (wifiMulti.run() != WL_CONNECTED) { delay(500); } - WiFi.setHostname("esp-clock"); + WiFi.setHostname("rc-gateway"); 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(); diff --git a/gateway/platformio.ini b/gateway/platformio.ini index 4828ef1..3faf2e2 100644 --- a/gateway/platformio.ini +++ b/gateway/platformio.ini @@ -22,13 +22,17 @@ lib_deps = bblanchon/ArduinoJson@6.21.5 adafruit/Adafruit Unified Sensor@^1.1.4 arkhipenko/TaskScheduler@^3.7.0 + adafruit/DHT sensor library@1.3.2 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 +upload_port = 192.168.6.161 +upload_protocol = espota +upload_flags = + --host_port=10000 [env:pro-mini] platform = atmelavr @@ -40,7 +44,7 @@ lib_deps = sui77/rc-switch@^2.6.3 bblanchon/ArduinoJson@6.21.5 adafruit/Adafruit Unified Sensor@^1.1.4 - adafruit/DHT sensor library@1.3.10 + adafruit/DHT sensor library@1.3.2 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 diff --git a/gateway/src/gateway.cpp b/gateway/src/gateway.cpp index defd07a..dd08f5a 100644 --- a/gateway/src/gateway.cpp +++ b/gateway/src/gateway.cpp @@ -4,41 +4,27 @@ #include "Protocol_1.h" #include "Protocol_2.h" #include "output.h" -#include - -#define RESET_PIN 10 -#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; +void runJsonCommand(char* cmd); + +#if defined(ESP8266) +#include "huzzah.h" +#else +#include "pro-mini.h" +#endif + void setup() { - digitalWrite(RESET_PIN, HIGH); pinMode(LED_BUILTIN, OUTPUT); - pinMode(RESET_PIN, OUTPUT); + Serial.begin(9600); mySwitch.enableReceive(digitalPinToInterrupt(RECEIVE_PIN)); mySwitch.enableTransmit(SEND_PIN); mySwitch.setRepeatTransmit(10); Dht::setup(); -#if defined(ESP8266) - Wifi::setup(); - Ota::setup(); -#endif - - Serial.begin(9600); + Board::setup(); delay(1000); } @@ -109,32 +95,8 @@ void runJsonCommand(char* cmd) { } } -void readCommand() { - if (serialReader.readLine(Serial) > 0) { - char* cmd = serialReader.getBuffer(); - if (strcmp("reset", cmd) == 0) { - Serial.println("resetting..."); - delay(1200); - digitalWrite(RESET_PIN, LOW); - Serial.println("resetting failed"); - } - runJsonCommand(cmd); - } -} - void loop() { - readCommand(); + Board::loop(); 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