From fbaaddcab75061f496ea2f0528ef696ebf7ac4b8 Mon Sep 17 00:00:00 2001 From: Jonathas Barbosa Date: Sat, 1 Apr 2023 20:43:00 -0300 Subject: [PATCH] Improv and settings page --- firmware/lib/cw-commons/CWWebServer.h | 196 ++++++++++-------- firmware/lib/cw-commons/SettingsWebPage.h | 137 ++++++++++++ .../{IOManager.h => StatusController.h} | 10 +- firmware/lib/cw-commons/WiFiConnect.h | 66 ------ firmware/lib/cw-commons/WiFiController.h | 57 +++++ firmware/platformio.ini | 2 +- firmware/src/main.cpp | 22 +- 7 files changed, 328 insertions(+), 162 deletions(-) create mode 100644 firmware/lib/cw-commons/SettingsWebPage.h rename firmware/lib/cw-commons/{IOManager.h => StatusController.h} (99%) delete mode 100644 firmware/lib/cw-commons/WiFiConnect.h create mode 100644 firmware/lib/cw-commons/WiFiController.h diff --git a/firmware/lib/cw-commons/CWWebServer.h b/firmware/lib/cw-commons/CWWebServer.h index 79e9751..b8b48b3 100644 --- a/firmware/lib/cw-commons/CWWebServer.h +++ b/firmware/lib/cw-commons/CWWebServer.h @@ -1,104 +1,134 @@ #pragma once -#include "ImprovWiFiLibrary.h" +#define HTTP_GET_SIZE 3 +#define HTTP_POST_SIZE 4 + #include #include -#include "IOManager.h" - +#include "StatusController.h" +#include "SettingsWebPage.h" WiFiServer server(80); -ImprovWiFi improvSerial(&Serial); struct ClockwiseWebServer { - - char linebuf[80]; - int charcount = 0; - - - static void onImprovWiFiErrorCb(improv::Error err) + String httpBuffer; + + static ClockwiseWebServer *getInstance() + { + static ClockwiseWebServer base; + return &base; + } + + void startWebServer() + { + server.begin(); + StatusController::getInstance()->blink_led(100, 3); + } + + void stopWebServer() + { + server.stop(); + } + + void handleHttpRequest() + { + WiFiClient client = server.available(); + if (client) { - server.stop(); - IOManager::blink_led(2000, 3); - } - - static void onImprovWiFiConnectedCb(std::string ssid, std::string password) - { - ClockwiseParams::getInstance()->load(); - ClockwiseParams::getInstance()->wifiSsid = String(ssid.c_str()); - ClockwiseParams::getInstance()->wifiPwd = String(password.c_str()); - ClockwiseParams::getInstance()->save(); - - startCWWebServer(); - } - - static void startCWWebServer() { - server.begin(); - IOManager::blink_led(100, 3); - } + StatusController::getInstance()->blink_led(100, 1); - void begin() - { - WiFi.mode(WIFI_STA); - WiFi.disconnect(); - - improvSerial.setDeviceInfo(improv::ChipFamily::CF_ESP32, "CW-20230324", "1.1.0", "Clockwise"); - improvSerial.onImprovWiFiError(onImprovWiFiErrorCb); - improvSerial.onImprovWiFiConnected(onImprovWiFiConnectedCb); - - ClockwiseParams::getInstance()->load(); - if (improvSerial.tryConnectToWifi(ClockwiseParams::getInstance()->wifiSsid.c_str(), ClockwiseParams::getInstance()->wifiPwd.c_str())) { - startCWWebServer(); - } - - } - - void handleHttpRequest() - { + while (client.connected()) + { + if (client.available()) + { + char c = client.read(); + httpBuffer.concat(c); - improvSerial.handleSerial(); + if (c == '\n') + { + //Serial.println(httpBuffer); - if (!improvSerial.isConnected()) - return; + uint8_t method_pos = httpBuffer.indexOf(' '); + uint8_t path_pos = httpBuffer.indexOf(' ', method_pos + 1); - WiFiClient client = server.available(); + String method = httpBuffer.substring(0, method_pos); + String path = httpBuffer.substring(method_pos + 1, path_pos); + String key = ""; + String value = ""; - if (client) - { - IOManager::blink_led(100, 1); - memset(linebuf, 0, sizeof(linebuf)); - charcount = 0; - // an http request ends with a blank line - boolean currentLineIsBlank = true; - while (client.connected()) + if (method == "POST") { - if (client.available()) - { - char c = client.read(); - // read char by char HTTP request - linebuf[charcount] = c; - if (charcount < sizeof(linebuf) - 1) - charcount++; - - if (c == '\n' && currentLineIsBlank) - { - // send a standard http response header - client.println("HTTP/1.1 200 OK"); - client.println("Content-Type: text/html"); - client.println("Connection: close"); // the connection will be closed after completion of the response - client.println(); - client.println(""); - client.println("

