ESP32 Quick Start (13): Brief Analysis of OTA Sample Code

This blog mainly records the implementation of ota in esp-idf. For related API references, please see ESP HTTPS OTA .

1. esp_http_client_config_t 介绍

In simple_ota example the esp_http_client_config_tfollowing:

esp_http_client_config_t config = {
    
    
    .url = CONFIG_EXAMPLE_FIRMWARE_UPGRADE_URL,
    .cert_pem = (char *)server_cert_pem_start,
    .event_handler = _http_event_handler,
};

It can be found that this config defines the following parameters:

  • url Set the HTTP URL to place the ota.bin file
  • cert_pem Set up SSL server certificate
  • event_handler Set HTTP event handler

Sometimes ota is also equipped .timeout_msto set ota reception timeout (idf in default 5000 ms), config is urlpossible make menuconfig -> Example Configuration -> firmware upgrade url endpointconfigurations, Wi-Fi SSID and PWD can make menuconfig -> Example Connection Configurationconfiguration.

Note: You can set config.skip_cert_common_name_check to true to skip the SSL check.

2. Introduction to OTA method

Method one: only in the menuconfig configuration url, ca_cert_pemsuch as the server information, the use of esp_https_ota()this API can be a ota upgrade.

Note: After this API is successfully executed, esp_restart() needs to be run immediately to reset the software.

Method 2: Use the following API in turn

  • esp_https_ota_begin(esp_https_ota_config_t *ota_config, esp_https_ota_handle_t *handle) Start the HTTPS OTA firmware upgrade, initialize the environment and establish an HTTPS connection
  • esp_https_ota_perform(esp_https_ota_handle_thttps_ota_handle) Read data from HTTP data stream and write to OTA
  • esp_https_ota_finish(esp_https_ota_handle_thttps_ota_handle) Clear the HTTPS OTA firmware upgrade and close the HTTPS connection

Note: esp_https_ota_is_complete_data_received(esp_https_ota_handle_thttps_ota_handle) can be used before esp_https_ota_finish(esp_https_ota_handle_thttps_ota_handle) to check whether all data has been received.

3. How to import CA certificate

Open CMakeLists.txt and you can see the following content:

# Embed the server root certificate into the final binary
idf_build_get_property(project_dir PROJECT_DIR)
idf_component_register(SRCS "simple_ota_example.c"
                    INCLUDE_DIRS "."
                    EMBED_TXTFILES ${
    
    project_dir}/server_certs/ca_cert.pem)

At this point the CA certificates in the ${project_dir}/server_certs/ca_cert.pemdirectory can be. Then use the following code to import and use the CA certificate:

extern const uint8_t server_cert_pem_start[] asm("_binary_ca_cert_pem_start");
extern const uint8_t server_cert_pem_end[] asm("_binary_ca_cert_pem_end");

void app_main(void)
{
    
    
    ...
    esp_http_client_config_t config = {
    
    
        .url = CONFIG_EXAMPLE_FIRMWARE_UPGRADE_URL,
        .cert_pem = (char *)server_cert_pem_start,
        .event_handler = _http_event_handler,
    };
    ...
}

Guess you like

Origin blog.csdn.net/zztiger123/article/details/106408793