Skip to content

ESP32 "connect" and disconnect many times (Solved) #641

Open
@Raul-Altamirano

Description

@Raul-Altamirano

I've looking in all issues, and found many similar issues, but i can't solve this,
I'm using NodeJS
Using WebSocket.begin("***. ***. *** .*", 3000) got an error on my server side,

Captura de Pantalla 2021-03-11 a la(s) 12 49 42

but when I change to WebSocket.beginSocketIO("***. ***. *** .*", 3000, ""/socket.io/?EIO=4"")

Got:

connect and disconnect

in this issue there is the same error I got
#598

I have change from recent version to 2.2.0 because I read in another issue It could fix the problem
#546
and got:
Captura de Pantalla 2021-03-10 a la(s) 23 45 24

So I decided use latest (3.3.5)

set delay to watch behavior of loop()
#592

and i cant find the solution I am using ESP32, also I've took code from another issues to test that is not my server side, and always got the same. It doesn't matter what config in my server side I use, I always got the same error with the code in ESP32.

could you help me how to configure using the code you wrote, I found that this is the best library for Arduino Socket.

this is my code for server NodeJS

const app = require('express');
const http = require('http').createServer(app);
const io = require('socket.io')(http);
io.on('connection', (socket) => {

  console.log('ESP32 conectado');

  socket.on('hi', () => {

    console.log('saludando desde ESP32');

  
  })
  
  socket.on('disconnect', () => {

    console.log('desconectado');

  })

})

http.listen(3000, () => {

  console.log("server launched on port 3000");
})`

ESP32

#include <Arduino.h>

#include <WiFi.h>
#include <WiFiMulti.h>
#include <WiFiClientSecure.h>

#include <WebSocketsClient.h>

#ifdef DEBUG_ESP_PORT
#define DEBUG_MSG(...) DEBUG_ESP_PORT( __VA_ARGS__)
#else 
#define DEBUG_MSG(...)
#endif
#define DEBUG_ESP_PORT Serial


WiFiMulti WiFiMulti;
WebSocketsClient webSocket;

#define USE_SERIAL Serial

void hexdump(const void *mem, uint32_t len, uint8_t cols = 16) {
	const uint8_t* src = (const uint8_t*) mem;
	USE_SERIAL.printf("\n[HEXDUMP] Address: 0x%08X len: 0x%X (%d)", (ptrdiff_t)src, len, len);
	for(uint32_t i = 0; i < len; i++) {
		if(i % cols == 0) {
			USE_SERIAL.printf("\n[0x%0hola8X] 0x%08X: ", (ptrdiff_t)src, i);
		}
		USE_SERIAL.printf("%02X ", *src);
		src++;
	}
	USE_SERIAL.printf("\n");
}

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

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

			// send message to server when Connected
			webSocket.sendTXT("connection");
			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;
		case WStype_ERROR:			
		case WStype_FRAGMENT_TEXT_START:
		case WStype_FRAGMENT_BIN_START:
		case WStype_FRAGMENT:
		case WStype_FRAGMENT_FIN:
			break;
	}

}

void setup() {

	// USE_SERIAL.begin(921600);
	USE_SERIAL.begin(115200);
 delay(10);
      USE_SERIAL.print("hola//////////////////////");

	//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("FERNAND FLOU", "verdugoblack");
  WiFiMulti.addAP("moto", "12345678");
//    WiFiMulti.addAP("INFINITUMD447DF", "sarawast");


	//WiFi.disconnect();
	while(WiFiMulti.run() != WL_CONNECTED) {
        
         Serial.println("reconectando...");

		delay(100);
	}

	// server address, port and URL /socket.io/?EIO=4&transport=pollin
	webSocket.beginSocketIO("192.168.2.106", 3000, "/socket.io/?EIO=4");

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

}

void loop() {
	webSocket.loop();

}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions