Client SDK to connect with ThingsBoard IoT Platform from various IoT devices (Arduino, Espressif, etc.)
This library provides access to the ThingsBoard platform over the MQTT or HTTP(S) protocols.
Examples
The SDK comes with a number of example sketches. See Files --> Examples --> ThingsBoard within the Arduino application.
Please review the complete guide for ESP32 Pico Kit GPIO control and DHT22 sensor monitoring available here.
Supported Frameworks
ThingsBoardArduinoSDK does not directly depend on any specific MQTT Client or HTTP Client implementation, instead any implementation of the IMQTT_Client or IHTTP Client can be used. Because there are no further dependencies on Arduino, besides the client that communicates it allows us to use this library with Arduino, when using the Arduino_MQTT_Client or with Espressif IDF when using the Espressif_MQTT_Client.
Example usage for Espressif can be found in the examples/0014-espressif_esp32_send_data folder, all other code portions can be implemented the same way only initialization of the needed dependencies is slightly different. Meaning internal call to ThingsBoard works the same on both Espressif and Arduino.
This is also the case, because the only always used dependency that is remaining, is ArduinoJson, which despite its name does not require any Arduino component. Please Note: you must use v6.x.x of this library as v7.x.x is not yet supported. Please see this issue for details as to why.
Installation
This project can be built with either PlatformIO, ESP IDF Extension or Arduino IDE.
The project can be found in the PlatformIO Registry, ESP Component registry or the Arduino libraries.
A description on how to include the library in you project can be found below for each of the aforementioned possible methods of integrating the project.
PlatformIO
To add an external library, the most important portion is the lib_deps specification, simply add thingsboard/ThingsBoard.
There are multiple ways to define the version that should be fetched, but the most basic is simply getting the last released version, with the aforementioned line, to learn more see Package Specifications.
lib_deps=
thingsboard/ThingsBoard
ESP IDF Extension
To add an external library, what needs to be done differs between versions. If an ESP-IDF version after and including v3.2.0 is used
then the project can simply be added over the Component Manager.
To do that we can simply call idf.py add-dependency <DEPENDENCY>, with the name of the dependency as an argument. Similar to PlatformIO there are a multiple way to define the version that should be fetched, but the method below is the most basic to simply get the last released version, to learn more see Using Component Manager with a Project.
idf.py add-dependency "thingsboard/ThingsBoard"
If an ESP-IDF version prior to v3.2.0 is used then the component has to be added as a git submodule.
Meaning the repository has to first be a git project, if that is not the case already simply install git and call git init in the folder containing your project.
Similar to the other call there are a multiple way to define the version that should be fetched, but the method below is the most basic to simply get the last released version, to learn more see Git Submodule Help page.
git submodule add https://github.com/thingsboard/thingsboard-client-sdk.git components/ThingsBoard
Arduino IDE
To add an external library, we simply have to open Tools -> Manage Libraries and then search for ThingsBoard then press the install button for the wanted version. See how to install library on Arduino IDE for more detailed information and some troubleshooting if the aforementioned method does not work.
Dependencies
Following dependencies are installed automatically or must be installed, too:
Installed automatically:
- ArduinoJSON — needed for dealing with the
JSON payload that is sent to and received by ThingsBoard. Please Note: you must use v6.x.x of this library as v7.x.x is not yet supported. Please see this issue for details as to why.
- MQTT PubSub Client — for interacting with
MQTT, when using the Arduino_MQTT_Client instance as an argument to ThingsBoard. Only installed if this library is used over Arduino IDE or PlatformIO with the Arduino framework.
- Arduino Http Client — for interacting with
HTTP/S when using the Arduino_HTTP_Client instance as an argument to ThingsBoardHttp. Only installed if this library is used over Arduino IDE or PlatformIO with the Arduino framework.
Needs to be installed manually:
- MbedTLS Library — needed to create hashes for the OTA update for non
Espressif boards.
- Arduino Timer - needed to create non-blocking callback timers for non
Espressif boards.
- WiFiEsp Client — needed when using a
Arduino Uno with a ESP8266.
- StreamUtils — needed when sending arbitrary amount of payload even if the buffer size is too small to hold that complete payload is wanted, aforementioned feature is automatically enabled if the library is installed.
Supported ThingsBoard Features
Example implementations for all base features, mentioned above, can be found in the examples folder. See the README.md in each example folder, to see which boards are supported and which functionality the example shows.
Over MQTT:
All possible features are implemented over MQTT over a specific IAPI_Implementation instance:
Over HTTP(S):
The remaining features have to be implemented by hand with the sendGetRequest or sendPostRequest method. See the ThingsBoard Documentation on how these features could be implemented. This is not done directly in the library, because most features require constant polling, whether an event occurred or not, this would cause massive overhead if it is done for all possible features and therefore not recommended.
Troubleshooting
This troubleshooting guide contains common issues that are well known and can occur if the library is used wrongly. Ensure to read this section before creating a new GitHub Issue.
Enabling internal debug messages
If the device is causing problems that are not already described in more detail below, it might be useful to enable internal debug messages, which will allow the library to print more information about sent and received messages as well as internal processes. This is disabled per default to decrease the amount of logs and memory for the log strings on the flash.
// If not set the value is 0 per default, meaning it will only print internal error messages,
// set to 1 to also print debugging messages which might help to discern the exact place where a method fails
#define THINGSBOARD_ENABLE_DEBUG 1
#include <ThingsBoard.h>
Not enough space for JSON serialization
The buffer size for the serialized JSON is fixed to 64 bytes. The SDK will not send data if the size of it is bigger than the configured internal buffer size. Respective logs in the "Serial Monitor" window will indicate the condition:
[TB] Send buffer size (64) to small for the given payloads size (83), increase with setBufferSize accordingly or install the StreamUtils library
If that's the case, the buffer size for serialization should be increased. To do so, setBufferSize() method can be used or the send_buffer_size passed to the constructor can be increased as illustrated below:
// Initialize underlying client, used to establish a connection
WiFiClient espClient;
// Initalize the Mqtt client instance
Arduino_MQTT_Client mqttClient(espClient);
// The SDK setup with 64 bytes for JSON payload and 8 fields for JSON object
// ThingsBoard tb(mqttClient);
// The SDK setup with 128 bytes for JSON payload and 32 fields for JSON object
ThingsBoardSized<32> tb(mqttClient, 128, 128);
void setup() {
// Increase internal buffer size after inital creation.
tb.setBufferSize(128, 128);
}
Alternatively, it is possible to enable the mentioned THINGSBOARD_ENABLE_STREAM_UTILS option, which sends messages that are bigger than the given buffer size with a method that skips the internal buffer, be aware tough this only works for sent messages. The internal buffer size still has to be big enough to receive the biggest possible message received by the client that is sent by the server.
For that the only thing that needs to be done is to install the required StreamUtils library, see the Dependencies section.
Dynamic ThingsBoard usage
All internal methods call attempt to utilize the stack as far as possible and completely minimize heap usage, that is the reason why there are places in the library where template arguments are required. If that memory being on the heap is not an issue, it is possible to remove the need to enter those template arguments altogether. Simply enable the THINGSBOARD_ENABLE_DYNAMIC