文章目錄
- 1 HTTP Client 實驗
- 1.1 新建工程
- 1.1.1 新建自己的工程app-http-client
- 1.1.2 直接使用官方例程esp_http_client
- 1.3 代碼分析
1 HTTP Client 實驗
本實驗是在app-wifi-station例程基礎之上添加樂鑫官方例程esp_http_client所做的實驗
app-wifi-station例程:作用是連接指定wifi;
樂鑫官方例程esp_http_client:作用是測驗http作為client的各種功能,

1.1 新建工程
如果只是想測驗http client相關的內容則推薦使用 1.1.2 直接使用官方例程esp_http_client
如果想新建自己的工程則使用 1.1.1 新建自己的工程app-http-client,但該方法較麻煩,
1.1.1 新建自己的工程app-http-client
- 復制app-wifi-station例程并改名為app-http-client;
- 將編譯生成的可燒錄檔案更名為app-http-client;

- 洗掉例程內舊的menu相關檔案(包括main檔案夾下),將官方例程esp_http_client內menu相關檔案復制過來,完成后如下:

- 將examples\common_components\protocol_examples_common檔案夾下的Kconfig.projbuild檔案內容復制到app-http-client\main檔案夾下的Kconfig.projbuild,這時會有連個wifi連接相關的配置,將舊的wifi配置部分替換到新復制過來的wifi配置完成后如下:

menu "Example Configuration"
config EXAMPLE_CONNECT_WIFI
bool "connect using WiFi interface"
default y
help
Protocol examples can use Wi-Fi and/or Ethernet to connect to the network.
Choose this option to connect with WiFi
if EXAMPLE_CONNECT_WIFI
config ESP_WIFI_SSID
string "WiFi SSID"
default "myssid"
help
SSID (network name) for the example to connect to.
config ESP_WIFI_PASSWORD
string "WiFi Password"
default "mypassword"
help
WiFi password (WPA or WPA2) for the example to use.
config ESP_MAXIMUM_RETRY
int "Maximum retry"
default 5
help
Set the Maximum retry to avoid station reconnecting to the AP unlimited when the AP is really inexistent.
endif
……/*省略了剩余代碼*/
endmenu
- 將官方例程下的howsmyssl_com_root_cert.pem檔案復制過來,并在main檔案夾下的CMakeLists.txt、component.mk檔案內增加該檔案


- 將官方例程esp_http_client_example.c檔案復制過來,更名為user_http_client.c,并添加進CMakeLists.txt

- 新建user_http_client.h檔案
- 打開user_http_client.c檔案,將app_main改為user_http_client_init,并洗掉多于的函式,完成后如下:

- 將user_http_client_init函式在user_http_client.h檔案中作宣告,再添加到station_example_main.c檔案下的app_main函式中

- 使用idf.py menuconfig指令配置wifi

- 使用idf.py build指令編譯工程
1.1.2 直接使用官方例程esp_http_client
- 直接將官方例程復制到自己做實驗的檔案夾下;
- 使用idf.py menuconfig指令配置wifi;
- 使用idf.py build編譯即可;
注意:
- 確保 examples\common_components\protocol_examples_common 該路徑下有如下官方庫檔案

- protocol_examples_common的作用就是連使用wifi或**Ethernet(以太網)**連接
1.3 代碼分析
- 在函式 http_test_task 中做了http client各種功能實驗
static void http_test_task(void *pvParameters)
{
http_rest_with_url(); /*使用URL連接http server*/
http_rest_with_hostname_path();/*使用hostname+path連接http server*/
.
.
.
ESP_LOGI(TAG, "Finish http example");
vTaskDelete(NULL);
}
- 在功能測驗用出現的 GET、POST、PUT、PATCH等,其含義如下

- HTTP 配置引數
/**
* @brief HTTP configuration
*/
typedef struct {
const char *url; /*!< HTTP URL, the information on the URL is most important, it overrides the other fields below, if any */
const char *host; /*服務器域名或ip地址 !< Domain or IP as string */
int port; /*埠 http默認80 https 默認443!< Port to connect, default depend on esp_http_client_transport_t (80 or 443) */
const char *username; /*用戶名,認證時使用!< Using for Http authentication */
const char *password; /*用戶密碼,認證時使用!< Using for Http authentication */
esp_http_client_auth_type_t auth_type; /*認證方式!< Http authentication type, see `esp_http_client_auth_type_t` */
const char *path; /*路徑!< HTTP Path, if not set, default is `/` */
const char *query; /*請求引數!< HTTP query */
const char *cert_pem; /*證書!< SSL server certification, PEM format as string, if the client requires to verify server */
const char *client_cert_pem; /*!< SSL client certification, PEM format as string, if the server requires to verify client */
const char *client_key_pem; /*!< SSL client key, PEM format as string, if the server requires to verify client */
esp_http_client_method_t method; /*!< HTTP Method */
int timeout_ms; /*請求超時!< Network timeout in milliseconds */
bool disable_auto_redirect; /*!< Disable HTTP automatic redirects */
int max_redirection_count; /*!< Max redirection number, using default value if zero*/
http_event_handle_cb event_handler; /*!< HTTP Event Handle */
esp_http_client_transport_t transport_type; /*!< HTTP transport type, see `esp_http_client_transport_t` */
int buffer_size; /*!< HTTP receive buffer size */
int buffer_size_tx; /*!< HTTP transmit buffer size */
void *user_data; /*!< HTTP user_data context */
bool is_async; /*!< Set asynchronous mode, only supported with HTTPS for now */
bool use_global_ca_store; /*!< Use a global ca_store for all the connections in which this bool is set. */
bool skip_cert_common_name_check; /*!< Skip any validation of server certificate CN field */
} esp_http_client_config_t;
- 實驗函式分析
實驗中用到的函式請參考官網:樂鑫官網HTTP client 決議
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/282172.html
標籤:其他
