OTA updates working

This commit is contained in:
Zuckerberg 2023-11-26 20:53:34 -07:00
parent bb665628a7
commit a0bfd2bcca
2 changed files with 64 additions and 23 deletions

View File

@ -10,6 +10,8 @@
[platformio] [platformio]
lib_dir = lib lib_dir = lib
lib_deps = hieromon/AutoConnect@^1.4.2
board_build.partitions = min_spiffs.csv
[env:tinypico-color] [env:tinypico-color]
platform = espressif32 platform = espressif32

View File

@ -1,47 +1,82 @@
#include <SPI.h> #include <SPI.h>
#include <WiFi.h> #include <WiFi.h>
#include <HTTPClient.h> #include <HTTPClient.h>
#include <WebServer.h>
#include <AutoConnect.h>
#include "eink.h" #include "eink.h"
#include "network.h"
void fetchAndDrawImage(const char *url); void fetchAndDrawImage(const char *url);
void hibernate_and_restart(); void hibernateAndRestart();
const char *ssid = WIFI_SSID; WebServer Server;
const char *password = WIFI_PASSWORD; 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 5 // hibernate time in seconds
// #define HIBERNATE_TIME_SEC 120 // 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 *clearImage = "http://192.168.3.192:8080/clearImage";
const char *fetchImage = "http://192.168.3.192:8080/fetchImage"; const char *fetchImage = "http://192.168.3.192:8080/fetchImage";
const char *calibrationImage = "http://192.168.3.192:8080/calibrationImage"; 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() void setup()
{ {
// configure as a pullup because it made wiring a tad bit simplier
pinMode(SLEEP_WAKE_PIN, INPUT_PULLUP);
Serial.begin(9600); Serial.begin(9600);
delay(3000); delay(START_UP_DELAY * 1000);
initDisplay(); bool supressDrawAndSleep = sleepWakeButtonPressed();
WiFi.begin(ssid, password); Config.ota = AC_OTA_BUILTIN;
Serial.println("Connecting"); Portal.config(Config);
while (WiFi.status() != WL_CONNECTED)
Server.on("/", rootPage);
Serial.println("Connecting to wireless...");
if (Portal.begin())
{ {
delay(1000); Serial.println("WiFi connected: " + WiFi.localIP().toString());
Serial.print(".");
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) void fetchAndDrawImage(const char *url)
@ -76,18 +111,21 @@ void fetchAndDrawImage(const char *url)
http.end(); http.end();
} }
void hibernate_and_restart() void hibernateAndRestart()
{ {
// Go to sleep and wake up later // 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.print(HIBERNATE_TIME_SEC);
Serial.println(" seconds"); Serial.println(" seconds");
// Configure wake up source as timer // Configure wake up source as timer
esp_sleep_enable_timer_wakeup(HIBERNATE_TIME_SEC * 1000000); 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 // 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_SLOW_MEM, ESP_PD_OPTION_OFF);
esp_sleep_pd_config(ESP_PD_DOMAIN_RTC_FAST_MEM, ESP_PD_OPTION_OFF); esp_sleep_pd_config(ESP_PD_DOMAIN_RTC_FAST_MEM, ESP_PD_OPTION_OFF);
esp_deep_sleep_start(); esp_deep_sleep_start();
@ -95,4 +133,5 @@ void hibernate_and_restart()
void loop() void loop()
{ {
Portal.handleClient();
} }