I'm a ESP32 newby and I'm studying how the potentialities of the ESP microcontrollers.
I'm trying to create an automatic firmware update that happens when I insert a particular URL. The ESP32 is configured as an access point, while my PC is connected to it.
If I insert: 192.168.2.1/update in my PC browser I activate an handler.
- httpd_uri_t do_update = {
- .uri = "/update",
- .method = HTTP_GET,
- .handler = update_handler,
- .user_ctx = server_data
- };
- httpd_register_uri_handler(server, &do_update);
I took the .bin file from the build project folder and put it into the SPIFFS folder.
I confirm it works.
- #define CONFIG_FIRMWARE_UPGRADE_URL "/spiffs/firmware.bin"
- static esp_err_t update_handler(httpd_req_t *req){
- int file_size = 0;
- esp_err_t ret;
- const esp_partition_t *update_partition;
- const esp_partition_t *configured = esp_ota_get_boot_partition();
- const esp_partition_t *running = esp_ota_get_running_partition();
- esp_ota_handle_t well_done_handle = 0;
- ESP_LOGI(TAG, "firmware.bin");
- // Open for reading firmware.bin
- FILE* f = fopen(CONFIG_FIRMWARE_UPGRADE_URL, "r");
- if (f == NULL) {
- ESP_LOGE(TAG, "Failed to open firmware.bin");
- return ESP_FAIL;
- }
- // I calculate the length of the file firmware.bin
- fseek(f, 0L, SEEK_END);
- file_size = ftell(f);
- rewind(f);
- ESP_LOGI(TAG, "Starting OTA example");
- // OTA partition
- update_partition = esp_ota_get_next_update_partition(NULL);
- ESP_LOGI(TAG, "Writing to partition subtype %d at offset 0x%x",
- update_partition->subtype, update_partition->address);
- assert(update_partition != NULL);
- // Reset and begin.
- ESP_ERROR_CHECK(esp_ota_begin(update_partition, OTA_SIZE_UNKNOWN, &well_done_handle));
- ret = esp_ota_write(well_done_handle, f, file_size);// **** HERE THE PROBLEM!!!! ****
- if (ret == ESP_OK) {
- ESP_ERROR_CHECK(esp_ota_end(well_done_handle));
- // I select a new boot partition
- ESP_ERROR_CHECK(esp_ota_set_boot_partition(update_partition));
- ESP_LOGI(TAG, "Restarting...");
- esp_restart();
- }
- else {
- ESP_LOGE(TAG, "Firmware upgrade failed");
- }
- while (1) {
- vTaskDelay(1000 / portTICK_PERIOD_MS);
- }
- fclose(f);
- return ESP_OK;
- }
OTA image has invalid magic byte (expected 0xE9, saw 0xa8)
However my .bin file starts with E9!
Here I show you my partition table (if it can help):
- nvs, data, nvs, 0x9000, 0x6000,
- phy_init, data, phy, 0xf000, 0x1000,
- factory, app, factory, 0x10000, 0x100000,
- ota_0, app, ota_0, 0x110000, 0x100000,
- ota_1, app, ota_1, 0x210000, 0x100000,
- storage, data, spiffs, 0x310000, 0xEE000,
- otadata, data, ota, 0x3FE000, 0x2000,
Regards,
Filippo