Skip to content

unstable websocket connection when use the software serial #105

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

Closed
mkeyno opened this issue Jul 6, 2016 · 5 comments
Closed

unstable websocket connection when use the software serial #105

mkeyno opened this issue Jul 6, 2016 · 5 comments

Comments

@mkeyno
Copy link

mkeyno commented Jul 6, 2016

Hi @Links2004 Markus , I have no problem with websocket unless try read the software serial data and pass it over the websocket link . its kinda some buffer full and overflow, because after couple of try the connection is sever
following is the loop function

#include <SoftwareSerial.h>
#define RxD 15  
#define TxD 13 
SoftwareSerial SIM(RxD, TxD, false, 256);
WebSocketsServer webSocket = WebSocketsServer(81);
ESP8266WebServer server(80);
void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t lenght) {
 client_num=num;
 switch(type) {
        case WStype_DISCONNECTED:
                                 Serial.printf("[WS:%u Disconnected!]\n", num); 
                 ACCESS=false;
        break;
        case WStype_CONNECTED:   { 
                       IPAddress ip = webSocket.remoteIP(num);
                   ACCESS=true;
                  Serial.print("[WS:]Clinet "); Serial.print(num); Serial.print(" CONNECTED with IP: ");Serial.println(ip);

                 } 
        break;
        case WStype_TEXT:    // this for  webSocket request          
                String text = String((char *) &payload[0]);
                Serial.print("[WS:"); Serial.print(num);Serial.print("]");Serial.println(text);

               SIM.print(text+"\r\n");
        break;


      }

}

String read_sim(void)
{
   String sim_buf="";
  while (SIM.available() > 0) 
                           { 
                           char c = (char)SIM.read();
                            sim_buf+=c;
                           Serial.print(c);
                           }
 return sim_buf;
}

void loop() 
{
 server.handleClient();  
 webSocket.loop(); 
if(SIM.available() > 0) webSocket.sendTXT(client_num,read_sim().c_str()); 
}
@Links2004
Copy link
Owner

you overload the TCP layer of the ESP (too many sendText calls, possible for every byte you get from serial).
I personal have add a buffer for the serial data when I do WS to Serial.

example with advanced buffer handling:
#61 (comment)

@mkeyno
Copy link
Author

mkeyno commented Jul 8, 2016

can you be more specific Markus , how come reading serial overload websocket layer ?it is due to softserial library or hardware serial? when I increase softserial buffer from 256 to 1024 it became more stable , but still have problem when I read big data from serial , you said I must encapsulate serial reading from TCP layer? what would your solution when need read softserial data and pass it to websocket link

@mkeyno
Copy link
Author

mkeyno commented Jul 8, 2016

I recheck your last solution about Serial to WS bridge, you make time constraint and websocket header reservation for given buffer length, am I right?
I might add I reading from SIM module so I should send commend and wait for respond
can you tell me how to enable websocket debug and check duration between each sending ?
is it possible to make time constraint in library or it must added to sketch only

@Links2004
Copy link
Owner

the reading itself in your code is not the problem, but if you call for every byte sendTXT you will send a full tcp packet + websocket header for one byte, this can easily overload the TCP layer.

yes, the buffer will be send when full or when for 50ms no new data is coming in.

Debugging:

  1. Set debug port in IDE Menu
  2. enable debug for Websockets (https://github.com/Links2004/arduinoWebSockets/blob/master/src/WebSockets.h#L30)

@mkeyno
Copy link
Author

mkeyno commented Jul 9, 2016

thanks Markus

@mkeyno mkeyno closed this as completed Jul 9, 2016
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

2 participants