Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Connecting as a client to another ESP8266 hosting Async websocket server is failing on long messages. #861

Open
Gabeki17 opened this issue Dec 30, 2023 · 1 comment

Comments

@Gabeki17
Copy link

Gabeki17 commented Dec 30, 2023

I would like to receive some variable every second from one ESP8266.
Shot messages are arriving. but long ones are not. Like the below is cut and the rest is not even shown.

WS][0][handleWebsocket] ------- read massage frame -------
[WS][0][handleWebsocket] fin: 0 rsv1: 0 rsv2: 0 rsv3 0 opCode: 1
[WS][0][handleWebsocket] mask: 0 payloadLen: 1064
[WS][0][handleWebsocket] text: 0,cim,Nappali,ß0,hoerz,24.70,ß0,temp,24.85,ß0,hoerz,24.70,ß0,hum,50.17,ß2,cs_0,24.00,ß0,ido, párás idő várható,ß0,pres,100262.86,ß0,LK,0;1 4 24 24 0 0 1 1 0 0 0 1,ß0,debug_0,56,ß0,debug_1,1198,ß1,LA,true,ß1,LB,true,ß1,LC,true,ß1,LD,true,ß2,cs_1,1,A,ß2,cs_2,4,,ß2,cs_3,0,A,ß0,i1,,ß3,va0,ß3,vn0,Nappali,ß3,vs0,24.85,ß3,vt0,24.00,ß3,vp0,,ß3,vn2,Vendégszoba,ß3,vs2,24.16,ß3,vt2,24.00,ß3,vp2,,ß3,vn6,Háló,ß3,vs6,23.22,ß3,vt6,24.00,ß3,vp6,,ß3,vn7,Gyerekszoba,ß3,vs7,25.13,ß3,vt7,24.00,ß3,vp7,,ß4,ig2,ß4,ia0,1,ß4,ib0,05:30,ß4,ic0,24.00,ß4,id0,0,ß4,ie0,--:--,ß4,ia1,1,ß4,ib1,23:00,ß4,ic1,24.00,ß4,id1,2,ß4,ie1,--:--,ß4,ia2,2,ß4,ib2,08:00,ß4
[WS][0][handleWebsocketWaitFor] size: 2 cWsRXsize: 0
[WS][0][handleWebsocketWaitFor][readCb] size: 2 ok: 1
[WS][0][handleWebsocketWaitFor] size: 4 cWsRXsize: 2
[WS][0][handleWebsocketWaitFor][readCb] size: 4 ok: 1
[WS][0][handleWebsocket] ------- read massage frame -------

The code for the client is as follows:

/*

  • WebSocketClient.ino
  • Created on: 24.05.2015

*/

#include <Arduino.h>

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

#include <WebSocketsClient.h>

#include <Hash.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");
		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(921600);
Serial.begin(115200);

//Serial.setDebugOutput(true);
Serial.setDebugOutput(true);

Serial.println();
Serial.println();
Serial.println();

for(uint8_t t = 4; t > 0; t--) {
	Serial.printf("[SETUP] BOOT WAIT %d...\n", t);
	Serial.flush();
	delay(1000);
}

WiFiMulti.addAP("Balicsa", "326534qwA");

//WiFi.disconnect();
while(WiFiMulti.run() != WL_CONNECTED) {
	delay(100);
}

// server address, port and URL
webSocket.begin("192.168.11.99", 80, "/ws");

// 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();
}

@Gabeki17
Copy link
Author

I will answer may own question as follows:

I did not have a full understanding on the logic of fregmentation.

So the key was this section in the websocketEvent section:

WStype_FRAGMENT_BIN_START, WStype_FRAGMENT, WStype_FRAGMENT_FIN,

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant