From a0bfd2bcca415774f0ae6eb0cd0e005416d876a4 Mon Sep 17 00:00:00 2001 From: Zuckerberg Date: Sun, 26 Nov 2023 20:53:34 -0700 Subject: [PATCH] OTA updates working --- firmware/platformio.ini | 2 + firmware/src/main.cpp | 85 ++++++++++++++++++++++++++++++----------- 2 files changed, 64 insertions(+), 23 deletions(-) diff --git a/firmware/platformio.ini b/firmware/platformio.ini index 85ae10b..3241996 100644 --- a/firmware/platformio.ini +++ b/firmware/platformio.ini @@ -10,6 +10,8 @@ [platformio] lib_dir = lib +lib_deps = hieromon/AutoConnect@^1.4.2 +board_build.partitions = min_spiffs.csv [env:tinypico-color] platform = espressif32 diff --git a/firmware/src/main.cpp b/firmware/src/main.cpp index e95627d..a6c34cb 100644 --- a/firmware/src/main.cpp +++ b/firmware/src/main.cpp @@ -1,47 +1,82 @@ #include #include #include +#include +#include #include "eink.h" -#include "network.h" void fetchAndDrawImage(const char *url); -void hibernate_and_restart(); +void hibernateAndRestart(); -const char *ssid = WIFI_SSID; -const char *password = WIFI_PASSWORD; +WebServer Server; +AutoConnect Portal(Server); +AutoConnectConfig Config; + +#define START_UP_DELAY 3 // Time to wait before connecting to WiFi and beginning #define HIBERNATE_TIME_SEC 5 // hibernate time in seconds // #define HIBERNATE_TIME_SEC 120 // hibernate time in seconds +// Pin for waking the CPU and preventing default loop (get image, draw image, and hibernate) +// Must be an RTC GPIO Pin +#define SLEEP_WAKE_PIN 25 +#define SLEEP_WAKE_PIN_RTC GPIO_NUM_25 + const char *clearImage = "http://192.168.3.192:8080/clearImage"; const char *fetchImage = "http://192.168.3.192:8080/fetchImage"; const char *calibrationImage = "http://192.168.3.192:8080/calibrationImage"; +bool sleepWakeButtonPressed() +{ + return !digitalRead(SLEEP_WAKE_PIN); +} + +void rootPage() +{ + Server.sendHeader("Location", String("http://") + WiFi.localIP().toString() + String("/_ac")); + Server.send(302, "text/plain", ""); + Server.client().flush(); + Server.client().stop(); +} + void setup() { + // configure as a pullup because it made wiring a tad bit simplier + pinMode(SLEEP_WAKE_PIN, INPUT_PULLUP); + Serial.begin(9600); - delay(3000); + delay(START_UP_DELAY * 1000); - initDisplay(); + bool supressDrawAndSleep = sleepWakeButtonPressed(); - WiFi.begin(ssid, password); - Serial.println("Connecting"); - while (WiFi.status() != WL_CONNECTED) + Config.ota = AC_OTA_BUILTIN; + Portal.config(Config); + + Server.on("/", rootPage); + + Serial.println("Connecting to wireless..."); + if (Portal.begin()) { - delay(1000); - Serial.print("."); + Serial.println("WiFi connected: " + WiFi.localIP().toString()); + + if (!supressDrawAndSleep) + { + Serial.println("Fetching and drawing image."); + fetchAndDrawImage(fetchImage); + hibernateAndRestart(); + } + else + { + Serial.println("Default fetch, draw, sleep loop is supressed due to user pressing button."); + } + } + else + { + // TODO: should we still hibernate after a while in this case? + Serial.println("Cannot fetch image because network isn't connected / configured."); } - Serial.println(""); - Serial.print("Connected! IP Address: "); - Serial.println(WiFi.localIP()); - - // fetchAndDrawImage(clearImage); - // fetchAndDrawImage(calibrationImage); - fetchAndDrawImage(fetchImage); - - hibernate_and_restart(); } void fetchAndDrawImage(const char *url) @@ -76,18 +111,21 @@ void fetchAndDrawImage(const char *url) http.end(); } -void hibernate_and_restart() +void hibernateAndRestart() { // Go to sleep and wake up later - Serial.print("Begining hibernation. Waking up in "); + Serial.print("Beginning hibernation. Waking up in "); Serial.print(HIBERNATE_TIME_SEC); Serial.println(" seconds"); // Configure wake up source as timer esp_sleep_enable_timer_wakeup(HIBERNATE_TIME_SEC * 1000000); + // Allow being woken up by pin + esp_sleep_enable_ext0_wakeup(SLEEP_WAKE_PIN_RTC, 0); + // Enter hibernation mode - esp_sleep_pd_config(ESP_PD_DOMAIN_RTC_PERIPH, ESP_PD_OPTION_OFF); + // esp_sleep_pd_config(ESP_PD_DOMAIN_RTC_PERIPH, ESP_PD_OPTION_OFF); esp_sleep_pd_config(ESP_PD_DOMAIN_RTC_SLOW_MEM, ESP_PD_OPTION_OFF); esp_sleep_pd_config(ESP_PD_DOMAIN_RTC_FAST_MEM, ESP_PD_OPTION_OFF); esp_deep_sleep_start(); @@ -95,4 +133,5 @@ void hibernate_and_restart() void loop() { + Portal.handleClient(); }