username
username

Reputation: 613

WebSockets, WebRTC, UDP: how to communicate with esp8266 real-time

I am working on a esp8266 library for using a web enable device (mostly just phones) to control a robot. The user connects to a server running on the esp and the library uses WebSockets to send updates about buttons, joysticks, and/or anything else.

The problem is that the data starts getting delayed and sometimes message get received very late, because it uses TCP (it would work better if those messages just didn't get delivered). Also since the esp has low memory this can causes it to crash.

How would I optimize this to update as fast as possible without receiving old messages?

Would something like WebRTC be better or is there something else more suited for this purpose? WebSockets using UDP seems like a better choice, but apparently browsers cannot send UDP?

What sort of stuff should look into/use?

By the way, if it helps, here is a link to my library.

Thanks for any help.

Upvotes: 3

Views: 1554

Answers (3)

Aaron Franz
Aaron Franz

Reputation: 72

MQTT is a great protocol option when looking for rapid, lightweight messaging. I may be biased as a member of the HiveMQ team, but HiveMQ's cloud offering is a great free demonstration of what can be achieved with MQTT.

MQTT also offers quality of service levels that can be specified to ensure message delivery follows the pattern you are expecting - if for example a message needs to be delivered only once, that is an option.

If you happen to be interested in utilizing MQTT, there is even a getting started guide available specifically for ESP8266 devices.

https://www.hivemq.com/mqtt-cloud-broker/

Best,

Aaron from the HiveMQ Team

Upvotes: 0

lamoboos223
lamoboos223

Reputation: 41

I would suggest to use an MQTT as your communication protocol, try using HiveMQ on the cloud it's very easy and straight forward. The MQTT is a concept where your client connect to a broker and publish or subscribe to write/read messages.

Upvotes: 0

user1390208
user1390208

Reputation: 2096

You need to find the reason why messages come late. Non-stable network or you are sending too many small messages or you are not using the arduinoWebSockets library correctly?

  1. The arduinoWebSockets library offers some async behavior; make sure you aren't misusing it.
  2. You need to pack your messages into a single payload, until the payload reaches MTU size, or x number of milliseconds passed and you must send the data. As opposed to sending 10 tiny messages 100 bytes each, accumulate them into one 1000 bytes payload and send it at once. You should see great throughput improvement.

If all the above does not help, then web browsers are not an option for you, and you will need to create apps for mobiles, which will communicate raw UDP to your server. Browsers cannot do UDP, except WebRTC, but in your case you cannot use WebRTC, because it will simply not run on your low memory esp. WebRTC is a resource hog; not really applicable to DSP.

Upvotes: 3

Related Questions