Links2004 / arduinoWebSockets

arduinoWebSockets
GNU Lesser General Public License v2.1
1.88k stars 556 forks source link

websocket client for esp8266 keep disconnecting #680

Open zhongwujie opened 3 years ago

zhongwujie commented 3 years ago

I tried many times to connect to the websocket server but failed。I used wireShark to capture packets but did not find the corresponding ip data packet。 The source code is as follows:

/*
 * websocket_test.ino
 *
 *  Created on: 08.06.2021
 *
 */

#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>
#include <WebSocketsClient.h>

ESP8266WiFiMulti WiFiMulti;
WebSocketsClient webSocket;

void webSocketEvent(WStype_t type, uint8_t * payload, size_t length) {

  switch(type) {
    case WStype_DISCONNECTED:
      Serial.printf("[WSc] Disconnected!\n");
      webSocket.sendTXT("DisConnected");
      break;
    case WStype_CONNECTED: {
      Serial.printf("[WSc] Connected to url: %s\n", payload);

      // send message to server when Connected
      webSocket.sendTXT("Connected");
    }
      break;
    case WStype_TEXT:
      Serial.printf("[WSc] get text: %s\n", payload);

      // send message to server
      // webSocket.sendTXT("message here");
      break;
    case WStype_BIN:
      Serial.printf("[WSc] get binary length: %u\n", length);
      hexdump(payload, length);

      // send data to server
      // webSocket.sendBIN(payload, length);
      break;
    case WStype_PING:
      // pong will be send automatically
      Serial.printf("[WSc] get ping\n");
      break;
    case WStype_PONG:
      // answer to a ping we send
      Serial.printf("[WSc] get pong\n");
      break;
    }

}

void setup() {
  Serial.begin(115200);
  WiFi.begin("vivo X21", "12345678");
  Serial.println();
  Serial.print("Connecting");
  while (WiFi.status() != WL_CONNECTED)
  {
    delay(500);
    Serial.print(".");
  }
  Serial.println();

  Serial.print("Connected, IP address: ");
  Serial.println(WiFi.localIP());

  // server address, port and URL
  webSocket.begin("192.168.1.104", 5000);

  // event handler
  webSocket.onEvent(webSocketEvent);

  // use HTTP Basic Authorization this is optional remove if not needed
  //  webSocket.setAuthorization("user", "Password");

  // try ever 5000 again if connection has failed
  webSocket.setReconnectInterval(5000);

  // start heartbeat (optional)
  // ping server every 15000 ms
  // expect pong from server within 3000 ms
  // consider connection disconnected if pong is not received 2 times
  webSocket.enableHeartbeat(15000, 3000, 2);

}

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

The information on Serial is as follows:

Connecting.....scandone state: 0 -> 2 (b0) .state: 2 -> 3 (0) state: 3 -> 5 (10) add 0 aid 1 cnt

connected with vivo X21, channel 1 dhcp client start... ....ip:192.168.1.107,mask:255.255.255.0,gw:192.168.1.1 . Connected, IP address: 192.168.1.107 [WSc] Disconnected! [WSc] Disconnected! [WSc] Disconnected! [WSc] Disconnected! [WSc] Disconnected! [WSc] Disconnected! [WSc] Disconnected! [WSc] Disconnected! [WSc] Disconnected! [WSc] Disconnected! [WSc] Disconnected! [WSc] Disconnected! [WSc] Disconnected! [WSc] Disconnected! [WSc] Disconnected! [WSc] Disconnected! [WSc] Disconnected! [WSc] Disconnected! [WSc] Disconnected! [WSc] Disconnected! [WSc] Disconnected! [WSc] Disconnected! [WSc] Disconnected! [WSc] Disconnected! [WSc] Disconnected! [WSc] Disconnected!

Links2004 commented 3 years ago

please enable debug output.

in the arduino IDE its a setting in the menu where you configure your board. https://arduino-esp8266.readthedocs.io/en/latest/Troubleshooting/debugging.html

for more advanced IDEs you need to set a compile define options. for example platform IO:

build_flags = -D DEBUG_ESP_PORT=Serial
zhongwujie commented 3 years ago

the debug information is as follows. I still don't know why it failed and why wireShark can't capture it's packets. 1${CY0E3CADK ~X_QHDZJ8

Links2004 commented 3 years ago

