From 039c29a39d7ef85af3613a1c497d544ea470e8a6 Mon Sep 17 00:00:00 2001 From: Thaddeus Hughes Date: Sat, 27 Dec 2025 11:52:57 -0600 Subject: [PATCH] OTA works, log download works, integrated OTA build sys --- .externalToolBuilders/OTA SC-F001.launch | 8 ++ .project | 10 ++ CMakeLists.txt | 2 +- main/control_fsm.c | 4 +- main/landingpage.html | 53 +++++++++- main/power_mgmt.c | 12 +-- main/rf_433.c | 128 ++++++++--------------- main/storage.h | 17 +-- main/webpage.h | 4 +- main/webpage_minified.html | 4 +- main/webserver.c | 40 ++++++- ota_deploy.bat | 49 +++++++++ 12 files changed, 219 insertions(+), 112 deletions(-) create mode 100644 .externalToolBuilders/OTA SC-F001.launch create mode 100644 ota_deploy.bat diff --git a/.externalToolBuilders/OTA SC-F001.launch b/.externalToolBuilders/OTA SC-F001.launch new file mode 100644 index 0000000..3c0455d --- /dev/null +++ b/.externalToolBuilders/OTA SC-F001.launch @@ -0,0 +1,8 @@ + + + + + + + + diff --git a/.project b/.project index f31512b..c50decb 100644 --- a/.project +++ b/.project @@ -11,6 +11,16 @@ + + org.eclipse.ui.externaltools.ExternalToolBuilder + full,incremental, + + + LaunchConfigHandle + <project>/.externalToolBuilders/OTA SC-F001.launch + + + org.eclipse.cdt.core.cnature diff --git a/CMakeLists.txt b/CMakeLists.txt index bda6977..90f2202 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,4 +3,4 @@ cmake_minimum_required(VERSION 3.5) include($ENV{IDF_PATH}/tools/cmake/project.cmake) -project(app-template) +project(SC-F001) diff --git a/main/control_fsm.c b/main/control_fsm.c index 26bddaa..743c8e1 100644 --- a/main/control_fsm.c +++ b/main/control_fsm.c @@ -50,7 +50,7 @@ void setRelay(int8_t relay, bool state) { void driveRelays() { uint8_t state = 0x00; - relay_states[0] = (current_time / 1000000) % 2; + //relay_states[0] = (current_time / 1000000) % 2; // for testing purposes for (uint8_t i=0; i<8; i++) { // if we command and efuse permits it set the relay @@ -60,7 +60,7 @@ void driveRelays() { } } - ESP_LOGI(TAG, "RELAY STATE: %x", state); + //ESP_LOGI(TAG, "RELAY STATE: %x", state); i2c_set_relays(state); } diff --git a/main/landingpage.html b/main/landingpage.html index c93d088..3e7dec1 100644 --- a/main/landingpage.html +++ b/main/landingpage.html @@ -22,11 +22,26 @@ - + + + + + + + + + + + + + + + +
Firmware
Log File
- - + + \ No newline at end of file diff --git a/main/webserver.c b/main/webserver.c index 0b81a66..27d2cd0 100644 --- a/main/webserver.c +++ b/main/webserver.c @@ -29,6 +29,8 @@ //#include "mdns.h" #include "webpage.h" +#include "esp_partition.h" + #define HOSTNAME "sc.local" #define SOFT_AP_SSID "stockcropper" @@ -54,9 +56,39 @@ static esp_err_t root_get_handler(httpd_req_t *req) { static esp_err_t log_get_handler(httpd_req_t *req) { ESP_LOGI(TAG, "log_get_handler"); - // Send the HTML response - httpd_resp_set_type(req, "text/html"); - return httpd_resp_send(req, html_content, HTTPD_RESP_USE_STRLEN); + + const esp_partition_t *storage_partition = esp_partition_find_first(ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_ANY, "storage"); + if (storage_partition == NULL) { + ESP_LOGE(TAG, "Storage partition not found"); + return httpd_resp_send_err(req, HTTPD_500_INTERNAL_SERVER_ERROR, "Storage partition not found"); + } + + size_t total_size = storage_partition->size; + char len_str[16]; + sprintf(len_str, "%u", (unsigned)total_size); + httpd_resp_set_type(req, "application/octet-stream"); + httpd_resp_set_hdr(req, "Content-Disposition", "attachment; filename=\"sc_storage.bin\""); + httpd_resp_set_hdr(req, "Content-Length", len_str); + + uint8_t buf[1024]; + size_t offset = 0; + while (offset < total_size) { + size_t to_read = MIN(sizeof(buf), total_size - offset); + esp_err_t err = esp_partition_read(storage_partition, offset, buf, to_read); + if (err != ESP_OK) { + ESP_LOGE(TAG, "Failed to read partition: %s", esp_err_to_name(err)); + return httpd_resp_send_err(req, HTTPD_500_INTERNAL_SERVER_ERROR, "Failed to read storage"); + } + if (httpd_resp_send_chunk(req, (const char *)buf, to_read) != ESP_OK) { + ESP_LOGE(TAG, "Failed to send chunk"); + return ESP_FAIL; + } + offset += to_read; + } + + // End chunked transfer + httpd_resp_send_chunk(req, NULL, 0); + return ESP_OK; } // set time: timestamp (unix epoch, seconds) @@ -203,6 +235,8 @@ static esp_err_t ota_post_handler(httpd_req_t *req) { // Send response FIRST httpd_resp_send(req, "OTA update successful, rebooting...", HTTPD_RESP_USE_STRLEN); + set_param_value_t(PARAM_BOOT_TIME, (param_value_t){.i64 = system_rtc_get_raw_time()}); + // THEN delay and reboot vTaskDelay(pdMS_TO_TICKS(2000)); // Give time for TCP to close properly esp_restart(); diff --git a/ota_deploy.bat b/ota_deploy.bat new file mode 100644 index 0000000..faaf534 --- /dev/null +++ b/ota_deploy.bat @@ -0,0 +1,49 @@ +@echo off +REM ESP32 OTA Deployment Script for Windows + +setlocal + +REM Configuration +set ESP32_IP=192.168.4.1 +set PROJECT_NAME=SC-F001 +set BUILD_DIR=build +set BINARY_FILE=%BUILD_DIR%\%PROJECT_NAME%.bin + +echo ======================================== +echo ESP32 OTA Deployment Script +echo ======================================== + + +REM Step 1: Check if binary exists +if not exist "%BINARY_FILE%" ( + echo Error: Binary file not found at %BINARY_FILE% + echo Please update PROJECT_NAME in this script + exit /b 1 +) + +for %%A in ("%BINARY_FILE%") do set BINARY_SIZE=%%~zA +echo Binary size: %BINARY_SIZE% bytes + +REM Step 2: Upload via OTA +echo. +echo [2/3] Uploading to ESP32 at %ESP32_IP%... + +curl -X POST --data-binary @"%BINARY_FILE%" -w "HTTP Code: %%{http_code}\n" -o nul "http://%ESP32_IP%/ota" + +if %ERRORLEVEL% NEQ 0 ( + echo Upload failed! Is the ESP32 reachable at %ESP32_IP%? + exit /b 1 +) + +echo Upload successful! +echo ESP32 should be rebooting now... + +REM Step 3: Wait for reboot +echo. +echo [3/3] Waiting for ESP32 to reboot... +timeout /t 5 /nobreak > nul + +echo Deployment complete! +echo Check your device to verify it's running the new firmware. + +exit /b 0 \ No newline at end of file