From 940d7bd48d927c130e5367825931080d3407e0ad Mon Sep 17 00:00:00 2001 From: Scott Baker Date: Fri, 10 Mar 2023 10:17:01 -0800 Subject: [PATCH] Add MAC/Uptime to HTML --- src/WebOTA.cpp | 32 +++++++++++++++++++++++++++++--- src/WebOTA.h | 1 + 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/src/WebOTA.cpp b/src/WebOTA.cpp index b30aaf0..b475260 100644 --- a/src/WebOTA.cpp +++ b/src/WebOTA.cpp @@ -106,7 +106,8 @@ const char INDEX_HTML[] PROGMEM = R"!^!(

Board: %s
- Flash: %0.1f KB + MAC: %s
+ Uptime: %s

@@ -220,7 +221,7 @@ int WebOTA::add_http_routes(WebServer *server, const char *path) { if (this->custom_html != NULL) { html = this->custom_html; } else { - uint32_t maxSketchSpace = this->max_sketch_size(); + //uint32_t maxSketchSpace = this->max_sketch_size(); #if defined(ESP8266) const char* BOARD_NAME = "ESP8266"; @@ -230,8 +231,11 @@ int WebOTA::add_http_routes(WebServer *server, const char *path) { const char* BOARD_NAME = "Unknown"; #endif + String uptime_str = human_time(millis() / 1000); + const char* mac_addr = WiFi.macAddress().c_str(); + char buf[1024]; - snprintf_P(buf, sizeof(buf), INDEX_HTML, WEBOTA_VERSION, BOARD_NAME, maxSketchSpace / float(1024)); + snprintf_P(buf, sizeof(buf), INDEX_HTML, WEBOTA_VERSION, BOARD_NAME, mac_addr, uptime_str.c_str()); html = buf; } @@ -339,6 +343,28 @@ String ip2string(IPAddress ip) { return ret; } +String WebOTA::human_time(uint32_t sec) { + int days = (sec / 86400); + sec = sec % 86400; + int hours = (sec / 3600); + sec = sec % 3600; + int mins = (sec / 60); + sec = sec % 60; + + char buf[20] = ""; + if (days) { + snprintf(buf, sizeof(buf), "%d days %d hours\n", days, hours); + } else if (hours) { + snprintf(buf, sizeof(buf), "%d hours %d minutes\n", hours, mins); + } else { + snprintf(buf, sizeof(buf), "%d minutes %d seconds\n", mins, sec); + } + + String ret = buf; + + return ret; +} + int init_wifi(const char *ssid, const char *password, const char *mdns_hostname) { WiFi.mode(WIFI_STA); WiFi.begin(ssid, password); diff --git a/src/WebOTA.h b/src/WebOTA.h index 3ab856a..4705e8b 100644 --- a/src/WebOTA.h +++ b/src/WebOTA.h @@ -33,6 +33,7 @@ class WebOTA { bool init_has_run; char const * custom_html = NULL; String get_ota_html(); + String human_time(uint32_t sec); long max_sketch_size(); };