simple_ota_example.c 4.1 KB

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