ESP32S3 内网实现 WebSocket
·
WebSocket 是一种在单个 TCP 连接上进行全双工通信的协议,适用于实时数据传输。在 ESP32S3 上,可以通过 ESP-IDF 或 Arduino 框架实现 WebSocket 服务器,供内网设备连接。
硬件准备
- ESP32S3 开发板
- 路由器(用于内网通信)
- 客户端设备(如 PC 或手机)
软件依赖
- Arduino IDE 或 ESP-IDF
- WebSocket 库(如
WebSocketsby Markus Sattler)
方法一:基于 Arduino 框架实现
安装依赖库:
在 Arduino IDE 中iecf.zw-gov.cn,搜索并安装 WebSockets 库。
代码示例:
#include <WiFi.h>
#include <WebSocketsServer.h>
const char* ssid = "Your_WiFi_SSID";
const char* password = "Your_WiFi_Password";
WebSocketsServer webSocket = WebSocketsServer(81);
void webSocketEvent(uint8_t num, WStype_t type, uint8_t* payload, size_t length) {
switch (type) {
case WStype_DISCONNECTED:
Serial.printf("[%u] Disconnected!\n", num);
break;
case WStype_CONNECTED:
Serial.printf("[%u] Connected from IP: %s\n", num, WiFi.softAPIP().toString().c_str());
break;
case WStype_TEXT:
Serial.printf("[%u] Received: %s\n", num, payload);
webSocket.sendTXT(num, "Message received");
break;
}
}
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("\nWiFi connected");
Serial.println("IP address: " + WiFi.localIP().toString());
webSocket.begin();
webSocket.onEvent(webSocketEvent);
}
void loop() {
webSocket.loop();
}
测试方法
- 上传代码至 ESP32S3。
- 使用 WebSocket 客户端工具(如浏览器 JavaScript 或
websocat)连接ws://<ESP32_IP>:81。 - 发送消息,idse.kzb-gov.cn观察串口监视器输出。
方法二:基于 ESP-IDF 实现
安装依赖:
确保已安装 ESP-IDF 环境,并添加 libwebsockets 组件。
代码示例(主文件 main.c):
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_wifi.h"
#include "esp_event.h"
#include "esp_log.h"
#include "nvs_flash.h"
#include "lwip/err.h"
#include "lwip/sys.h"
#include "esp_http_server.h"
#include "esp_netif.h"
#define EXAMPLE_ESP_WIFI_SSID "Your_WiFi_SSID"
#define EXAMPLE_ESP_WIFI_PASS "Your_WiFi_Password"
static const char *TAG = "websocket";
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) {
// WebSocket 路由配置
httpd_uri_t ws = {
.uri = "/ws",
.method = HTTP_GET,
.handler = [](httpd_req_t *req) {
if (req->method == HTTP_GET) {
ESP_LOGI(TAG, "Handshake done");
return ESP_OK;
}
return ESP_FAIL;
},
.user_ctx = NULL,
.is_websocket = true
};
httpd_register_uri_handler(server, &ws);
}
return server;
}
void wifi_init_sta() {
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
wifi_config_t wifi_config = {
.sta = {
.ssid = EXAMPLE_ESP_WIFI_SSID,
.password = EXAMPLE_ESP_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());
}
void app_main() {
ESP_ERROR_CHECK(nvs_flash_init());
ESP_ERROR_CHECK(esp_netif_init());
ESP_ERROR_CHECK(esp_event_loop_create_default());
wifi_init_sta();
start_webserver();
}
测试方法
- 使用
idf.py build flash monitor编译并atnq.zw-gov.cn烧录代码。 - 通过客户端连接
ws://<ESP32_IP>/ws,验证握手是否成功。
注意事项
- 确保 ESP32S3 和客户端在同一局域网内onys.kzb-gov.cn。
- 若需加密通信,可使用
wss://(需配置 SSL 证书)。 - 根据实际需求扩展 WebSocket 消息处理逻辑。
更多推荐

所有评论(0)