8266使用websocket库

安装 WebSocket 库

  1. 使用 Arduino IDE 安装

    • 打开 Arduino IDE。

    • 转到 Sketch > Include Library > Manage Libraries...

    • 在搜索框中输入"WebSockets"并查找 WebSockets by Markus Sattler 的库。

    • 点击安装。

  2. 最下面那个,安装

  3. 编译运行测试,通过,没问题

下来我贴一些测试代码

复制代码
#include <WebSocketsServer.h>
#include <ESP8266WiFi.h>

// 替换为你的WiFi网络凭据
const char* ssid = "your_SSID";
const char* password = "your_PASSWORD";

// WebSocket服务器端口
WebSocketsServer webSocket = WebSocketsServer(81);

void setup() {
  Serial.begin(115200);
  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.print("Connected to ");
  Serial.println(ssid);
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());

  // 启动WebSocket服务器
  webSocket.begin();
  webSocket.on("message", handleWebSocketMessage);
  Serial.println("WebSockets started");
}

void loop() {
  webSocket.loop();
}

void handleWebSocketMessage(uint8_t num, WStype_t type, uint8_t *payload, size_t length) {
  if (type == WStype_TEXT) {
    Serial.printf("Received message: %s\n", payload);
    webSocket.sendTXT(num, "Message received");
  }
}
相关推荐
YuMiao9 小时前
gstatic连接问题导致Google Gemini / Studio页面乱码或图标缺失问题
服务器·网络协议
Jony_3 天前
高可用移动网络连接
网络协议
chilix3 天前
Linux 跨网段路由转发配置
网络协议
DianSan_ERP5 天前
电商API接口全链路监控:构建坚不可摧的线上运维防线
大数据·运维·网络·人工智能·git·servlet
呉師傅5 天前
火狐浏览器报错配置文件缺失如何解决#操作技巧#
运维·网络·windows·电脑
gihigo19985 天前
基于TCP协议实现视频采集与通信
网络协议·tcp/ip·音视频
2501_946205525 天前
晶圆机器人双臂怎么选型?适配2-12寸晶圆的末端效应器有哪些?
服务器·网络·机器人
linux kernel5 天前
第七部分:高级IO
服务器·网络
数字护盾(和中)5 天前
BAS+ATT&CK:企业主动防御的黄金组合
服务器·网络·数据库
~远在太平洋~5 天前
Debian系统如何删除多余的kernel
linux·网络·debian