ESP8266未连接到WebSocket服务器

发布于 2025-02-03 22:13:21 字数 2030 浏览 6 评论 0原文

我已经创建并部署了Heroku的Websocket。我可以在Postman上使用Websocket,但我无法将其与节点MCU连接起来。我可以将节点MCU连接到WiFi,但不能将其连接到Websocket。 我不确定应用于WebSocketClient对象的begin()方法的端口号。

以下是我的代码与我的Websocket连接:

#include "ESP8266WiFi.h"
#include "WebSocketsClient.h"

// WiFi parameters to be configured
const char* ssid = "<wifi_ssid>"; // Write here your router's username
const char* password = "<password>"; // Write here your router's passward
int LED_STATUS = 0;
const char* host = "<project_name>.herokuapp.com";
const uint16_t port = 3000;
const char* url = "/";

WebSocketsClient webSocketsClient = WebSocketsClient();

void handleMessage(char * message){
  Serial.println("message...");
  Serial.println(message);
}

void webSocketCallback(WStype_t type, uint8_t * payload, size_t length){
  toggleLED();
  switch(type){
    case WStype_DISCONNECTED:
      offLED();
      break;
    case WStype_TEXT:
      onLED();
      handleMessage((char*) payload);
      break;
    case WStype_CONNECTED:
    default:
      onLED();
      break;
  }
}

void toggleLED(){
  LED_STATUS = (LED_STATUS + 1)%2;
  digitalWrite(LED_BUILTIN, LED_STATUS); 
}

void offLED(){
  LED_STATUS = 1;
  digitalWrite(LED_BUILTIN, LED_STATUS); 
}

void onLED(){
  LED_STATUS = 0;
  digitalWrite(LED_BUILTIN, LED_STATUS); 
}

void setup(void)
{ 
  Serial.begin(9600);
  // Connect to WiFi
  WiFi.begin(ssid, password);

  //setting the led pin
  pinMode(LED_BUILTIN, OUTPUT);

  // while wifi not connected yet, print '.'
  // then after it connected, get out of the loop
  while (WiFi.status() != WL_CONNECTED) {
     toggleLED();
     delay(500);
     Serial.print(".");
  }
  //WiFi connected, IP address
  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println(WiFi.localIP());

  offLED();

  //websocket connection
  webSocketsClient.begin(host, port, url);
  webSocketsClient.onEvent(webSocketCallback);

  webSocketsClient.setReconnectInterval(5000);
  
}

void loop() {
  // Nothing

  webSocketsClient.loop();
}

I have created and deployed a websocket to the heroku. I am able to use the websocket on postman but I can't connect it with my node mcu. I could connect the node mcu to the wifi but not with the websocket.
I am not sure about the port number which should be used for begin() method for websocketsclient object.

Below is my code to connect with my websocket:

#include "ESP8266WiFi.h"
#include "WebSocketsClient.h"

// WiFi parameters to be configured
const char* ssid = "<wifi_ssid>"; // Write here your router's username
const char* password = "<password>"; // Write here your router's passward
int LED_STATUS = 0;
const char* host = "<project_name>.herokuapp.com";
const uint16_t port = 3000;
const char* url = "/";

WebSocketsClient webSocketsClient = WebSocketsClient();

void handleMessage(char * message){
  Serial.println("message...");
  Serial.println(message);
}

void webSocketCallback(WStype_t type, uint8_t * payload, size_t length){
  toggleLED();
  switch(type){
    case WStype_DISCONNECTED:
      offLED();
      break;
    case WStype_TEXT:
      onLED();
      handleMessage((char*) payload);
      break;
    case WStype_CONNECTED:
    default:
      onLED();
      break;
  }
}

void toggleLED(){
  LED_STATUS = (LED_STATUS + 1)%2;
  digitalWrite(LED_BUILTIN, LED_STATUS); 
}

void offLED(){
  LED_STATUS = 1;
  digitalWrite(LED_BUILTIN, LED_STATUS); 
}

void onLED(){
  LED_STATUS = 0;
  digitalWrite(LED_BUILTIN, LED_STATUS); 
}

void setup(void)
{ 
  Serial.begin(9600);
  // Connect to WiFi
  WiFi.begin(ssid, password);

  //setting the led pin
  pinMode(LED_BUILTIN, OUTPUT);

  // while wifi not connected yet, print '.'
  // then after it connected, get out of the loop
  while (WiFi.status() != WL_CONNECTED) {
     toggleLED();
     delay(500);
     Serial.print(".");
  }
  //WiFi connected, IP address
  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println(WiFi.localIP());

  offLED();

  //websocket connection
  webSocketsClient.begin(host, port, url);
  webSocketsClient.onEvent(webSocketCallback);

  webSocketsClient.setReconnectInterval(5000);
  
}

void loop() {
  // Nothing

  webSocketsClient.loop();
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

回忆追雨的时光 2025-02-10 22:13:21

解决方案(代码中的错误很少):

  1. 由于我的Websocket支持有安全连接:WSS,因此而不是begin()需要使用beginssl(),
  2. 因为使用了WSS,因此有安全连接的端口号为443

Solutions(few mistakes in the code):

  1. Since my websocket supports secured connection : wss, so instead of begin() need to use beginSSL()
  2. since wss is used, port number for secured connections is 443
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文