From 96a68cd711760086b5b3541d01bca2358ca2c9c3 Mon Sep 17 00:00:00 2001 From: GreenWizard Date: Sat, 13 Jan 2024 13:19:21 +0100 Subject: [PATCH] static IP, reconnect, etc. --- .../tea_poor/lib/Arduino/RemoteControl.cpp | 56 ++++++++++--------- .../tea_poor/lib/Arduino/RemoteControl.h | 19 ++++--- controller/tea_poor/src/main.cpp | 26 ++++++--- controller/tea_poor/src/secrets.h.example | 3 + 4 files changed, 63 insertions(+), 41 deletions(-) diff --git a/controller/tea_poor/lib/Arduino/RemoteControl.cpp b/controller/tea_poor/lib/Arduino/RemoteControl.cpp index 16c3ec9..5d9b1e7 100644 --- a/controller/tea_poor/lib/Arduino/RemoteControl.cpp +++ b/controller/tea_poor/lib/Arduino/RemoteControl.cpp @@ -38,16 +38,7 @@ void debugNetworkInfo() { Serial.println(); } -RemoteControl::RemoteControl(const char* SSID, const char* SSIDPassword) : - _SSID(SSID), _SSIDPassword(SSIDPassword), - _server(80), _app() -{ -} - -RemoteControl::~RemoteControl() { -} - -void RemoteControl::_setupNetwork() { +void verifyNetwork() { if (WiFi.status() == WL_NO_MODULE) { Serial.println("Communication with WiFi module failed!"); while(true) delay(500); @@ -59,15 +50,25 @@ void RemoteControl::_setupNetwork() { Serial.println(WIFI_FIRMWARE_LATEST_VERSION); Serial.println("Please upgrade your firmware."); } +} + +RemoteControl::RemoteControl(const NetworkConnectCallback &onConnect) : + _onConnect(onConnect) +{ +} +RemoteControl::~RemoteControl() { +} + +void RemoteControl::connectTo(const char* ssid, const char* password) { Serial.print("Connecting to "); - Serial.println(_SSID); + Serial.println(ssid); int attempts = 0; while (WL_CONNECTED != WiFi.status()) { // try to connect to the network attempts++; - Serial.println("Atempt to connect: " + String(attempts)); - WiFi.begin(_SSID.c_str(), _SSIDPassword.c_str()); + Serial.println("Attempt to connect: " + String(attempts)); + WiFi.begin(ssid, password); for (int i = 0; i < 50; i++) { // wait for connection Serial.print("."); delay(500); @@ -77,30 +78,33 @@ void RemoteControl::_setupNetwork() { Serial.println("Connection status: " + String(WiFi.status())); } Serial.println(); - + // successfully connected debugNetworkInfo(); } -void RemoteControl::setup(RemoteControlRoutesCallback routes) { - _setupNetwork(); - routes(_app); // setup routes +void RemoteControl::setup() { reconnect(); } + +void RemoteControl::reconnect() { + // reset everything + WiFi.disconnect(); + verifyNetwork(); + _app = Application(); // reset routes + _server = WiFiServer(80); // reset server + // reconnect + _onConnect(*this, _app); _server.begin(); } void RemoteControl::process() { - // TODO: check if we still have a connection. If not, reconnect. + if(WL_CONNECTED != WiFi.status()) { + reconnect(); + return; // wait for next tick, just to be sure that all is ok + } + /////////////////////////// WiFiClient client = _server.available(); if (client.connected()) { _app.process(&client); client.stop(); } -} - -String RemoteControl::asJSONString() const { - String result = "{"; - result += "\"SSID\": \"" + _SSID + "\","; - result += "\"signal strength\": " + String(WiFi.RSSI()); - result += "}"; - return result; } \ No newline at end of file diff --git a/controller/tea_poor/lib/Arduino/RemoteControl.h b/controller/tea_poor/lib/Arduino/RemoteControl.h index 1447248..9cc6c95 100644 --- a/controller/tea_poor/lib/Arduino/RemoteControl.h +++ b/controller/tea_poor/lib/Arduino/RemoteControl.h @@ -4,20 +4,25 @@ #include #include #include +#include -// define routes callback function signature -typedef void (*RemoteControlRoutesCallback)(Application &app); +// forward declaration +class RemoteControl; + +// define callback for (re)connecting to WiFi, use std::function +typedef std::function NetworkConnectCallback; class RemoteControl { public: - RemoteControl(const char* SSID, const char* SSIDPassword); + RemoteControl(const NetworkConnectCallback &onConnect); ~RemoteControl(); - void setup(RemoteControlRoutesCallback routes); + void setup(); void process(); - String asJSONString() const; + void reconnect(); + /////////////////// + void connectTo(const char* ssid, const char* password); private: - const String _SSID; - const String _SSIDPassword; + NetworkConnectCallback _onConnect; WiFiServer _server; Application _app; diff --git a/controller/tea_poor/src/main.cpp b/controller/tea_poor/src/main.cpp index 7509e0b..ad142fe 100644 --- a/controller/tea_poor/src/main.cpp +++ b/controller/tea_poor/src/main.cpp @@ -18,9 +18,6 @@ auto waterPump = std::make_shared( ) ); -// setting up remote control -RemoteControl remoteControl(WIFI_SSID, WIFI_PASSWORD); - // build command processor CommandProcessor commandProcessor( WATER_PUMP_SAFE_THRESHOLD, @@ -35,10 +32,17 @@ void withExtraHeaders(Response &res) { res.set("Content-Type", "application/json"); } -void setup() { - Serial.begin(9600); - waterPump->setup(); - remoteControl.setup([](Application &app) { +RemoteControl remoteControl( + // lambda function to setup network + [](RemoteControl &remoteControl, Application &app) { + // connect to WiFi + // set static IP address, if defined in configs + #ifdef WIFI_IP_ADDRESS + WiFi.config(WIFI_IP_ADDRESS); + #endif + + remoteControl.connectTo(WIFI_SSID, WIFI_PASSWORD); + // setup routes app.get("/pour_tea", [](Request &req, Response &res) { char milliseconds[64]; req.query("milliseconds", milliseconds, 64); @@ -59,7 +63,13 @@ void setup() { withExtraHeaders(res); res.print(response.c_str()); }); - }); + } +); + +void setup() { + Serial.begin(9600); + waterPump->setup(); + remoteControl.setup(); } void loop() { diff --git a/controller/tea_poor/src/secrets.h.example b/controller/tea_poor/src/secrets.h.example index 4fe08d5..c5b9e57 100644 --- a/controller/tea_poor/src/secrets.h.example +++ b/controller/tea_poor/src/secrets.h.example @@ -15,4 +15,7 @@ const int WATER_PUMP_POWER_PIN = 3; // Their is no reason to make it configurable and add unnecessary complexity const int WATER_PUMP_SAFE_THRESHOLD = 10 * 1000; +// Static IP address. If not defined, dynamic IP address will be used +// #define WIFI_IP_ADDRESS IPAddress(192, 168, 1, 123) + #endif // SECRETS_H \ No newline at end of file