WebSocketBundle
WebSocketBundle copied to clipboard
Having issue connecting with NodeMCU Arduino.
Hi. I have big headache configuring my NodeMCU Arduino to talk to the web socket.
Here is the log from the NodeMCU :
[WS-Client] connect ws...
[hostByName] request IP for: example.com
[hostByName] Host: example.com IP: xxx.xxx.xxx.xxx
:ref 1
[WS-Client] connected to example.com:1337.
[WS-Client][sendHeader] sending header...
[WS-Client][sendHeader] handshake GET /socket.io/?EIO=3 HTTP/1.1
Host: example.com:1337
Connection: Upgrade
Upgrade: websocket
Sec-WebSocket-Version: 13
Sec-WebSocket-Key: eJv3Fzry1j7wUnm2n6Wzsg==
Sec-WebSocket-Protocol: wamp
Origin: file://
User-Agent: arduino-WebSocket-Client
[write] n: 264 t: 305248
:wr 264 264 0
:wrc 256 264 0
:wrc 8 8 0
[WS-Client][sendHeader] sending header... Done (28569us).
:sent 264
:rn 246
[WS-Client][handleHeader] RX: HTTP/1.1 101 Switching Protocols
[WS-Client][handleHeader] RX: Sec-WebSocket-Protocol: wamp
[WS-Client][handleHeader] RX: Upgrade: websocket
[WS-Client][handleHeader] RX: Connection: Upgrade
[WS-Client][handleHeader] RX: Sec-WebSocket-Accept: ECMiLB74HiYmmnQ2tZYrp1pfN98=
[WS-Client][handleHeader] RX: X-Powered-By: Ratchet/0.4.1
[WS-Client][handleHeader] Header read fin.
[WS-Client][handleHeader] Client settings:
[WS-Client][handleHeader] - cURL: /socket.io/?EIO=3
[WS-Client][handleHeader] - cKey: eJv3Fzry1j7wUnm2n6Wzsg==
[WS-Client][handleHeader] Server header:
[WS-Client][handleHeader] - cCode: 101
[WS-Client][handleHeader] - cIsUpgrade: 1
[WS-Client][handleHeader] - cIsWebsocket: 1
[WS-Client][handleHeader] - cAccept: ECMiLB74HiYmmnQ2tZYrp1pfN98=
[WS-Client][handleHeader] - cProtocol: wamp
[WS-Client][handleHeader] - cExtensions:
[WS-Client][handleHeader] - cVersion: 0
[WS-Client][handleHeader] - cSessionId:
[WS-Client][handleHeader] Websocket connection init done.
[WS][0][headerDone] Header Handling Done (126us).
[WSc] Connected to url: /socket.io/?EIO=3
[WS][0][sendFrame] ------- send message frame -------
[WS][0][sendFrame] fin: 1 opCode: 1 mask: 1 length: 9 headerToPayload: 0
[WS][0][sendFrame] text: Connected
[WS][0][sendFrame] pack to one TCP package...
[write] n: 15 t: 305395
:wr 15 15 0
:wrc 15 15 0
[WS][0][sendFrame] sending Frame Done (5118us).
[WS][0][handleWebsocketWaitFor] size: 2 cWsRXsize: 0
[readCb] n: 2 t: 305408
:rd 2, 246, 188
:rdi 58, 2
[WS][0][handleWebsocketWaitFor][readCb] size: 2 ok: 1
[WS][0][handleWebsocket] ------- read massage frame -------
[WS][0][handleWebsocket] fin: 1 rsv1: 0 rsv2: 0 rsv3 0 opCode: 1
[WS][0][handleWebsocket] mask: 0 payloadLen: 56
[readCb] n: 56 t: 305433
:sent 15
:rch 246, 4
:rcl
:abort
:rd 56, 246, 190
:rdi 56, 56
:c 56, 246, 250
[WS][0][handleWebsocket] text: [0,"4321426985b1f549313793461425674",1,"Ratchet\/0.4.1"]
[WSc] get text: [0,"4321426985b1f549313793461425674",1,"Ratchet\/0.4.1"]
[WS][0][handleWebsocketWaitFor] size: 2 cWsRXsize: 0
[readCb] n: 2 t: 305461
:rd 2, 4, 0
:rdi 4, 2
[WS][0][handleWebsocketWaitFor][readCb] size: 2 ok: 1
[WS][0][handleWebsocket] ------- read massage frame -------
[WS][0][handleWebsocket] fin: 1 rsv1: 0 rsv2: 0 rsv3 0 opCode: 8
[WS][0][handleWebsocket] mask: 0 payloadLen: 2
[readCb] n: 2 t: 305485
:rd 2, 4, 2
:rdi 2, 2
:c0 2, 4
[WS][0][handleWebsocket] get ask for close. Code: 1007
[WS][0][handleWebsocket] clientDisconnect code: 1000
[WS][0][sendFrame] not Connected!?
:ur 1
:del
[WS-Client] client disconnected.
[WSc] Disconnected!
Here is the code I use base on the ArduinoWebSocket lib.
/*
* WebSocketClientSSL.ino
*
* Created on: 10.12.2015
*
* note SSL is only possible with the ESP8266
*
*/
#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>
#include <WebSocketsClient.h>
#include <Hash.h>
ESP8266WiFiMulti WiFiMulti;
WebSocketsClient webSocket;
#define USE_SERIAL Serial
void webSocketEvent(WStype_t type, uint8_t * payload, size_t length) {
switch(type) {
case WStype_DISCONNECTED:
USE_SERIAL.printf("[WSc] Disconnected!\n");
break;
case WStype_CONNECTED:
{
USE_SERIAL.printf("[WSc] Connected to url: %s\n", payload);
// send message to server when Connected
webSocket.sendTXT("Connected");
}
break;
case WStype_TEXT:
USE_SERIAL.printf("[WSc] get text: %s\n", payload);
// send message to server
// webSocket.sendTXT("message here");
break;
case WStype_BIN:
USE_SERIAL.printf("[WSc] get binary length: %u\n", length);
hexdump(payload, length);
// send data to server
// webSocket.sendBIN(payload, length);
break;
}
}
void setup() {
// USE_SERIAL.begin(921600);
USE_SERIAL.begin(115200);
//Serial.setDebugOutput(true);
USE_SERIAL.setDebugOutput(true);
USE_SERIAL.println();
USE_SERIAL.println();
USE_SERIAL.println();
for(uint8_t t = 4; t > 0; t--) {
USE_SERIAL.printf("[SETUP] BOOT WAIT %d...\n", t);
USE_SERIAL.flush();
delay(1000);
}
WiFiMulti.addAP("xxx", "xxx");
//WiFi.disconnect();
while(WiFiMulti.run() != WL_CONNECTED) {
delay(100);
}
//webSocket.beginSocketIOSSL("example.com", 1338);
webSocket.begin("example.com", 1337, "/socket.io/?EIO=3", "wamp");
//webSocket.beginSSL("example.com", 1338);
webSocket.onEvent(webSocketEvent);
}
void loop() {
webSocket.loop();
}
Here is the log on the websocket server. We can see it's connecting, but right away get disconnect.
Jun 12 04:22:00 dyniot www-data: 04:22:00 INFO [websocket] anon-3825485005b1f4a6801e42485948976 connected ["connection_id" => 4372,"session_id" => "3825485005b1f4a6801e42485948976","storage_id" => 4372] []
Jun 12 04:22:00 dyniot www-data: 04:22:00 INFO [websocket] anon-3825485005b1f4a6801e42485948976 disconnected ["connection_id" => 4372,"session_id" => "3825485005b1f4a6801e42485948976","storage_id" => 4372,"username" => "anon-382548500
anything I do wrong?
Note that the script from the demo is connecting without any issue.