washo4evr / Socket.io-v1.x-Library

Socket.io Library for Arduino
108 stars 58 forks source link

Problem when using esp8266 as realtime #53

Open tahnaconus opened 6 years ago

tahnaconus commented 6 years ago

Hi, I trying to use your library send and receive data 10ms/times and get some problem: esp8266 lagging like video https://youtu.be/DKr2FgKOwdg. I dont know how to fix it. I need some help, many thanks

useafterfree commented 6 years ago

Looks like you have 2 connections. Can you post arduino code?

tahnaconus commented 6 years ago

ya, i have 2 connections. 1 internet explorer, 1 esp8266. my code:

include

include

include

extern "C" {

include "user_interface.h"

}

float timer0,timer1; int counterDisconnect;

SocketIOClient socket; const char ssid = "Tenda_42D9C0"; const char password = "12345678"; char host[] = "192.168.0.108"; int port = 3000;

extern String RID; extern String Rname; extern String Rcontent;

void setup() { Wire.begin(); Wire.setClockStretchLimit(150000); Serial.begin(115200); Serial.println("ESP8266 Websocket Client"); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println("ket noi thanh cong"); Serial.println(WiFi.localIP()); Serial.setTimeout(100); while (!socket.connect(host, port)) { Serial.println(F("Ket noi den socket server that bai!")); delay(2000); } Serial.println("Join Server thanh cong"); delay(500);
socket.send("arduino_join","0","0");

void loop() { if(socket.connected()){ if(socket.monitor()){ String nana=Rcontent; Serial.println(nana); } } else{ socket.reconnect(host, port); Serial.println("reconnect"); counterDisconnect++; socket.send("arduino_join","0","0"); }

if(millis()-timer0 >10){ timer0=millis(); static int m=0,n=0,b=0; m++;n++;b++; if(m>50000) m=0; if(n>5000) n=0; if(b>500) b=0; String cs=""; cs+=String(m); socket.send("a-e","O",cs);

} }

alessandrocapra commented 6 years ago

I'm not sure, but I have to make also a game controlled through arduino so lagging is no option. I think I have found a nice compromise by calling the monitor() function only every 30s. Another thing I noticed (but might be completely not true) is that having the monitor function and a lot of messages sent to Serial slowed things down.