Closed MirkoGelsomini closed 1 year ago
Hi @MirkoGelsomini
PROBLEM: Immediate disconnection after connection
That's the way the example is written and you have to read and understand to use
You must modify the example depending on your different use-case.
You have to write the correct and matching WS Client to reconnect in loop
to use with this example.
For example, the WS server is @ 192.168.2.118:8080
WSClient.py
# Run by $ python3.8 WSClient.py
# From websocket-client package. Install by $ pip3 install websocket-client
import websocket
import time
ws = websocket.WebSocket()
while True:
ws.connect("ws://192.168.2.118:8080")
ws.send("Hello, Server")
result = ws.recv()
print(result)
time.sleep(10)
kh@kh-Inspiron-3593:~/Downloads/Nodejs/WSClient_Python$ python3.8 WSClient.py
Echo:
Echo:
Echo:
Echo:
Echo:
Echo:
...
Starting RP2040W_ServerAllFunctionsDemo on RASPBERRY_PI_PICO_W
WebSockets2_Generic v1.13.2
Connecting to SSID: HueNet
SSID: HueNet
Local IP Address: 192.168.2.118
WebSockets Server Running and Ready on RASPBERRY_PI_PICO_W
IP address: 192.168.2.118, Port: 8080
wsServer: ws://192.168.2.95:8080/
[WS] WebsocketsServer::recvHandshakeRequest: key = Upgrade
[WS] WebsocketsServer::recvHandshakeRequest: value = websocket
[WS] WebsocketsServer::recvHandshakeRequest: lowkey = upgrade
[WS] WebsocketsServer::recvHandshakeRequest: lowvalue = websocket
[WS] WebsocketsServer::recvHandshakeRequest: key = Host
[WS] WebsocketsServer::recvHandshakeRequest: value = 192.168.2.118:8080
[WS] WebsocketsServer::recvHandshakeRequest: lowkey = host
[WS] WebsocketsServer::recvHandshakeRequest: lowvalue = 192.168.2.118:8080
[WS] WebsocketsServer::recvHandshakeRequest: key = Origin
[WS] WebsocketsServer::recvHandshakeRequest: value = http://192.168.2.118:8080
[WS] WebsocketsServer::recvHandshakeRequest: lowkey = origin
[WS] WebsocketsServer::recvHandshakeRequest: lowvalue = http://192.168.2.118:8080
[WS] WebsocketsServer::recvHandshakeRequest: key = Sec-WebSocket-Key
[WS] WebsocketsServer::recvHandshakeRequest: value = kPyLjt0Z8UfeSFy0purr3Q==
[WS] WebsocketsServer::recvHandshakeRequest: lowkey = sec-websocket-key
[WS] WebsocketsServer::recvHandshakeRequest: lowvalue = kPyLjt0Z8UfeSFy0purr3Q==
[WS] WebsocketsServer::recvHandshakeRequest: key = Sec-WebSocket-Version
[WS] WebsocketsServer::recvHandshakeRequest: value = 13
[WS] WebsocketsServer::recvHandshakeRequest: lowkey = sec-websocket-version
[WS] WebsocketsServer::recvHandshakeRequest: lowvalue = 13
[WS] WebsocketsServer::recvHandshakeRequest: key = Connection
[WS] WebsocketsServer::recvHandshakeRequest: value = Upgrade
[WS] WebsocketsServer::recvHandshakeRequest: lowkey = connection
[WS] WebsocketsServer::recvHandshakeRequest: lowvalue = upgrade
Got Message:
[WS] WebsocketsServer::recvHandshakeRequest: key = Upgrade
[WS] WebsocketsServer::recvHandshakeRequest: value = websocket
[WS] WebsocketsServer::recvHandshakeRequest: lowkey = upgrade
[WS] WebsocketsServer::recvHandshakeRequest: lowvalue = websocket
[WS] WebsocketsServer::recvHandshakeRequest: key = Host
[WS] WebsocketsServer::recvHandshakeRequest: value = 192.168.2.118:8080
[WS] WebsocketsServer::recvHandshakeRequest: lowkey = host
[WS] WebsocketsServer::recvHandshakeRequest: lowvalue = 192.168.2.118:8080
[WS] WebsocketsServer::recvHandshakeRequest: key = Origin
[WS] WebsocketsServer::recvHandshakeRequest: value = http://192.168.2.118:8080
[WS] WebsocketsServer::recvHandshakeRequest: lowkey = origin
[WS] WebsocketsServer::recvHandshakeRequest: lowvalue = http://192.168.2.118:8080
[WS] WebsocketsServer::recvHandshakeRequest: key = Sec-WebSocket-Key
[WS] WebsocketsServer::recvHandshakeRequest: value = nrATeKoxvo2o9DEoFdiTcg==
[WS] WebsocketsServer::recvHandshakeRequest: lowkey = sec-websocket-key
[WS] WebsocketsServer::recvHandshakeRequest: lowvalue = nrATeKoxvo2o9DEoFdiTcg==
[WS] WebsocketsServer::recvHandshakeRequest: key = Sec-WebSocket-Version
[WS] WebsocketsServer::recvHandshakeRequest: value = 13
[WS] WebsocketsServer::recvHandshakeRequest: lowkey = sec-websocket-version
[WS] WebsocketsServer::recvHandshakeRequest: lowvalue = 13
[WS] WebsocketsServer::recvHandshakeRequest: key = Connection
[WS] WebsocketsServer::recvHandshakeRequest: value = Upgrade
[WS] WebsocketsServer::recvHandshakeRequest: lowkey = connection
[WS] WebsocketsServer::recvHandshakeRequest: lowvalue = upgrade
Got Message:
...
FYI, I just add WSClient_Python example to be used with the WS Server examples
Bug
Starting from "RP2040_ServerAllFunctionsDemo.ino" from File > Examples > WebSockets2_Generic > Generic > WiFiNINA > RP2040 . After some fixes (explained below) I can connect but I cannot maintain the communication open.
Steps to Reproduce
Expected behavior
Socket connection opened and communication happening (not interested in RGB to really work but the communication to being established).
Actual behavior
PROBLEM: Javascript error: "Uncaught ReferenceError: wsServer is not defined" .
SOLUTION: Solved by replacing in line 106 this:
var connection = new WebSocket(wsServer, ['arduino']);
with:var connection = new WebSocket('ws://'+window.location.hostname+':8080/', ['arduino']);
Compiled and uploaded new code.RESULT: problem 1 solved, but I receive "WebSocket Error"
PROBLEM: WebSocket Error
SOLUTION: Solved by developing my own script available below:
RESULT: I can connect but I quickly disconnect.
PROBLEM: Immediate disconnection after connection
SOLUTION: I removed line 140 and line 143 from "RP2040_ServerAllFunctionsDemo.ino " Compiled and uploaded new code.
RESULT: same error. Immediate disconnection after connection.
Information
Please ensure to specify the following, or your post will be ignored and deleted:
RP2040
Core Version: Arduino-mbed OS Nano 3.5.4RP2040
Board type: NANO_RP2040_CONNECT