http、

Moderator: ESP_ZT

Nanazhang
Posts: 1
Joined: Mon Jul 08, 2024 10:59 am

http、

Postby Nanazhang » Mon Jul 08, 2024 11:09 am

#include <string.h>
#include <sys/param.h>
#include <stdlib.h>
#include <ctype.h>
#include "esp_log.h"
#include "nvs_flash.h"
#include "esp_event.h"
#include "esp_netif.h"
#include "protocol_examples_common.h"
#include "protocol_examples_utils.h"
#include "esp_tls.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_system.h"
#include "esp_http_client.h"

#include "driver/gpio.h"
#include "cjson.h"

#define GPIO_INPUT_PIN GPIO_NUM_0
#define MAX_HTTP_OUTPUT_BUFFER 2048

static const char *TAG = "HTTP_CLIENT";

static int temp=0;
static char *name;
static char *text;
static char *wind_class;
static int rh;


esp_err_t _http_event_handler(esp_http_client_event_t *evt)
{
static char *output_buffer; // Buffer to store response of http request from event handler
static int output_len; // Stores number of bytes read
switch(evt->event_id) {
case HTTP_EVENT_ERROR:
ESP_LOGD(TAG, "HTTP_EVENT_ERROR");
break;
case HTTP_EVENT_ON_CONNECTED:
ESP_LOGD(TAG, "HTTP_EVENT_ON_CONNECTED");
break;
case HTTP_EVENT_HEADER_SENT:
ESP_LOGD(TAG, "HTTP_EVENT_HEADER_SENT");
break;
case HTTP_EVENT_ON_HEADER:
ESP_LOGD(TAG, "HTTP_EVENT_ON_HEADER, key=%s, value=%s", evt->header_key, evt->header_value);
break;
case HTTP_EVENT_ON_DATA:
ESP_LOGD(TAG, "HTTP_EVENT_ON_DATA, len=%d", evt->data_len);
/*
* Check for chunked encoding is added as the URL for chunked encoding used in this example returns binary data.
* However, event handler can also be used in case chunked encoding is used.
*/
if (!esp_http_client_is_chunked_response(evt->client)) {
// If user_data buffer is configured, copy the response into the buffer
int copy_len = 0;
if (evt->user_data) {
copy_len = MIN(evt->data_len, (MAX_HTTP_OUTPUT_BUFFER - output_len));
if (copy_len) {
memcpy(evt->user_data + output_len, evt->data, copy_len);
}
} else {
const int buffer_len = esp_http_client_get_content_length(evt->client);
if (output_buffer == NULL) {
output_buffer = (char *) malloc(buffer_len);
output_len = 0;
if (output_buffer == NULL) {
ESP_LOGE(TAG, "Failed to allocate memory for output buffer");
return ESP_FAIL;
}
}
copy_len = MIN(evt->data_len, (buffer_len - output_len));
if (copy_len) {
memcpy(output_buffer + output_len, evt->data, copy_len);
}
}
output_len += copy_len;
}

break;
case HTTP_EVENT_ON_FINISH:
ESP_LOGD(TAG, "HTTP_EVENT_ON_FINISH");
if (output_buffer != NULL) {
// Response is accumulated in output_buffer. Uncomment the below line to print the accumulated response
// ESP_LOG_BUFFER_HEX(TAG, output_buffer, output_len);
free(output_buffer);
output_buffer = NULL;
}
output_len = 0;
break;
case HTTP_EVENT_DISCONNECTED:
ESP_LOGI(TAG, "HTTP_EVENT_DISCONNECTED");
int mbedtls_err = 0;
esp_err_t err = esp_tls_get_and_clear_last_error((esp_tls_error_handle_t)evt->data, &mbedtls_err, NULL);
if (err != 0) {
ESP_LOGI(TAG, "Last esp error code: 0x%x", err);
ESP_LOGI(TAG, "Last mbedtls failure: 0x%x", mbedtls_err);
}
if (output_buffer != NULL) {
free(output_buffer);
output_buffer = NULL;
}
output_len = 0;
break;
case HTTP_EVENT_REDIRECT:
ESP_LOGD(TAG, "HTTP_EVENT_REDIRECT");
esp_http_client_set_header(evt->client, "From", "user@example.com");
esp_http_client_set_header(evt->client, "Accept", "text/html");
esp_http_client_set_redirection(evt->client);
break;
}
return ESP_OK;
}

static void http_rest_with_url(void)
{
char local_response_buffer[MAX_HTTP_OUTPUT_BUFFER] = {0};

esp_http_client_config_t config = {
.url = "https://api.map.baidu.com/weather/v1/?d ... /百度地图获取API
.event_handler = _http_event_handler,
.user_data = local_response_buffer, // Pass address of local buffer to get response
.disable_auto_redirect = true,
};
esp_http_client_handle_t client = esp_http_client_init(&config);

// GET
esp_err_t err = esp_http_client_perform(client);

cJSON *root = cJSON_Parse(local_response_buffer);

cJSON *result = cJSON_GetObjectItem(root,"result");
cJSON *location = cJSON_GetObjectItem(result,"location");
cJSON *now = cJSON_GetObjectItem(result,"now");

name = cJSON_GetObjectItem(location,"name")->valuestring;
text = cJSON_GetObjectItem(now,"text")->valuestring;
temp = cJSON_GetObjectItem(now,"temp")->valueint;
rh = cJSON_GetObjectItem(now,"rh")->valueint;
wind_class = cJSON_GetObjectItem(now,"wind_class")->valuestring;

ESP_LOGI(TAG,"地区 %s",name);
ESP_LOGI(TAG,"天气 %s",text);
ESP_LOGI(TAG,"温度 %d",temp);
ESP_LOGI(TAG,"湿度 %d",rh);
ESP_LOGI(TAG,"风力 %s",wind_class);

cJSON_Delete(root);

esp_http_client_cleanup(client);
}

static void http_test_task(void *pvParameters)
{
while (1)
{
if(gpio_get_level(GPIO_INPUT_PIN) == 0)
{
http_rest_with_url();
vTaskDelay(pdMS_TO_TICKS(100));
}
vTaskDelay(pdMS_TO_TICKS(100));
}
vTaskDelete(NULL);
}

void app_main(void)
{
esp_err_t ret = nvs_flash_init();
if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
ESP_ERROR_CHECK(nvs_flash_erase());
ret = nvs_flash_init();
}
ESP_ERROR_CHECK(ret);

ESP_ERROR_CHECK(esp_netif_init());
ESP_ERROR_CHECK(esp_event_loop_create_default());
ESP_ERROR_CHECK(example_connect());
ESP_LOGI(TAG, "Connected to AP, begin http example");

xTaskCreate(&http_test_task, "http_test_task", 8192, NULL, 5, NULL);
}
在vscode中编译上述代码报错
Attachments
屏幕截图 2024-07-08 153051.png
图片是vs报错内容
屏幕截图 2024-07-08 153051.png (29.57 KiB) Viewed 1580 times
新建 DOCX 文档.docx
文档里是代码
(22.3 KiB) Downloaded 68 times

Who is online

Users browsing this forum: No registered users and 9 guests