Welcome!

"); - client.println("

This is a simple webpage served by your ESP32

"); - client.println(""); - break; - } - } + key = path.substring(path.indexOf('?') + 1, path.indexOf('=')); + value = path.substring(path.indexOf('=') + 1, ' '); + path = path.substring(0, path.indexOf('?')); } - delay(1); - client.stop(); + + processRequest(client, method, path, key, value); + httpBuffer = ""; + break; + } } + } + delay(1); + client.stop(); + } + } + + void processRequest(WiFiClient client, String method, String path, String key, String value) + { + + // Serial.println(method); + // Serial.println(path); + // Serial.println(key); + // Serial.println(value); + + if (method == "GET" && path == "/") { + client.println("HTTP/1.0 200 OK"); + client.println("Content-Type: text/html"); + client.println(); + client.println(SETTINGS_PAGE); + } else if (method == "GET" && path == "/get") { + getCurrentSettings(client); + } else if (method == "POST" && path == "/set") { + ClockwiseParams::getInstance()->load(); + + //a baby seal has died due this ifs + if (key == ClockwiseParams::getInstance()->PREF_DISPLAY_BRIGHT) { + ClockwiseParams::getInstance()->displayBright = value.toInt(); + } else if (key == ClockwiseParams::getInstance()->PREF_SWAP_BLUE_GREEN) { + ClockwiseParams::getInstance()->swapBlueGreen = (value == "1"); + } else if (key == ClockwiseParams::getInstance()->PREF_USE_24H_FORMAT) { + ClockwiseParams::getInstance()->use24hFormat = (value == "1"); + } else if (key == ClockwiseParams::getInstance()->PREF_TIME_ZONE) { + ClockwiseParams::getInstance()->timeZone = value; + } + ClockwiseParams::getInstance()->save(); + client.println("HTTP/1.0 204 No Content"); } + } + + + void getCurrentSettings(WiFiClient client) { + ClockwiseParams::getInstance()->load(); + + char response[256]; + snprintf(response, sizeof(response), "{\"%s\":%d,\"%s\":%d,\"%s\":%d,\"%s\":\"%s\",\"%s\":\"%s\"}", \ + ClockwiseParams::getInstance()->PREF_DISPLAY_BRIGHT, + ClockwiseParams::getInstance()->displayBright, + ClockwiseParams::getInstance()->PREF_SWAP_BLUE_GREEN, + ClockwiseParams::getInstance()->swapBlueGreen, + ClockwiseParams::getInstance()->PREF_USE_24H_FORMAT, + ClockwiseParams::getInstance()->use24hFormat, + ClockwiseParams::getInstance()->PREF_TIME_ZONE, + ClockwiseParams::getInstance()->timeZone.c_str(), + ClockwiseParams::getInstance()->PREF_WIFI_SSID, + ClockwiseParams::getInstance()->wifiSsid.c_str()); + + client.println("HTTP/1.0 200 OK"); + client.println("Content-Type: application/json"); + client.println(); + client.println(response); + } - }; diff --git a/firmware/lib/cw-commons/SettingsWebPage.h b/firmware/lib/cw-commons/SettingsWebPage.h new file mode 100644 index 0000000..1d32f96 --- /dev/null +++ b/firmware/lib/cw-commons/SettingsWebPage.h @@ -0,0 +1,137 @@ +#pragma once + +#include + + + +typedef struct SettingsCards { + const char* title; + const char* description; + const char* formInput; + const char* icon; + const char* propName; +} SettingsCard; + +const char SETTINGS_PAGE[] PROGMEM = R""""( + + +Clockwise Settings + + + + + +
+ Logo +
+ +
+

