sz_x: OTAのイメージファイルのサイズ
x_path: OTAのイメージファイルのパス
esp_ota_get_next_update_partition()
↓
esp_ota_begin()
↓
esp_ota_write()
↓
esp_ota_end()
↓
esp_ota_set_boot_partition
↓
ESP.restart()
#include <esp_ota_ops.h>
void ota_test(void)
{
esp_err_t r = ESP_OK;
const esp_partition_t *part;
esp_ota_handle_t handle;
part = esp_ota_get_next_update_partition(NULL);
FILE *fp_x = fopen(x_path,"rb");
r = ((fp_x == NULL) ? ESP_FAIL : ESP_OK);
if (r == ESP_OK) r = esp_ota_begin(part, OTA_SIZE_UNKNOWN, &handle);
if (r == ESP_OK)
{
for (int i=0; i < (sz_x / 512); i++)
{
size_t rsz = fread(buf,sizeof(uint8_t),512,fp_x);
if (rsz != 512)
{
Serial.println("Read error (ota)");
r = ESP_FAIL;
break;
}
else
{
r = esp_ota_write(handle, (const void*)buf, 512);
}
}
if (r == ESP_OK && (sz_x % 512) > 0)
{
size_t rsz = fread(buf,sizeof(uint8_t),(sz_x % 512),fp_x);
if (rsz != (sz_x % 512))
{
Serial.println("Read error (ota)");
r = ESP_FAIL;
}
else
{
r = esp_ota_write(handle, (const void*)buf,(sz_x % 512));
}
}
}
if (r == ESP_OK) r = esp_ota_end(handle);
if (r == ESP_OK) r = esp_ota_set_boot_partition(part);
ESP.restart();
}