feat: implement OTA updates over HTTP(S), initiated over MQTT

This commit is contained in:
2026-05-17 00:24:43 +02:00
parent 8992e311ba
commit b9195a0dda
10 changed files with 221 additions and 8 deletions
+2
View File
@@ -12,6 +12,8 @@ idf_component_register(
ble_scanner
mqtt_publisher
led_indicator
ota_manager
app_update
driver
esp_driver_gpio
)
+19 -2
View File
@@ -3,6 +3,8 @@
#include "ble_scanner.h"
#include "mqtt_publisher.h"
#include "led_indicator.h"
#include "ota_manager.h"
#include "esp_ota_ops.h"
#include "esp_wifi.h"
#include "esp_event.h"
@@ -19,7 +21,7 @@
#define TAG "main"
#define WIFI_CONNECTED_BIT BIT8 /* must not clash with mqtt_publisher bits (BIT0BIT6) */
#define WIFI_CONNECTED_BIT BIT8 /* must not clash with mqtt_publisher bits (BIT0-BIT7) */
#define DEFAULT_MQTT_PORT 1883
#define MDNS_QUERY_TIMEOUT_MS 3000
@@ -222,6 +224,15 @@ void app_main(void)
ESP_ERROR_CHECK(mqtt_publisher_init(sensor_id, broker_uri, s_evt));
xEventGroupWaitBits(s_evt, MQTT_CONNECTED_BIT, pdFALSE, pdTRUE, portMAX_DELAY);
/* Confirm this firmware is healthy so the bootloader won't roll back */
esp_ota_img_states_t ota_state;
const esp_partition_t *running = esp_ota_get_running_partition();
if (esp_ota_get_state_partition(running, &ota_state) == ESP_OK &&
ota_state == ESP_OTA_IMG_PENDING_VERIFY) {
esp_ota_mark_app_valid_cancel_rollback();
ESP_LOGI(TAG, "OTA rollback cancelled - firmware validated");
}
// init BLE scanner
ble_scanner_init(on_ble_scan_result);
ble_scanner_start();
@@ -233,7 +244,8 @@ void app_main(void)
const EventBits_t cmd_bits =
MQTT_CALIBRATE_START | MQTT_CALIBRATE_STOP |
MQTT_SELECTED_BIT | MQTT_DESELECTED_BIT |
MQTT_FACTORY_RESET_BIT | MQTT_RECONFIGURE_SETTINGS_BIT;
MQTT_FACTORY_RESET_BIT | MQTT_RECONFIGURE_SETTINGS_BIT |
MQTT_OTA_BIT;
bool calibrating = false;
bool selected = false;
@@ -262,6 +274,11 @@ void app_main(void)
selected = false;
if (!calibrating) led_indicator_set(LED_SCANNING);
}
if (bits & MQTT_OTA_BIT) {
const mqtt_ota_data_t *ota = mqtt_publisher_get_ota_data();
ESP_LOGI(TAG, "OTA requested: url=%s version=%s", ota->url, ota->version);
ota_manager_start(ota->url, ota->version);
}
if (bits & MQTT_FACTORY_RESET_BIT) {
// factory reset requested - erase NVS and reboot
ESP_LOGI(TAG, "Factory reset requested");