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

Arduino server send data to client #2

Closed
JEVBR opened this issue Oct 17, 2018 · 2 comments
Closed

Arduino server send data to client #2

JEVBR opened this issue Oct 17, 2018 · 2 comments
Assignees
Labels

Comments

@JEVBR
Copy link

JEVBR commented Oct 17, 2018

I got the simple-server. ino and the JS chat-client running straight out of the box, no issues, works like a charm.

my Q is, i would like to add a function in the arduino INO where i send sensor data on regular intervals to the client.

something like:

String mydata="long-data-string";
server.send(mydata);

How can i do this the best and easiest way?

@skaarj1989
Copy link
Owner

I added new broadcast function to WebSocketServer class.

Just put something like this in your code:

unsigned long previousTime = 0;

void loop() {
  server.listen();

  unsigned long currentTime = millis();
  if (currentTime - previousTime > 2000) {
    previousTime = currentTime;

    server.broadcast(TEXT, "hello", 5);
  }
}

server will send hello every 2sec to all connected clients.

Alternatively you can add callback to WebSocketServer which would react on message (i.e "getData") and send sensor data. On client side send this message ("getData") every X seconds.

@skaarj1989 skaarj1989 self-assigned this Oct 17, 2018
@JEVBR
Copy link
Author

JEVBR commented Oct 18, 2018

That's great, the "Broadcast" really is what i needed, i had tried already as you said by "getData" but it proved to be too slow since i need to send data from the server each 100ms. Broadcast does the trick.

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

No branches or pull requests

2 participants