Websocket client for Arduino, with fast data send

Overview

Websocket client for Arduino, with fast data send

This is a simple library that implements a Websocket client running on an Arduino.

Rationale

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.

Features

I added the following:

  • Faster data send (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);
  • For method 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)

Tests

The optimized code was tested for:

  • WiFiClient (<WiFi.h> and <WiFi101.h>)
  • Arduino UNO and ZERO
  • WiFi shield (retired) on Arduino UNO and WiFi shield 101 on Arduino ZERO
  • ws as Node.js websocket server

We 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:

  1. https://gist.github.com/u0078867/9df30eb7da64d8f43422faa70b1a9e52

    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. However, when the message rate you want to have is high, then some TCP packets could be lost (data or ACK); in this case, TCP fast-retransmit might not always be triggered, and this might increase the wait time for next packet to arrive. In this case, UDP protocol is strongly suggested.

  2. After point 1, we had to manually disable the mask flag for websocket messages, by replacing this line in src /WebSocketClient.h:

    #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.

MCU compatibility

  • Tested: Arduino UNO, ZERO
  • Not tested: Arduino DUE; howerer, by searching similar C++ repos on GitHub (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.

Notes

See the original code from Branden for additional notes.

Credits

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.

You might also like...
Arduino library for making an IHC in or output module using an Arduino

Introduction This is an Arduino library for making an IHC in or output module using an Arduino. (IHC controller is a home automation controller made b

ArduinoIoTCloud library is the central element of the firmware enabling certain Arduino boards to connect to the Arduino IoT Cloud

ArduinoIoTCloud What? The ArduinoIoTCloud library is the central element of the firmware enabling certain Arduino boards to connect to the Arduino IoT

Arduino, esp32 and esp8266 library for ABB (ex PowerOne) Aurora Inverter, implement a full methods to retrieve data from the Inverter via RS-485
Arduino, esp32 and esp8266 library for ABB (ex PowerOne) Aurora Inverter, implement a full methods to retrieve data from the Inverter via RS-485

ABB Aurora protocol You can refer the complete documentation on my site ABB Aurora PV inverter library for Arduino, esp8266 and esp32 I create this li

A simple C++ API client for the Ark Blockchain.
A simple C++ API client for the Ark Blockchain.

Ark C++ - Client A simple C++ API client for the Ark Blockchain. Lead Maintainer: Simon Downey Documentation You can find installation instructions an

Arduino library for controlling the MCP2515 in order to receive/transmit CAN frames.
Arduino library for controlling the MCP2515 in order to receive/transmit CAN frames.

107-Arduino-MCP2515 Arduino library for controlling the MCP2515 in order to receive/transmit CAN frames. This library is prepared to interface easily

Arduino library for interfacing with any GPS, GLONASS, Galileo or GNSS module and interpreting its NMEA messages.
Arduino library for interfacing with any GPS, GLONASS, Galileo or GNSS module and interpreting its NMEA messages.

107-Arduino-NMEA-Parser Arduino library for interfacing with any GPS, GLONASS, Galileo or GNSS module and interpreting its NMEA messages. This library

Arduino library for providing a convenient C++ interface for accessing UAVCAN.
Arduino library for providing a convenient C++ interface for accessing UAVCAN.

107-Arduino-UAVCAN Arduino library for providing a convenient C++ interface for accessing UAVCAN (v1.0-beta) utilizing libcanard. This library works f

A RESTful environment for Arduino

aREST Overview A simple library that implements a REST API for Arduino & the ESP8266 WiFi chip. It is designed to be universal and currently supports

Arduino web server library.

aWOT Arduino web server library. Documentation 1. Getting started Hello World Basic routing Application generator Serving static files 2. Guide Routin

Comments
  • Add socket.io client

    Add socket.io client

    I needed a websocket client able to communicate with a socket.io server so I modified the handshake to be compatible for socket.io server.

    As I used ISP8266 I also included an ISP8266 example.

    opened by kenkus-futurice 1
  • Make websocket headers case insensitive, better cookie header handling

    Make websocket headers case insensitive, better cookie header handling

    • Per Websocket/HTTP spec, updated to allow case-insensitivity in websocket/HTTP headers
    • Updated "set-cookie" header to allow for multiple cookie values, saving first one as sid
    • Add debug code to print client websocket request headers
    opened by evanbsmith 1
Releases(v1.0.0)
Owner
Davide Monari
IoT and stuff ...
Davide Monari
ESP32-S2 and CC1101S 433Mhz usb stick to record and send car gates/garages data keys and open stuff

HackZeGarage ESP32-S2 and CC1101S 433Mhz usb stick to record and send car gates/garages data keys and open stuff **HackZeGarage @sulfuroid / Dr CADIC

Dr PhilMorph 5 Mar 16, 2022
Minimal bit-bang send serial 38400/115200 baud for 1MHz or 115200/230400 baud for 8 or 16MHz ATtiny clock

Attiny Serial Out Available as Arduino library "ATtinySerialOut" Version 2.0.1 - work in progress Minimal bit-bang send serial 115200 baud for 1/8/16

Armin 35 Jan 4, 2023
Send and receive MIDI messages over Ethernet (rtpMIDI or AppleMIDI)

AppleMIDI (aka rtpMIDI) for Arduino Enables an Arduino with IP/UDP capabilities (Ethernet shield, ESP8266, ESP32, ...) to participate in an AppleMIDI

null 253 Dec 29, 2022
Arduino TopLevel Client for aliyun IoT Platform

运行于 arduino 的 阿里云 IoT 接入 SDK,在底层连接的基础上增加数据回调绑定、发送数据和事件等 api 的封装,免去自己解析数据的痛苦

芋头 119 Dec 13, 2022
Anto client library for ESP8266-Arduino

Anto client library for ESP8266-Arduino ESP8266-AntoIO provides common and easy way to connect your ESP8266 to Anto.io IoT platform service. Stable ve

Anto.io Internet of Things platform 10 Dec 1, 2021
Arduino HTTP Client library

ArduinoHttpClient ArduinoHttpClient is a library to make it easier to interact with web servers from Arduino. Derived from Adrian McEwen's HttpClient

Arduino Libraries 222 Jan 6, 2023
MQTT client for Arduino

ArduinoMqtt About MQTT Client library for Arduino based on the Eclipse Paho project. This library bundles the C/C++ MQTTPacket library of the Eclipse

Oleg Kovalenko 61 Nov 11, 2022
Arduino client for the Serial To TCP Bridge Protocol PC side service

Arduino Serial to TCP Bridge Client Arduino client for the Serial To TCP Bridge Protocol gateway service. Open a TCP connection to a server from the A

Roan Brand 13 Apr 12, 2022
OAuth 1.0 client library for Arduino

Arduino_OAuth Library for Arduino OAuth 1.0 client library for Arduino. This library depends on ArduinoHttpClient and ArduinoBearSSL. License Copyrigh

Arduino Libraries 17 Dec 13, 2022
Arduino Arduino library for the CloudStorage server project. The library provides easy access to server-stored values and operations.

Arduino-CloudStorage Arduino/ESP8266 library that allows you to easly store and retreive data from a remote (cloud) storage in a key/value fashion. Cl

Gil Maimon 7 Jan 30, 2022