simple_ota_example.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /* OTA example
  2. This example code is in the Public Domain (or CC0 licensed, at your option.)
  3. Unless required by applicable law or agreed to in writing, this
  4. software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  5. CONDITIONS OF ANY KIND, either express or implied.
  6. */
  7. #include "freertos/FreeRTOS.h"
  8. #include "freertos/task.h"
  9. #include "esp_system.h"
  10. #include "esp_event.h"
  11. #include "esp_event_loop.h"
  12. #include "esp_log.h"
  13. #include "esp_ota_ops.h"
  14. #include "esp_http_client.h"
  15. #include "esp_https_ota.h"
  16. #include "protocol_examples_common.h"
  17. #include "string.h"
  18. #include "nvs.h"
  19. #include "nvs_flash.h"
  20. #include "protocol_examples_common.h"
  21. #if CONFIG_EXAMPLE_CONNECT_WIFI
  22. #include "esp_wifi.h"
  23. #endif
  24. static const char *TAG = "simple_ota_example";
  25. extern const uint8_t server_cert_pem_start[] asm("_binary_ca_cert_pem_start");
  26. extern const uint8_t server_cert_pem_end[] asm("_binary_ca_cert_pem_end");
  27. #define OTA_URL_SIZE 256
  28. esp_err_t _http_event_handler(esp_http_client_event_t *evt)
  29. {
  30. switch (evt->event_id) {
  31. case HTTP_EVENT_ERROR:
  32. ESP_LOGD(TAG, "HTTP_EVENT_ERROR");
  33. break;
  34. case HTTP_EVENT_ON_CONNECTED:
  35. ESP_LOGD(TAG, "HTTP_EVENT_ON_CONNECTED");
  36. break;
  37. case HTTP_EVENT_HEADER_SENT:
  38. ESP_LOGD(TAG, "HTTP_EVENT_HEADER_SENT");
  39. break;
  40. case HTTP_EVENT_ON_HEADER:
  41. ESP_LOGD(TAG, "HTTP_EVENT_ON_HEADER, key=%s, value=%s", evt->header_key, evt->header_value);
  42. break;
  43. case HTTP_EVENT_ON_DATA:
  44. ESP_LOGD(TAG, "HTTP_EVENT_ON_DATA, len=%d", evt->data_len);
  45. break;
  46. case HTTP_EVENT_ON_FINISH:
  47. ESP_LOGD(TAG, "HTTP_EVENT_ON_FINISH");
  48. break;
  49. case HTTP_EVENT_DISCONNECTED:
  50. ESP_LOGD(TAG, "HTTP_EVENT_DISCONNECTED");
  51. break;
  52. }
  53. return ESP_OK;
  54. }
  55. void simple_ota_example_task(void *pvParameter)
  56. {
  57. ESP_LOGI(TAG, "Starting OTA example");
  58. esp_http_client_config_t config = {
  59. .url = CONFIG_EXAMPLE_FIRMWARE_UPGRADE_URL,
  60. .cert_pem = (char *)server_cert_pem_start,
  61. .event_handler = _http_event_handler,
  62. .keep_alive_enable = true,
  63. };
  64. #ifdef CONFIG_EXAMPLE_FIRMWARE_UPGRADE_URL_FROM_STDIN
  65. char url_buf[OTA_URL_SIZE];
  66. if (strcmp(config.url, "FROM_STDIN") == 0) {
  67. example_configure_stdin_stdout();
  68. fgets(url_buf, OTA_URL_SIZE, stdin);
  69. int len = strlen(url_buf);
  70. url_buf[len - 1] = '\0';
  71. config.url = url_buf;
  72. } else {
  73. ESP_LOGE(TAG, "Configuration mismatch: wrong firmware upgrade image url");
  74. abort();
  75. }
  76. #endif
  77. #ifdef CONFIG_EXAMPLE_SKIP_COMMON_NAME_CHECK
  78. config.skip_cert_common_name_check = true;
  79. #endif
  80. esp_err_t ret = esp_https_ota(&config);
  81. if (ret == ESP_OK) {
  82. esp_restart();
  83. } else {
  84. ESP_LOGE(TAG, "Firmware upgrade failed");
  85. }
  86. while (1) {
  87. vTaskDelay(1000 / portTICK_PERIOD_MS);
  88. }
  89. }
  90. void app_main()
  91. {
  92. // Initialize NVS.
  93. esp_err_t err = nvs_flash_init();
  94. if (err == ESP_ERR_NVS_NO_FREE_PAGES || err == ESP_ERR_NVS_NEW_VERSION_FOUND) {
  95. // 1.OTA app partition table has a smaller NVS partition size than the non-OTA
  96. // partition table. This size mismatch may cause NVS initialization to fail.
  97. // 2.NVS partition contains data in new format and cannot be recognized by this version of code.
  98. // If this happens, we erase NVS partition and initialize NVS again.
  99. ESP_ERROR_CHECK(nvs_flash_erase());
  100. err = nvs_flash_init();
  101. }
  102. ESP_ERROR_CHECK(err);
  103. tcpip_adapter_init();
  104. ESP_ERROR_CHECK(esp_event_loop_create_default());
  105. /* This helper function configures Wi-Fi or Ethernet, as selected in menuconfig.
  106. * Read "Establishing Wi-Fi or Ethernet Connection" section in
  107. * examples/protocols/README.md for more information about this function.
  108. */
  109. ESP_ERROR_CHECK(example_connect());
  110. #if CONFIG_EXAMPLE_CONNECT_WIFI
  111. /* Ensure to disable any WiFi power save mode, this allows best throughput
  112. * and hence timings for overall OTA operation.
  113. */
  114. esp_wifi_set_ps(WIFI_PS_NONE);
  115. #endif // CONFIG_EXAMPLE_CONNECT_WIFI
  116. xTaskCreate(&simple_ota_example_task, "ota_example_task", 8192, NULL, 5, NULL);
  117. }