参考chatgpt
Esp32可以配置为http服务器,可以socket编程。为了免除编写针对各种操作系统的app。完全可以用浏览器仿问esp32服务器,获取esp32的各种数据,甚至esp的音频,视频。也可以利用浏览器对esp进行各种操作。但esp不能主动仿问各客户端,只能客户端定时仿问服务器。
图片
图片
#include <string.h>
#include "freertos/FreeRTOS.h"
#include "freertos/event_groups.h"
#include "esp_wifi.h"
#include "esp_event.h"
#include "esp_log.h"
#include "nvs_flash.h"
#include "esp_http_server.h"// WiFi 配置信息
#define WIFI_SSID "ChinaNet-AETP5V"
#define WIFI_PASS "wf123456"static EventGroupHandle_t s_wifi_event_group;
static const int WIFI_CONNECTED_BIT = BIT0;
static const char *TAG = "WiFi_HTTP";// 事件处理程序
static void event_handler(void* arg, esp_event_base_t event_base, int32_t event_id, void* event_data) {if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START) {esp_wifi_connect(); // 启动连接} else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) {esp_wifi_connect(); // 重新连接ESP_LOGI(TAG, "重新连接...");} else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) {ip_event_got_ip_t* event = (ip_event_got_ip_t*) event_data;ESP_LOGI(TAG, "获取到IP: " IPSTR, IP2STR(&event->ip_info.ip));xEventGroupSetBits(s_wifi_event_group, WIFI_CONNECTED_BIT); // 设置连接标志位}
}// WiFi 初始化
void wifi_init_sta(void) {s_wifi_event_group = xEventGroupCreate(); // 创建事件组// 初始化 NVSesp_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);// 初始化 WiFiESP_ERROR_CHECK(esp_netif_init());ESP_ERROR_CHECK(esp_event_loop_create_default());esp_netif_create_default_wifi_sta();wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();ESP_ERROR_CHECK(esp_wifi_init(&cfg));ESP_ERROR_CHECK(esp_event_handler_instance_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &event_handler, NULL, NULL));ESP_ERROR_CHECK(esp_event_handler_instance_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &event_handler, NULL, NULL));// 设置 WiFi 连接参数wifi_config_t wifi_config = {.sta = {.ssid = WIFI_SSID,.password = WIFI_PASS,},};ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA)); // 设置为站点模式ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config));ESP_ERROR_CHECK(esp_wifi_start()); // 启动 WiFiESP_LOGI(TAG, "WiFi 初始化完成,开始连接");
}// HTTP GET 请求处理函数
esp_err_t hello_get_handler(httpd_req_t *req) {const char* resp_str = "Hello from ESP32!";httpd_resp_send(req, resp_str, strlen(resp_str)); // 发送响应return ESP_OK;
}// 注册 URI 处理程序
httpd_uri_t hello = {.uri = "/",.method = HTTP_GET,.handler = hello_get_handler,.user_ctx = NULL
};// 启动 HTTP 服务器
static httpd_handle_t start_webserver(void) {httpd_config_t config = HTTPD_DEFAULT_CONFIG();httpd_handle_t server = NULL;if (httpd_start(&server, &config) == ESP_OK) {httpd_register_uri_handler(server, &hello); // 注册处理函数}return server;
}void app_main(void) {// 初始化 WiFi 并连接wifi_init_sta();// 等待 WiFi 连接成功EventBits_t bits = xEventGroupWaitBits(s_wifi_event_group, WIFI_CONNECTED_BIT, pdFALSE, pdTRUE, portMAX_DELAY);if (bits & WIFI_CONNECTED_BIT) {ESP_LOGI(TAG, "WiFi ok");// 启动 HTTP 服务器start_webserver();} else {ESP_LOGI(TAG, "WiFi no");}
}