.:: Settings Page

+
+
+ +
+ + + +)""""; diff --git a/firmware/lib/cw-commons/IOManager.h b/firmware/lib/cw-commons/StatusController.h similarity index 99% rename from firmware/lib/cw-commons/IOManager.h rename to firmware/lib/cw-commons/StatusController.h index c020e11..983eeb1 100644 --- a/firmware/lib/cw-commons/IOManager.h +++ b/firmware/lib/cw-commons/StatusController.h @@ -549,8 +549,14 @@ const uint16_t epd_bitmap_qrcode[] PROGMEM = { 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff}; -struct IOManager +struct StatusController { + + static StatusController* getInstance() { + static StatusController base; + return &base; + } + void clockwiseLogo() { Locator::getDisplay()->drawRGBBitmap(1, 1, epd_bitmap_clockwise64, 63, 21); @@ -588,7 +594,7 @@ struct IOManager Locator::getDisplay()->print(buf); } - static void blink_led(int d, int times) + void blink_led(int d, int times) { for (int j = 0; j < times; j++) { diff --git a/firmware/lib/cw-commons/WiFiConnect.h b/firmware/lib/cw-commons/WiFiConnect.h deleted file mode 100644 index b8db366..0000000 --- a/firmware/lib/cw-commons/WiFiConnect.h +++ /dev/null @@ -1,66 +0,0 @@ -#pragma once - -#include -#include -#include "IOManager.h" - - -WiFiManager wifiManager; -Timezone myTZ; - -struct WiFiConnect -{ - std::vector _menu = {"wifi","exit"}; - char timezone[40]="0"; - bool shouldSaveConfig = false; - - void saveTimezone(const char* value) { - Serial.print("Save: "); - Serial.println(value); - myTZ.setCache(0); - myTZ.setLocation(value); - shouldSaveConfig = false; - } - - String loadTimezone() { - myTZ.setCache(0); - String tz = myTZ.getTimezoneName(); - Serial.print("Load: "); - Serial.println(tz); - return tz; - } - - - void connect() - { - IOManager io; - - bool resp; - //wifiManager.resetSettings(); - wifiManager.setSaveConfigCallback([&](){ shouldSaveConfig = true; }); - - wifiManager.setAPCallback([&](WiFiManager *myWiFiManager){ - io.wifiConnectionFailed(); - }); - - sprintf(timezone, "%s", loadTimezone().c_str()); - WiFiManagerParameter timezoneParam("tz", "Inform your timezone (e.g. America/Lima)", timezone, 36); - - wifiManager.setTitle("Clockwise Wifi Setup"); - wifiManager.setMenu(_menu); - wifiManager.addParameter(&timezoneParam); - - resp = wifiManager.autoConnect("Clockwise-Wifi", "12345678"); - - if (!resp) { - Serial.println("Failed to connect"); - delay(3000); - ESP.restart(); - } else { - Serial.println("connected!"); - if (shouldSaveConfig) { - saveTimezone(timezoneParam.getValue()); - } - } - } -}; diff --git a/firmware/lib/cw-commons/WiFiController.h b/firmware/lib/cw-commons/WiFiController.h new file mode 100644 index 0000000..d54588e --- /dev/null +++ b/firmware/lib/cw-commons/WiFiController.h @@ -0,0 +1,57 @@ +#pragma once + +#include "ImprovWiFiLibrary.h" +#include +#include "CWWebServer.h" +#include "StatusController.h" + +ImprovWiFi improvSerial(&Serial); + +struct WiFiController +{ + static void onImprovWiFiErrorCb(ImprovTypes::Error err) + { + ClockwiseWebServer::getInstance()->stopWebServer(); + StatusController::getInstance()->blink_led(2000, 3); + } + + static void onImprovWiFiConnectedCb(const char* ssid, const char* password) + { + ClockwiseParams::getInstance()->load(); + ClockwiseParams::getInstance()->wifiSsid = String(ssid); + ClockwiseParams::getInstance()->wifiPwd = String(password); + ClockwiseParams::getInstance()->save(); + + ClockwiseWebServer::getInstance()->startWebServer(); + } + + static bool isConnected() { + return improvSerial.isConnected(); + } + + static void handleImprovWiFi() + { + improvSerial.handleSerial(); + } + + void begin() + { + WiFi.mode(WIFI_STA); + WiFi.disconnect(); + + improvSerial.setDeviceInfo(ImprovTypes::ChipFamily::CF_ESP32, "CW-20230324", "1.1.0", "Clockwise"); + improvSerial.onImprovError(onImprovWiFiErrorCb); + improvSerial.onImprovConnected(onImprovWiFiConnectedCb); + + ClockwiseParams::getInstance()->load(); + + if (!ClockwiseParams::getInstance()->wifiSsid.isEmpty()) { + if (improvSerial.tryConnectToWifi(ClockwiseParams::getInstance()->wifiSsid.c_str(), ClockwiseParams::getInstance()->wifiPwd.c_str())) + { + ClockwiseWebServer::getInstance()->startWebServer(); + } + } + } + + +}; diff --git a/firmware/platformio.ini b/firmware/platformio.ini index ad70a77..50fb97f 100644 --- a/firmware/platformio.ini +++ b/firmware/platformio.ini @@ -26,5 +26,5 @@ lib_deps = arduino-libraries/NTPClient @ ^3.1.0 https://github.com/PaulStoffregen/Time.git ropg/ezTime@^0.8.3 - https://github.com/tzapu/WiFiManager.git@^2.0.11-beta + https://github.com/jnthas/Improv-WiFi-Library src_filter = +<*> -<.git/> -<.svn/> - - - - - diff --git a/firmware/src/main.cpp b/firmware/src/main.cpp index 7118e86..1539d53 100644 --- a/firmware/src/main.cpp +++ b/firmware/src/main.cpp @@ -4,22 +4,18 @@ // Clockface #include // Commons -#include +#include #include #include #include -#include +#include MatrixPanel_I2S_DMA *dma_display = nullptr; Clockface *clockface; -WiFiConnect wifi; +WiFiController wifi; CWDateTime cwDateTime; -IOManager io; - -ClockwiseWebServer webserver; - void displaySetup(bool swapBlueGreen, uint8_t displayBright) { @@ -51,14 +47,15 @@ void displaySetup(bool swapBlueGreen, uint8_t displayBright) void setup() { Serial.begin(115200); - pinMode(2, OUTPUT); - webserver.begin(); + StatusController::getInstance()->blink_led(5, 100); + wifi.begin(); Serial.println(ClockwiseParams::getInstance()->swapBlueGreen); Serial.println(ClockwiseParams::getInstance()->use24hFormat); Serial.println(ClockwiseParams::getInstance()->displayBright); + Serial.println(ClockwiseParams::getInstance()->timeZone); Serial.println(ClockwiseParams::getInstance()->wifiSsid); Serial.println(ClockwiseParams::getInstance()->wifiPwd); @@ -76,7 +73,12 @@ void setup() void loop() { + wifi.handleImprovWiFi(); + + if (WiFiController::isConnected()) + { + ClockwiseWebServer::getInstance()->handleHttpRequest(); + } - webserver.handleHttpRequest(); // clockface->update(); }