update firmware automatically - #1920

This commit is contained in:
proddy
2024-08-18 13:18:07 +02:00
parent d9d854e456
commit 92a8a268a7
10 changed files with 252 additions and 19 deletions

View File

@@ -48,6 +48,8 @@
#include <esp_mac.h>
#endif
#include <HTTPClient.h>
namespace emsesp {
// Languages supported. Note: the order is important and must match locale_translations.h
@@ -504,11 +506,12 @@ void System::start() {
// button single click
void System::button_OnClick(PButton & b) {
LOG_NOTICE("Button pressed - single click - show settings folders");
LOG_NOTICE("Button pressed - single click");
#if defined(EMSESP_TEST)
#ifndef EMSESP_STANDALONE
Test::listDir(LittleFS, FS_CONFIG_DIRECTORY, 3);
// show filesystem
Test::listDir(LittleFS, "/", 3);
#endif
#endif
}
@@ -1150,7 +1153,7 @@ bool System::check_restore() {
LOG_ERROR("Unrecognized file uploaded");
}
} else {
LOG_ERROR("Unrecognized file uploaded, not json");
LOG_ERROR("Unrecognized file uploaded, not json. Will be removed.");
}
// close (just in case) and remove the temp file
@@ -1809,6 +1812,7 @@ bool System::ntp_connected() {
return ntp_connected_;
}
// see if its a BBQKees Gateway by checking the nvs values
String System::getBBQKeesGatewayDetails() {
#ifndef EMSESP_STANDALONE
if (!EMSESP::nvs_.isKey("mfg")) {
@@ -1829,4 +1833,88 @@ String System::getBBQKeesGatewayDetails() {
#endif
}
// Stream from an URL and send straight to OTA uploader
bool System::uploadFirmwareURL(const char * url) {
#ifndef EMSESP_STANDALONE
// configure temporary server and url
HTTPClient http;
http.setFollowRedirects(HTTPC_FORCE_FOLLOW_REDIRECTS); // important for GitHub
http.useHTTP10(true);
http.begin(String(url));
// start connection
int httpCode = http.GET();
if (httpCode != HTTP_CODE_OK) {
LOG_ERROR("Firmware upload failed - HTTP code %u", httpCode);
return false;
}
// check we have enough space
int firmware_size = http.getSize();
LOG_INFO("Firmware uploading (file: %s, size: %d bytes)", url, firmware_size);
if (!Update.begin(firmware_size)) {
LOG_ERROR("Firmware upload failed - no space");
return false;
}
EMSESP::system_.upload_status(true); // tell EMS-ESP we're uploading
// get tcp stream and send it to Updater
WiFiClient * stream = http.getStreamPtr();
if (Update.writeStream(*stream) != firmware_size) {
LOG_ERROR("Firmware upload failed - size differences");
return false;
}
if (!Update.end(true)) {
LOG_ERROR("Firmware upload error");
return false;
}
http.end();
LOG_INFO("Firmware uploaded successfully. Restarting...");
restart_requested(true); // not sure this is needed?
#endif
return true; // OK
/*
TODO backup code to save firmware to LittleFS first, in case of slow networks
// create buffer for reading in 128 byte chunks
const size_t buffer_size = 1024;
uint8_t buff[buffer_size] = {0};
File file = LittleFS.open("/new_firmware", "w"); // TODO find new name
if (!file) {
Serial.println("Failed to open file for writing");
return false;
}
// read all data from server
while (http.connected() && (firmware_size > 0 || firmware_size == -1)) {
// get available data size
size_t size = stream->available();
if (size) {
// read up to 128 byte
int c = stream->readBytes(buff, ((size > sizeof(buff)) ? sizeof(buff) : size));
file.write(buff, c);
if (firmware_size > 0) {
firmware_size -= c;
}
}
yield();
// delay(1); // so not to hurt WTD or timeout
}
file.close();
*/
}
} // namespace emsesp

View File

@@ -100,6 +100,8 @@ class System {
String getBBQKeesGatewayDetails();
static bool uploadFirmwareURL(const char * url);
void led_init(bool refresh);
void network_init(bool refresh);
void button_init(bool refresh);

View File

@@ -408,11 +408,16 @@ void Test::run_test(uuid::console::Shell & shell, const std::string & cmd, const
ok = true;
}
// THESE ONLY WORK WITH AN ESP32, not in standalone mode
// THESE ONLY WORK WITH AN ESP32, not in standalone/native mode
#ifndef EMSESP_STANDALONE
if (command == "ls") {
listDir(LittleFS, "/", 3);
Serial.println();
ok = true;
}
if (command == "upload") {
// S3 has 16MB flash
EMSESP::system_.uploadFirmwareURL("https://github.com/emsesp/EMS-ESP32/releases/download/latest/EMS-ESP-3_7_0-dev_31-ESP32S3-16MB+.bin"); // TODO remove
ok = true;
}
#endif
@@ -2278,7 +2283,9 @@ void Test::listDir(fs::FS & fs, const char * dirname, uint8_t levels) {
Serial.print(" DIR: ");
Serial.println(file.name());
if (levels) {
listDir(fs, file.name(), levels - 1);
// prefix a / to the name to make it a full path
listDir(fs, ("/" + String(file.name())).c_str(), levels - 1);
// listDir(fs, file.name(), levels - 1);
}
Serial.println();
} else {

View File

@@ -58,6 +58,8 @@ namespace emsesp {
// #define EMSESP_DEBUG_DEFAULT "custom"
// #define EMSESP_DEBUG_DEFAULT "scheduler"
// #define EMSESP_DEBUG_DEFAULT "heat_exchange"
// #define EMSESP_DEBUG_DEFAULT "ls"
#define EMSESP_DEBUG_DEFAULT "upload"
#ifndef EMSESP_DEBUG_DEFAULT
#define EMSESP_DEBUG_DEFAULT "general"

View File

@@ -1 +1 @@
#define EMSESP_APP_VERSION "3.7.0-dev.31"
#define EMSESP_APP_VERSION "3.7.0-dev.32"