目录
- 参考教程
- 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)
{cJSON *pMacAdress = NULL;
#if 0
//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;}
#endifif (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 *pJsonRoot = cJSON_Parse(evt->data); // cJSON解析json格式数据if (pJsonRoot != NULL) // 如果是否json格式数据{pMacAdress = cJSON_GetObjectItem(pJsonRoot, "api"); // 解析字段字符串内容if (pMacAdress != NULL){if (cJSON_IsString(pMacAdress)) // 判断字段是否string类型{ESP_LOGI(TAG, "api = %s", pMacAdress->valuestring);}}pMacAdress = cJSON_GetObjectItem(pJsonRoot, "v"); // 解析字段字符串内容if (pMacAdress != NULL){if (cJSON_IsString(pMacAdress)) // 判断字段是否string类型{ESP_LOGI(TAG, "v = %s", pMacAdress->valuestring);}}pMacAdress = cJSON_GetObjectItem(pJsonRoot, "ret");if (pMacAdress != NULL){// ret的数据应该怎么提取,不知道.有知道的朋友麻烦留言告知}pMacAdress = cJSON_GetObjectItem(pJsonRoot, "data"); // 解析字段字符串内容if (pMacAdress != NULL){cJSON *pSub_2 = cJSON_GetObjectItem(pMacAdress, "t");if (pSub_2 != NULL){if (cJSON_IsString(pSub_2)) // 判断字段是否string类型{ESP_LOGI(TAG, "data/t = %s", pSub_2->valuestring);}}}}else{ESP_LOGI(TAG, "数据错误,不是JSon格式");}cJSON_Delete(pJsonRoot);}}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",// .url = "http://sensor.local/number/sensor_time",.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()); // 在menuconfig中wifi的账号密码xTaskCreate(&http_get_task, "http_get_task", 4096, NULL, 5, NULL);
}