diff --git a/controller/tea_poor/lib/RemoteControl/RemoteControl.cpp b/controller/tea_poor/lib/RemoteControl/RemoteControl.cpp new file mode 100644 index 0000000..23693ae --- /dev/null +++ b/controller/tea_poor/lib/RemoteControl/RemoteControl.cpp @@ -0,0 +1,99 @@ +#include "RemoteControl.h" + +void printMacAddress(byte mac[]) { + for (int i = 0; i < 6; i++) { + if (i > 0) { + Serial.print(":"); + } + if (mac[i] < 16) { + Serial.print("0"); + } + Serial.print(mac[i], HEX); + } + Serial.println(); +} + +void debugNetworkInfo() { + Serial.print("Connected. IP: "); + Serial.println(WiFi.localIP()); + + Serial.print("SSID: "); + Serial.println(WiFi.SSID()); + + // print the MAC address of the router you're attached to: + byte bssid[6]; + WiFi.BSSID(bssid); + Serial.print("BSSID: "); + printMacAddress(bssid); + + // print the received signal strength: + Serial.print("signal strength (RSSI): "); + Serial.println(WiFi.RSSI()); + + // print the encryption type: + Serial.print("Encryption Type: "); + Serial.println(WiFi.encryptionType(), HEX); + + Serial.println("----------------------------------------------"); + Serial.println(); +} + +RemoteControl::RemoteControl(const char* SSID, const char* SSIDPassword) : + _SSID(SSID), _SSIDPassword(SSIDPassword), + _server(80), _app() +{ +} + +RemoteControl::~RemoteControl() { +} + +void RemoteControl::_setupNetwork() { + if (WiFi.status() == WL_NO_MODULE) { + Serial.println("Communication with WiFi module failed!"); + while(true) delay(500); + } + + String firmware_version = WiFi.firmwareVersion(); + if ( firmware_version < WIFI_FIRMWARE_LATEST_VERSION) { + Serial.print("Latest available version: "); + Serial.println(WIFI_FIRMWARE_LATEST_VERSION); + Serial.println("Please upgrade your firmware."); + while(true) delay(500); + } + + Serial.print("Connecting to "); + 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()); + for (int i = 0; i < 50; i++) { // wait for connection + Serial.print("."); + delay(500); + if (WL_CONNECTED == WiFi.status()) break; + } + Serial.println(); + Serial.println("Connection status: " + String(WiFi.status())); + } + Serial.println(); + + debugNetworkInfo(); +} + +void RemoteControl::setup(RemoteControlRoutesCallback routes) { + _setupNetwork(); + routes(_app); // setup routes + _server.begin(); +} + +void RemoteControl::process() { + // TODO: check if we still have a connection. If not, reconnect. + WiFiClient client = _server.available(); + + if (client.connected()) { + _app.process(&client); + client.stop(); + } +} \ No newline at end of file diff --git a/controller/tea_poor/lib/RemoteControl/RemoteControl.h b/controller/tea_poor/lib/RemoteControl/RemoteControl.h new file mode 100644 index 0000000..ccb0f56 --- /dev/null +++ b/controller/tea_poor/lib/RemoteControl/RemoteControl.h @@ -0,0 +1,26 @@ +#ifndef REMOTECONTROL_H +#define REMOTECONTROL_H + +#include +#include +#include + +// define routes callback function signature +typedef void (*RemoteControlRoutesCallback)(Application &app); + +class RemoteControl { +public: + RemoteControl(const char* SSID, const char* SSIDPassword); + ~RemoteControl(); + void setup(RemoteControlRoutesCallback routes); + void process(); +private: + const String _SSID; + const String _SSIDPassword; + WiFiServer _server; + Application _app; + + void _setupNetwork(); +}; + +#endif \ No newline at end of file diff --git a/controller/tea_poor/src/main.cpp b/controller/tea_poor/src/main.cpp index 4a0aca8..87b8358 100644 --- a/controller/tea_poor/src/main.cpp +++ b/controller/tea_poor/src/main.cpp @@ -1,8 +1,6 @@ #include -#include -#include -#include #include +#include // Setting up water pump WaterPumpController waterPumpController(12, 9, 3); @@ -10,81 +8,11 @@ WaterPumpController waterPumpController(12, 9, 3); // Their is no reason to make it configurable and add unnecessary complexity const int WATER_PUMP_SAFE_THRESHOLD = 10 * 1000; -// setting up WiFi -const char *SSID = "MyWiFiNetwork"; -const char *PWD = "VerySecurePassword"; - -// minimalistic webserver -WiFiServer server(80); -Application app; - -void printMacAddress(byte mac[]) { - for (int i = 0; i < 6; i++) { - if (i > 0) { - Serial.print(":"); - } - if (mac[i] < 16) { - Serial.print("0"); - } - Serial.print(mac[i], HEX); - } - Serial.println(); -} - -void printCurrentNet() { - // print the SSID of the network you're attached to: - Serial.print("SSID: "); - Serial.println(WiFi.SSID()); - - // print the MAC address of the router you're attached to: - byte bssid[6]; - WiFi.BSSID(bssid); - Serial.print("BSSID: "); - printMacAddress(bssid); - - // print the received signal strength: - long rssi = WiFi.RSSI(); - Serial.print("signal strength (RSSI):"); - Serial.println(rssi); - - // print the encryption type: - byte encryption = WiFi.encryptionType(); - Serial.print("Encryption Type:"); - Serial.println(encryption, HEX); - Serial.println(); -} - -void connectToWiFi() { - if (WiFi.status() == WL_NO_MODULE) { - Serial.println("Communication with WiFi module failed!"); - // block further activity - while(true) delay(500); - } - - // info about your adapter - String firmware_version = WiFi.firmwareVersion(); - - Serial.print("WiFi Firmware Version: "); - Serial.println(firmware_version); - if ( firmware_version < WIFI_FIRMWARE_LATEST_VERSION) { - Serial.print("Latest available version: "); - Serial.println(WIFI_FIRMWARE_LATEST_VERSION); - Serial.println("Please upgrade your firmware."); - } - - Serial.print("Connecting to "); - Serial.println(SSID); - - WiFi.begin(SSID, PWD); - while (WiFi.status() != WL_CONNECTED) { - Serial.print("."); - delay(500); - } - - Serial.print("Connected. IP: "); - Serial.println(WiFi.localIP()); - printCurrentNet(); -} +// setting up remote control +RemoteControl remoteControl( + "MyWiFiNetwork", // network name/SSID + "VerySecurePassword" // network password +); bool isValidIntNumber(const char *str, const int maxValue, const int minValue=0) { if (strlen(str) <= 0) return false; @@ -116,21 +44,11 @@ void pour_tea(Request &req, Response &res) { void setup() { Serial.begin(9600); waterPumpController.setup(); - - // connect to WiFi - connectToWiFi(); - - // Set endpoints - app.post("/pour_tea", &pour_tea); - // setup Server - server.begin(); + remoteControl.setup([](Application &app) { + app.get("/pour_tea", pour_tea); + }); } void loop() { - WiFiClient client = server.available(); - - if (client.connected()) { - app.process(&client); - client.stop(); - } + remoteControl.process(); }; \ No newline at end of file