socketio服务器未连接到ESP8266

时间:2019-02-21 06:21:37

标签: socket.io arduino esp8266 nodemcu arduino-esp8266

我通过ESP8266连接到SocketIo服务器时遇到问题。 我将ESP连接到Wifi,并且节点服务器在localhost上运行。 我在ESP8266中有以下代码

#include <ESP8266HTTPClient.h>
#include <ESP8266WiFi.h>
#include <SocketIoClient.h>


//192.168.1.37 --My IP Address

SocketIoClient webSocket;

const char* ssid     = "ssid";      // SSID
const char* password = "pass";        // Password
const char* host = "192.168.1.37";        // Server IP (localhost)
const int   port = 8080;                  // Server Port
const char* url = "http://localhost:8080/test";

void event(const char * payload, size_t length) {
  Serial.println("Message");
}

void setup() {
  Serial.begin(115200);
  Serial.print("Connecting to ");
  Serial.println(ssid);

  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);     // 5ms Delay
    Serial.print(".");

  }

  Serial.print("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
  Serial.println("Connecting To Socket");
  webSocket.on("event", event);
  webSocket.begin("192.168.1.37", 8080);

}

void loop() {

  if (WiFi.status() == WL_CONNECTED) { //Check WiFi connection status
     delay(3000);    //Send a request every 3 seconds  
     webSocket.loop();
     Serial.println("Retrying ....");
}

我的服务器端代码也在JavaScript中。

var io = socket(server);
io.on('connection', (socket) => {
    console.log('made socket connection', socket.id);
    socket.on("ESP", function(data){
        socket.emit('Hi ESP, ESP called', data);
        console.log("Socket Working !");
    });
    console.log('made socket connection', socket.id);
    socket.on("connect", function(data){
        socket.emit('Hi ESP connect called', data);
        console.log("Socket Working !");
    });

});

但是我继续在串行监视器中接收此输出。 另外,我的服务器上没有收到任何请求。

11:37:09.729 -> ..........WiFi connected
11:37:14.129 -> IP address: 
11:37:14.129 -> 192.168.1.13
11:37:14.129 -> Connecting To Socket
11:37:17.245 -> Retrying ....
11:37:17.245 -> [SIoC] Disconnected!
11:37:17.245 -> [SIoC] event disconnected not found. 1 events available
11:37:20.230 -> Retrying ....
11:37:20.230 -> [SIoC] Disconnected!
11:37:20.230 -> [SIoC] event disconnected not found. 1 events available

2 个答案:

答案 0 :(得分:0)

我遇到了同样的问题。当我将 socket.io 版本更改为 ^2.3.0 时已修复 将 package.json 中的 socket io 版本更改为 ^2.3.0,删除节点模块并使用 npm install 命令

重新安装包

答案 1 :(得分:0)

我遇到了同样的问题并进行了更改:

WiFi.begin(ssid, password);

WiFiMulti.addAP(ssid, pass);

它连接良好。

PS:我使用了 <SocketIOclient.h>