Skip to content

Latest commit

 

History

History
216 lines (119 loc) · 4.75 KB

2022-4-25-OTA.md

File metadata and controls

216 lines (119 loc) · 4.75 KB

自制OTA组件

功能:按下KEY1时进行OTA,目前仅测试http。

按照程序逻辑,该程序可以兼容https

依赖:KEY1组件

组件使用方法:

  1. 复制里面的全部文件到项目文件夹下的components文件夹中

  2. 修改主程序main文件夹下的cmakelist,添加依赖

idf_component_register(SRCS "main.c"
  
  ​          INCLUDE_DIRS "."
  
  ​          REQUIRES OTA)
  1. 修改main/main.c的app_main程序

    修改默认的nvs_flash_init()函数的调用,替换为如下

esp_err_t err = nvs_flash_init();

  if (err == ESP_ERR_NVS_NO_FREE_PAGES || err == ESP_ERR_NVS_NEW_VERSION_FOUND)

  {

​    // 1.OTA app partition table has a smaller NVS partition size than the non-OTA// partition table. This size mismatch may cause NVS initialization to fail.// 2.NVS partition contains data in new format and cannot be recognized by this version of code.// If this happens, we erase NVS partition and initialize NVS again.ESP_ERROR_CHECK(nvs_flash_erase());

​    err = nvs_flash_init();

  }

  ESP_ERROR_CHECK(err);



  get_sha256_of_partitions();

在完成联网的语句(如example_connect)后,添加如下语句

  ESP_ERROR_CHECK(example_connect());//示例连接


#if CONFIG_EXAMPLE_CONNECT_WIFI

  /* Ensure to disable any WiFi power save mode, this allows best throughput

   \* and hence timings for overall OTA operation.

   */

  esp_wifi_set_ps(WIFI_PS_NONE);

#endif // CONFIG_EXAMPLE_CONNECT_WIFI

然后调用OTA_Init()方法

4.修改menuconfig配置

(可选)采用example_connect的话,记得在menuconfig配置

Partition Table设置为

Factory app, two OTA definitions

ESP HTTPS OTA勾选

Allow HTTP for OTA (WARNING: ONLY FOR TESTING PURPOSE, READ HELP)

5.修改OTA固件下载地址,其位于components/OTA/OTA.c的48行

char FirmwareUrl[] = "http://192.168.137.1:8070/xiaozhi.bin";

6.(可选)该项目在ESP32-C3-12F-KIT上测试,其中有按键,其连接于GPIO9,按键为默认拉高,拉低时触发中断

该触发写在KEY1组件内

如果你需要修改其电平触发方式或引脚,可以自行修改components/KEY1/KEY1.c

#define GPIO_INPUT_IO_0 9

//change gpio intrrupt type for one pin
gpio_set_intr_type(GPIO_INPUT_IO_0, GPIO_INTR_ANYEDGE);

7.启动OTA服务器

bin目录中打开终端

python -m http.server 8070

注意:OTA需要固件空间为4m及以上。

速度测试

测试固件

Total sizes:
Used stat D/IRAM:   90204 bytes ( 237476 remain, 27.5% used)
      .data size:   11180 bytes
      .bss  size:   16560 bytes
      .text size:   62464 bytes
Used Flash size : 1421560 bytes
      .text     :  643222 bytes
      .rodata   :  122722 bytes
Total image size: 1495204 bytes (.bin may be padded larger)

实际bin大小814kb

串口下载

Wrote 834368 bytes (466462 compressed) at 0x00010000 in 14.0 seconds (effective 476.3 kbit/s)...
Hash of data verified.
Compressed 19840 bytes to 12005...
Wrote 19840 bytes (12005 compressed) at 0x00000000 in 0.6 seconds (effective 260.3 kbit/s)...
Hash of data verified.
Compressed 3072 bytes to 138...
Wrote 3072 bytes (138 compressed) at 0x00008000 in 0.1 seconds (effective 397.7 kbit/s)...
Hash of data verified.
Compressed 8192 bytes to 31...
Wrote 8192 bytes (31 compressed) at 0x0000d000 in 0.1 seconds (effective 574.2 kbit/s)...

476.3 kbit/s等于59.54 kB/s

串口速度:19.86s

OTA速度19.17s

可以忽略不计的差距.jpg

组件编译遇到的问题与解决

主要参考:

https://docs.espressif.com/projects/esp-idf/zh_CN/latest/esp32/api-guides/build-system.html#id28

set(EXTRA_COMPONENT_DIRS $ENV{IDF_PATH}/examples/common_components/protocol_examples_common)



idf_component_register(SRCS "OTA.c"

​          INCLUDE_DIRS "include"

​          REQUIRES KEY1 app_update esp_http_client esp_https_ota protocol_examples_common nvs_flash

​          EMBED_TXTFILES ./server_certs/ca_cert.pem)

REQUIRES后面跟你是components,其中包含你这个项目的私有components和公共components。

你发现该引用到的头文件没引用上,多半就是这个问题

去example的文件夹里搜索该头文件,然后它一般就是放在components文件夹下,如

esp_ota_ops.h文件夹放在

/home/xtx/esp/esp-idf/components/app_update/include/esp_ota_ops.h

那就需要加app_update到REQUIRES里

EMBED_TXTFILES ./server_certs/ca_cert.pem是用来处理当最后引用阶段出错的。

文件里该include的头文件就得做,这个编译系统更像是帮你做include环境变量的系统

/mnt/hgfs/xiaozhi/build/../components/OTA/OTA.c:103: undefined reference to `_binary_ca_cert_pem_start'

添加

EMBED_TXTFILES ./server_certs/ca_cert.pem