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

webSocket.sendTXT #33

Closed
greengiant83 opened this issue Dec 24, 2015 · 18 comments

Comments

@greengiant83
Copy link

commented Dec 24, 2015

Hopefully I am doing something wrong, but it looks like it takes around 400 milliseconds to transmit simple messages.

My code for the ESP8266:

void loop() 
{
  webSocket.loop();
  webSocket.sendTXT("1");
}

The websocket that it is connecting to is a simple NodeJS script that measures time between messages. It shows an average duration between messages of 400 milliseconds. I can send data from a web browser and have it received with no noticeable latency.

It seems like I should be able to send data much faster than this. Can you advise?

Thanks.

@Links2004

This comment has been minimized.

Copy link
Owner

commented Dec 24, 2015

a delay of around ~12ms is what I get on my network.
for the test I send a message from the ESP to the server (node.js) and then echo it back.
https://github.com/Links2004/arduinoWebSockets/blob/master/tests/webSocketServer/index.js

may try to send every loop is overkill for the ESP network stack which leads to an delay,
you can try to send every 15ms.

void loop() {
    static unsigned long l = 0;
    unsigned long t = millis();
    if((t - l) > 15) {
        webSocket.sendTXT("1");
        l = t;
    }
    webSocket.loop();
}
@greengiant83

This comment has been minimized.

Copy link
Author

commented Dec 27, 2015

Thanks for the reply. Using your Node script I was able to improve the latency from ~400 to 200. It looks like the difference is that your code sends a reply message. Mine was not sending anything back. I am not sure why that improves performance, but it does. I tried your code with and without the throttling, and it doesn't to effect the speed at all.

Still I am still getting results that are significantly slower than yours. Do you have the arduino code posted somewhere? Maybe I am doing something else wrong on that front.

Thanks for this library. I imagine it will be very helpful for alot of people.

@Links2004

This comment has been minimized.

Copy link
Owner

commented Dec 27, 2015

for testing i use the example.
wit this line active.
https://github.com/Links2004/arduinoWebSockets/blob/master/examples/WebSocketClient/WebSocketClient.ino#L42
the server sends a "welcome" message this message is send then in ping pong style.
you can add a

Serial.println(millis());

the half of the difference between too prints is the time for one message.
(the message go client --> server --> client)

server:
https://github.com/Links2004/arduinoWebSockets/blob/master/tests/webSocketServer/index.js
if you like you can measure at nodejs level too. result shut be the same.

@greengiant83

This comment has been minimized.

Copy link
Author

commented Dec 27, 2015

I get the same results as before. Still taking around 200ms between messages. The chip is sitting about 2 feet from the router, and other devices can communicate significantly faster. I can can receive data significantly faster on the chip when I run it as the socket server.

Any ideas why there is such a large difference between my results and yours? I am using Adafruit's Huzzah board. Which one are you using?

@Links2004

This comment has been minimized.

Copy link
Owner

commented Dec 27, 2015

i use bare ESP12 with minimal resistor setup.

you can test:

extern "C" {
#include <user_interface.h>
}


void setup() {
..........
 wifi_set_sleep_type(NONE_SLEEP_T);
}

may it helps, on my VNC project it increases the possible data rate.
(my ws tests are without it)

@greengiant83

This comment has been minimized.

Copy link
Author

commented Dec 28, 2015

Sadly, that made no appreciable difference in response time. I have tried it on two different chips. I'll try it on another network and see if there is any difference

@Links2004

This comment has been minimized.

Copy link
Owner

commented Dec 28, 2015

I did the test again with NONE_SLEEP and git version of the ESP core.
results are amazing:

[WSc][11584] get text(20): Connection accepted.
[WSc][11585] get text(20): Connection accepted.
[WSc][11588] get text(20): Connection accepted.
[WSc][11590] get text(20): Connection accepted.
[WSc][11592] get text(20): Connection accepted.
[WSc][11594] get text(20): Connection accepted.
[WSc][11596] get text(20): Connection accepted.
[WSc][11598] get text(20): Connection accepted.
[WSc][11600] get text(20): Connection accepted.
[WSc][11602] get text(20): Connection accepted.

non sleep brings me down to <=1ms for one direction.

    Serial.printf("[WSc][%d] get text(%u): %s\n", millis(), lenght, payload);

may its better for you network with this change:
ff33056

@greengiant83

This comment has been minimized.

Copy link
Author

commented Dec 28, 2015

I just updated to the latest version. Wow. Talk about a difference. Went from 200 ms in between messages down to 1-3 ms.

You kick ass. Thanks for all your work on this.

@greengiant83

This comment has been minimized.

Copy link
Author

commented Dec 28, 2015

It doesn't really matter, but if I just send data from the esp to the server and don't have the server respond with anything the message time goes from 1ms up to 200ms. Do you know what that is about?

@Links2004

This comment has been minimized.

Copy link
Owner

commented Dec 28, 2015

i thinks this are strange network effects, but the question is where they coming from.

@greengiant83

This comment has been minimized.

Copy link
Author

commented Dec 28, 2015

Prior to your last update, it was taking 400 ms to send messages (with no response), and now its 200, so it seems like whatever you changed was in the ball park.

@Links2004

This comment has been minimized.

Copy link
Owner

commented Dec 28, 2015

the new code only sends one TCP package, before it where two.
but why one TCP package needs 200ms, is strange.

@greengiant83

This comment has been minimized.

Copy link
Author

commented Dec 29, 2015

Does your new code apply to the websocketserver class as well? I am trying to get two esp to communicate now. I have the server replying to each message, but it seems to get bogged down now.

@Links2004

This comment has been minimized.

Copy link
Owner

commented Dec 29, 2015

yes it for the server too, all functions in the WebSockets.cpp are shared.
I tested it with the LEDcontrol example, what is the exact problem, server dont answer? or is it slow?

@yuwenyangfeng

This comment has been minimized.

Copy link

commented Mar 6, 2016

Dear Markus,
I wanna use /webSocket.sendTXT();/ API like
char a='x'; webSocket.sendTXT("s%",a);
just like print() use string. but I got string is %s.
how can send string and inster number or variable.
may you can give me some suggestion.

@Links2004

This comment has been minimized.

Copy link
Owner

commented Mar 6, 2016

on ESP you can do:

#include <StreamString.h>


StreamString data;
char a='x';
data.printf("%c",a);
webSocket.sendTXT(data);

on AVR this will not work.

@yuwenyangfeng

This comment has been minimized.

Copy link

commented May 2, 2016

My think is the same as you:

unsigned int i = 0;     //declare one int

void setup() {
  Serial.begin(9600);
}

void loop() {
  String Str;           //declare one string
  char buffer[50];      //declare array
  sprintf(buffer, "the current value is %d", i++);
  Serial.println(buffer);
  Str = buffer;
  Serial.println(Str);
  delay(1000);
}
@zykovkirill

This comment has been minimized.

Copy link

commented Dec 23, 2018

Hello. Im need help. How to send json data on the server. im can send this data format:
webSocket.sendTXT("{"type":"usermsg","name":"d","message":"nu","color":"null"}");
but when i send this data
webSocket.sendTXT("{"type":"usermsg","name":"%c %s",'d',"","message":"nu","color":"null"}"); server dont read this data

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
4 participants
You can’t perform that action at this time.