Links2004 / arduinoWebSockets

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

Client Connection to espasyncwebserver fails #643

Closed ChangeD20 closed 3 years ago

ChangeD20 commented 3 years ago

Need help to set up a websocket client connection from an Esp8266 to an esp32 espasyncwebserver.

library link: https://github.com/me-no-dev/ESPAsyncWebServer

server set up like this on the esp32:

  AsyncWebServer server(80);
  AsyncWebSocket ws("/ws"); // access at ws://192.168.4.1/ws

how to fill this function from the "WebSocketClient.ino" to establish the connection from the esp8266 correctly?

// server address, port and URL

webSocket.begin("192.168.0.123", 81, "/");

i´ve tried every combination I could imagine, like:

   webSocket.begin("192.168.4.1", 80, "/");
   webSocket.begin("192.168.4.1", 80, "/ws");
   webSocket.begin("ws://192.168.4.1/", 80, "/ws");
   webSocket.begin("ws://192.168.4.1", 80, "/ws");
   webSocket.begin("ws://192.168.4.1/ws", 80, "/");

no success up to now

could you please help out

Links2004 commented 3 years ago

I never used the ESPAsyncWebServer so not sure. but based on what you have shown this looks correct to me:

  webSocket.begin("192.168.4.1", 80, "/ws");

if you enable the debug serial output for the lib we will get much more info and details.

for the Arduino IDE: 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
ChangeD20 commented 3 years ago

Thanks for helping!

Tryed it again with

webSocket.begin("192.168.4.1", 80, "/ws");

no success.

First Time using debug, so I will tell you in short how I did it, just to be sure not making a mistake at this point.

I`ve set Debug Port to "Serial" in Arduino IDE

Set Debug Level to "Core+ Wifi+ and so on...."

Changed

#define USE_SERIAL Serial1 

to

  #define USE_SERIAL Serial

in the example sketch

and then I got this in the Serial Monitor:

17:16:10.113 -> [SETUP] BOOT WAIT 4... 17:16:10.113 -> wifi evt: 2 17:16:10.204 -> scandone 17:16:10.204 -> state: 0 -> 2 (b0) 17:16:10.204 -> state: 2 -> 3 (0) 17:16:10.204 -> state: 3 -> 5 (10) 17:16:10.204 -> add 0 17:16:10.204 -> aid 1 17:16:10.204 -> cnt 17:16:10.204 -> 17:16:10.204 -> connected with ShotClock, channel 1 17:16:10.204 -> dhcp client start... 17:16:10.204 -> wifi evt: 0 17:16:10.252 -> ip:192.168.4.2,mask:255.255.255.0,gw:192.168.4.1 17:16:10.252 -> wifi evt: 3 17:16:11.120 -> [SETUP] BOOT WAIT 3... 17:16:12.097 -> [SETUP] BOOT WAIT 2... 17:16:13.120 -> [SETUP] BOOT WAIT 1... 17:16:14.096 -> [WIFI][APlistAdd] add SSID: ShotClock 17:16:14.096 -> Wifi connected 17:16:14.096 -> [WS-Client] Websocket Version: 2.3.6 17:16:14.096 -> [WS-Client] connect ws... 17:16:14.096 -> [hostByName] Host: 192.168.4.1 is a IP! 17:16:14.096 -> :ref 1 17:16:18.842 -> wifi evt: 7 17:16:19.120 -> wifi evt: 7 17:16:19.260 -> wifi evt: 7 17:16:19.260 -> :ctmo 17:16:19.260 -> :abort 17:16:19.260 -> :ur 1 17:16:19.260 -> :del 17:16:19.260 -> [WS-Client] connection to 192.168.4.1:80 Failed 17:16:19.260 -> [WS-Client] client disconnected. 17:16:19.260 -> [WSc] Disconnected! 17:16:22.284 -> [WS-Client] connect ws... 17:16:22.284 -> [hostByName] Host: 192.168.4.1 is a IP! 17:16:22.284 -> :ref 1

Links2004 commented 3 years ago

yes, you activated the debug output the to lines:

17:16:19.260 -> [WS-Client] connection to 192.168.4.1:80 Failed
17:16:19.260 -> [WS-Client] client disconnected.

show that the ESP can not connect to 192.168.4.1:80 on the TCP layer, which shows that the problem most likely is in the network setup you have. are the ESP32 and ESP8266 on the same WiFi? is there a firewall?

ChangeD20 commented 3 years ago

the Esp32 just sets up a Wifi Access Point and the esp8266 should connect to it. (with this function?)

WiFiMulti.addAP("ShotClock", "12345678");

i think

17:16:10.252 -> ip:192.168.4.2,mask:255.255.255.0,gw:192.168.4.1

indicates that the wifi connection could be established?

ChangeD20 commented 3 years ago

Further Information:

The Webserver hosts a webpage where javascript is used to communicated via websocket. this is working very good.

in the Javascrpit I use this

  var gateway = `ws://${window.location.hostname}/ws`;

When I add this

  alert(" Host " + window.location.hostname);

this is shown in the browser:

  Host 192.168.4.1

may help?

Links2004 commented 3 years ago

yes

17:16:10.252 -> ip:192.168.4.2,mask:255.255.255.0,gw:192.168.4.1

shows that the ESP connected a AP and gets a IP. and 192.168.4.1 looks correct to since its match the GW IP which makes sense when the ESP32 is the AP. not sure where the problem is based on the Infos so far.

ChangeD20 commented 3 years ago

Okay, thank you for your help up to now. I´ve learned a lot ;-)

ChangeD20 commented 3 years ago

I do not really understand why, but now its working as it should.

I´ve tried lots of libs and variations, one was just a wifi client example. There the wife mode had been changed:

 WiFi.mode(WIFI_STA);

the Esp hab been an access point before and maybe this could lead to the strange behavior until the mode was changed ones.

Thanks a lot for your help and great work !!