the log shows that there is a connection problem on the TCP level. the ESP can not connect to 192.168.1.104 on port 5000.

the ESP is connected to 192.168.43.183/24 which is not the same layer 2 then 192.168.1.104. so most likely a routing or firewall problem in the network.

zhongwujie commented 3 years ago

I don't think so. 192.168.43.183 is the IP of esp8266 given by DHCP

Links2004 commented 3 years ago

based on the logs the ESP tries to connect 192.168.1.104:5000 which is not the same layer 2 network segment then 192.168.43.183/24.

in this case the gateway (192.168.43.250) needs to have routes to 192.168.1.104 and the firewall needs to allow the connection.

Address:   192.168.43.183        11000000.10101000.00101011 .10110111
Netmask:   255.255.255.0 = 24    11111111.11111111.11111111 .00000000
Wildcard:  0.0.0.255             00000000.00000000.00000000 .11111111
=>
Network:   192.168.43.0/24       11000000.10101000.00101011 .00000000 (Class C)
Broadcast: 192.168.43.255        11000000.10101000.00101011 .11111111
HostMin:   192.168.43.1          11000000.10101000.00101011 .00000001
HostMax:   192.168.43.254        11000000.10101000.00101011 .11111110
Hosts/Net: 254                   (Private Internet)
zhongwujie commented 3 years ago

Thank you , I understand. We have also tested under the same subnet but also failed and I'll submit the debug log tomorrow. But I still have a question that why Wireshark can't capture the packets from 192.168.43.183? I think esp8266 will send the packet even if it can't reach the destination.

Links2004 commented 3 years ago

do you run Wireshark on the gateway (192.168.43.250) ? or on 192.168.1.104?

zhongwujie commented 3 years ago

I make sure that the esp8266 and the server are under the same subnet, but it still failed.

(O AV6B43W%NNCW5ZV{DL87

zhongwujie commented 3 years ago

We have changed the code of the server. Now the server can receive the data but the esp8266 log shows that the connection will be disconnected. Can you please help to see what is the problem?

F1}CP _4MT)~)EB57)JM15V esp8266_log.txt

Links2004 commented 3 years ago

ok the log now shows that the TCP connection is established and the first HTTP header is send from the ESP to the server. but the server kills the TCP connection after getting the header, from the ESP log alone its impossible to say why. only the server log can tell.

the most common problems are:

[WS-Client] connected to 192.168.1.106:5000.
[WS-Client][sendHeader] sending header...
[WS-Client][sendHeader] handshake GET / HTTP/1.1
Host: 192.168.1.106:5000
Connection: Upgrade
Upgrade: websocket
Sec-WebSocket-Version: 13
Sec-WebSocket-Key: 7g5iBhC5kxXlYQGnjDS93A==
Sec-WebSocket-Protocol: arduino
Origin: file://
User-Agent: arduino-WebSocket-Client

[write] n: 245 t: 1217930
:wr 245 0
:wrc 245 245 0
[WS-Client][sendHeader] sending header... Done (30236us).
:ack 245
:rn 17
:rch 17, 168
:rcl pb=0x3fff1c64 sz=185
[WS-Client] connection lost.
ddeepak8894 commented 9 months ago

@Links2004 i am also facing this issue [WS-Client] connected to 3.111.108.14:8878. [WS-Client][sendHeader] sending header... [String] 'GET / HTTP ... cket-Key: ': Reallocating large String(128 -> 154 bytes) [String] 'GET / HTTP ... bDN1mA== ': Reallocating large String(154 -> 178 bytes) [String] 'GET / HTTP ... arduino ': Reallocating large String(187 -> 204 bytes) [String] 'GET / HTTP ... file:// ': Reallocating large String(204 -> 242 bytes) [WS-Client][sendHeader] handshake GET / HTTP/1.1 Host: 3.111.108.14:8878 Connection: Upgrade Upgrade: websocket Sec-WebSocket-Version: 13 Sec-WebSocket-Key: 7UbTtB1Prh+C0heMbDN1mA== Sec-WebSocket-Protocol: arduino Origin: file:// User-Agent: arduino-WebSocket-Client

[write] n: 244 t: 134722 :wr 244 0 :wrc 244 244 0 [WS-Client][sendHeader] sending header... Done (58142us). :ack 244 :rn 28 :rcl pb=0x3fff276c sz=28 [WS-Client] connection lost. :ur 1 :dsrcv 28 :close