|
|
5 лет назад | |
|---|---|---|
| .. | ||
| examples | 5 лет назад | |
| src | 5 лет назад | |
| README.md | 5 лет назад | |
| keywords.txt | 5 лет назад | |
| library.properties | 5 лет назад | |
This is a simple library that implements a Websocket client running on an Arduino.
For our IoT prototype project based on Arduino, we needed a reliable data transmission protocol, and we thought of Websocket. We then searched for existing client implementations for Arduino, something that was able to handle any subclass of the Client one provided by Arduino, and that was essential in implementation but complete as well. We found the excellent code here https://github.com/brandenhall/Arduino-Websocket. However, some modifications were needed for our purpose. In particular, we needed max throughput possible, approaching 100 messages/s.
I added the following:
client.sendData(..., true), default behaviour): instead of sending a TCP packet per char, everything is sent
in one shot in a single TCP packet. This makes the implementation much faster. However, take into consideration max string length when using WiFiClient.write() method (around 90 bytes, from users experience when googled). Example: webSocketClient.sendData("my string to send", WS_OPCODE_TEXT, true);
client.getData(), I created a pure C string implementation, to avoid chances of heap fragmentation due to String
class. Example: char msg_in[100]; // should be long enough to hold the longest arriving message
uint8_t opcode_in;
...
webSocketClient.getData(msg_in, &opcode_in)
The optimized code was tested for:
WiFiClient (<WiFi.h> and <WiFi101.h>)ws as Node.js websocket serverWe were able to obtain to reach the target throughput indicated above, with a message length of around 70 bytes (*):
(*) In order to reach that speed, we had to apply the following hack:
We did not want to get the loop() stuck if the TCP message was not sent (via WiFi), and we could afford some data lost randomly; although, we wanted our data to be reliable and in time order on the server side, so we excluded UDP packets.
#define WS_MASK 0x80
with this one:
#define WS_MASK 0x00
This modification disables the message mask, which normally is compulsory. ws tolerates it however.
arduino websocket due in:readme,name,description fork:true), it seems that the conditional inclusion (in src/sha1.cpp) of #include <avr/io.h> and #include <avr/pgmspace.h> needed for ZERO board, would also fix compilation for DUE board. Any good-soul tester is welcome to feedback.See the original code from Branden for additional notes.
This is an optimized version of the client code from the excellent job in https://github.com/brandenhall/Arduino-Websocket. Most of the credit goes to Branden.