目录
- 参考教程
- 1.使用的http连接
- 2.使用Vscode-IDF创建`http_request`例程
- 3.修改`http_request_example_main.c`函数
- 4.已经获取到响应的数据
参考教程
ESP-IDF HTTP获取网络时间
1.使用的http连接
http://api.m.taobao.com/rest/api3.do?api=mtop.common.getTimestamp
请求可以得到下面Json数据
2.使用Vscode-IDF创建http_request
例程
创建例程后,在设置中输入wifi的账号密码
3.修改http_request_example_main.c
函数
http_request_example_main.c
#include <string.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_system.h"
#include "esp_wifi.h"
#include "esp_event.h"
#include "esp_log.h"
#include "nvs_flash.h"
#include "protocol_examples_common.h"
#include "lwip/err.h"
#include "lwip/sockets.h"
#include "lwip/sys.h"
#include "lwip/netdb.h"
#include "lwip/dns.h"
#include "sdkconfig.h"
#include <cJSON.h>
#include "esp_http_client.h"
static const char *TAG = "example";
#define MAX_HTTP_OUTPUT_BUFFER 2048 // http最大数据接收大小
bool gettime = true; // 是否从网络请求时间
esp_err_t _http_event_handler(esp_http_client_event_t *evt)
{
/*switch判断事件代码提示出错可以写成if方式
switch (evt->event_id)
{
case HTTP_EVENT_ERROR:
ESP_LOGI(TAG, "HTTP_EVENT_ERROR");
break;
case HTTP_EVENT_ON_CONNECTED:
ESP_LOGI(TAG, "HTTP_EVENT_ON_CONNECTED");
break;
case HTTP_EVENT_HEADER_SENT:
ESP_LOGI(TAG, "HTTP_EVENT_HEADER_SENT");
break;
case HTTP_EVENT_ON_HEADER:
ESP_LOGI(TAG, "HTTP_EVENT_ON_HEADER");
printf("%.*s", evt->data_len, (char *)evt->data);
break;
case HTTP_EVENT_ON_DATA:
ESP_LOGI(TAG, "HTTP_EVENT_ON_DATA,len=%d", evt->data_len);
if (!esp_http_client_is_chunked_response(evt->client))
{
printf("%.*s", evt->data_len, (char *)evt->data);
}
break;
case HTTP_EVENT_ON_FINISH://http完成事件
ESP_LOGI(TAG, "HTTP_EVENT_ON_FINISH");
break;
case HTTP_EVENT_DISCONNECTED://http断开连接事件
ESP_LOGI(TAG, "HTTP_EVENT_DISCONNECTED");
break;
}
*/
if (evt->event_id == HTTP_EVENT_ON_DATA) // 收到数据事件
{
ESP_LOGI(TAG, "HTTP_EVENT_ON_DATA,len=%d", evt->data_len);
if (!esp_http_client_is_chunked_response(evt->client))
{
ESP_LOGI(TAG, "响应:%s", (char *)evt->data);
// cJSON *json = cJSON_Parse(evt->data); // cJSON解析json格式数据
// cJSON *time_json = cJSON_GetObjectItem(json, "data");
// char *timestamp = cJSON_GetObjectItem(time_json, "t")->valuestring;
// printf("%.*s", evt->data_len, (char *)evt->data);
// struct timeval now = {.tv_sec = (atoll(timestamp) / 1000)}; // 从网上获取到的时间戳要除1000
// settimeofday(&now, NULL); // ESPIDF官方提供的设置时间函数
// cJSON_Delete(json);
}
}
return ESP_OK;
}
static void http_get_task(void *pvParameters)
{
while (gettime)
{
char local_response_buffer[MAX_HTTP_OUTPUT_BUFFER] = {0};
esp_http_client_config_t config = {
.method = HTTP_METHOD_GET, // get方式请求数据
.url = "http://api.m.taobao.com/rest/api3.do?api=mtop.common.getTimestamp",
.event_handler = _http_event_handler,
.user_data = local_response_buffer,
};
esp_http_client_handle_t client = esp_http_client_init(&config);
esp_err_t err = esp_http_client_perform(client);
if (err == ESP_OK)
{
ESP_LOGI(TAG, "HTTP GET Status = %d, content_length = %d",
esp_http_client_get_status_code(client),
(int)esp_http_client_get_content_length(client));
gettime = false;
}
else
{
ESP_LOGI(TAG, "HTTP GET request failed: %s", esp_err_to_name(err));
}
ESP_LOGI(TAG, "%s\n", local_response_buffer);
esp_http_client_cleanup(client);
vTaskDelay(1000 / portTICK_PERIOD_MS);
}
while (1)
{
vTaskDelay(1000 / portTICK_PERIOD_MS);
}
}
void app_main(void)
{
ESP_ERROR_CHECK(nvs_flash_init());
ESP_ERROR_CHECK(esp_netif_init());
ESP_ERROR_CHECK(esp_event_loop_create_default());
ESP_ERROR_CHECK(example_connect());
xTaskCreate(&http_get_task, "http_get_task", 4096, NULL, 5, NULL);
}