https_request_example_main.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /* HTTPS GET Example using plain mbedTLS sockets
  2. *
  3. * Contacts the howsmyssl.com API via TLS v1.2 and reads a JSON
  4. * response.
  5. *
  6. * Adapted from the ssl_client1 example in mbedtls.
  7. *
  8. * Original Copyright (C) 2006-2016, ARM Limited, All Rights Reserved, Apache 2.0 License.
  9. * Additions Copyright (C) Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD, Apache 2.0 License.
  10. *
  11. *
  12. * Licensed under the Apache License, Version 2.0 (the "License");
  13. * you may not use this file except in compliance with the License.
  14. * You may obtain a copy of the License at
  15. *
  16. * http://www.apache.org/licenses/LICENSE-2.0
  17. *
  18. * Unless required by applicable law or agreed to in writing, software
  19. * distributed under the License is distributed on an "AS IS" BASIS,
  20. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  21. * See the License for the specific language governing permissions and
  22. * limitations under the License.
  23. */
  24. #include <string.h>
  25. #include <stdlib.h>
  26. #include "freertos/FreeRTOS.h"
  27. #include "freertos/task.h"
  28. #include "freertos/event_groups.h"
  29. #include "esp_wifi.h"
  30. #include "esp_event.h"
  31. #include "esp_log.h"
  32. #include "esp_system.h"
  33. #include "nvs_flash.h"
  34. #include "protocol_examples_common.h"
  35. #include "esp_netif.h"
  36. #include "lwip/err.h"
  37. #include "lwip/sockets.h"
  38. #include "lwip/sys.h"
  39. #include "lwip/netdb.h"
  40. #include "lwip/dns.h"
  41. #include "esp_tls.h"
  42. #include "esp_crt_bundle.h"
  43. /* Constants that aren't configurable in menuconfig */
  44. #define WEB_SERVER "www.howsmyssl.com"
  45. #define WEB_PORT "443"
  46. #define WEB_URL "https://www.howsmyssl.com/a/check"
  47. static const char *TAG = "example";
  48. static const char *REQUEST = "GET " WEB_URL " HTTP/1.0\r\n"
  49. "Host: "WEB_SERVER"\r\n"
  50. "User-Agent: esp-idf/1.0 esp32\r\n"
  51. "\r\n";
  52. static void https_get_task(void *pvParameters)
  53. {
  54. char buf[512];
  55. int ret, len;
  56. while(1) {
  57. esp_tls_cfg_t cfg = {
  58. .crt_bundle_attach = esp_crt_bundle_attach,
  59. };
  60. struct esp_tls *tls = esp_tls_conn_http_new(WEB_URL, &cfg);
  61. if(tls != NULL) {
  62. ESP_LOGI(TAG, "Connection established...");
  63. } else {
  64. ESP_LOGE(TAG, "Connection failed...");
  65. goto exit;
  66. }
  67. size_t written_bytes = 0;
  68. do {
  69. ret = esp_tls_conn_write(tls,
  70. REQUEST + written_bytes,
  71. strlen(REQUEST) - written_bytes);
  72. if (ret >= 0) {
  73. ESP_LOGI(TAG, "%d bytes written", ret);
  74. written_bytes += ret;
  75. } else if (ret != ESP_TLS_ERR_SSL_WANT_READ && ret != ESP_TLS_ERR_SSL_WANT_WRITE) {
  76. ESP_LOGE(TAG, "esp_tls_conn_write returned 0x%x", ret);
  77. goto exit;
  78. }
  79. } while(written_bytes < strlen(REQUEST));
  80. ESP_LOGI(TAG, "Reading HTTP response...");
  81. do
  82. {
  83. len = sizeof(buf) - 1;
  84. bzero(buf, sizeof(buf));
  85. ret = esp_tls_conn_read(tls, (char *)buf, len);
  86. if(ret == ESP_TLS_ERR_SSL_WANT_WRITE || ret == ESP_TLS_ERR_SSL_WANT_READ)
  87. continue;
  88. if(ret < 0)
  89. {
  90. ESP_LOGE(TAG, "esp_tls_conn_read returned -0x%x", -ret);
  91. break;
  92. }
  93. if(ret == 0)
  94. {
  95. ESP_LOGI(TAG, "connection closed");
  96. break;
  97. }
  98. len = ret;
  99. ESP_LOGD(TAG, "%d bytes read", len);
  100. /* Print response directly to stdout as it is read */
  101. for(int i = 0; i < len; i++) {
  102. putchar(buf[i]);
  103. }
  104. } while(1);
  105. exit:
  106. esp_tls_conn_delete(tls);
  107. putchar('\n'); // JSON output doesn't have a newline at end
  108. static int request_count;
  109. ESP_LOGI(TAG, "Completed %d requests", ++request_count);
  110. for(int countdown = 10; countdown >= 0; countdown--) {
  111. ESP_LOGI(TAG, "%d...", countdown);
  112. vTaskDelay(1000 / portTICK_PERIOD_MS);
  113. }
  114. ESP_LOGI(TAG, "Starting again!");
  115. }
  116. }
  117. void app_main(void)
  118. {
  119. ESP_ERROR_CHECK( nvs_flash_init() );
  120. ESP_ERROR_CHECK(esp_netif_init());
  121. ESP_ERROR_CHECK(esp_event_loop_create_default());
  122. /* This helper function configures Wi-Fi or Ethernet, as selected in menuconfig.
  123. * Read "Establishing Wi-Fi or Ethernet Connection" section in
  124. * examples/protocols/README.md for more information about this function.
  125. */
  126. ESP_ERROR_CHECK(example_connect());
  127. xTaskCreate(&https_get_task, "https_get_task", 8192, NULL, 5, NULL);
  128. }