Git Product home page Git Product logo

thingsboard-arduino-sdk's Introduction

Arduino ThingsBoard SDK

MIT license ESP32 ESP8266 GitHub release GitHub downloads Arduino actions status ESP32 actions status ESP8266 actions status GitHub stars

This library provides access to ThingsBoard platform over the MQTT protocol or alternatively over HTTP/S.

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.

Installation

ThingsBoard SDK can be installed directly from the Arduino Library manager or PlattformIO. Following dependencies are installed automatically or must be installed, too:

Installed automatically:

Needs to be installed manually:

  • MbedTLS Library — needed to create hashes for the OTA update (ESP8266 only, already included in ESP32 base firmware).
  • WiFiEsp Client — needed when using a Arduino Uno in combination with a ESP8266.

Supported ThingsBoard Features

Over MQTT:

All possible features are implemented over MQTT

Over HTTP\S:

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.

Example implementations for all base features, mentioned above, can be found in the examples folder. See the according README.md, to see which boards are supported and which functionality the example shows.

Troubleshooting

No PROGMEM support causing crashes

If the device is crashing with an Exception especially Exception (3), more specifically LoadStoreError or LoadStoreErrorCause this might be because, all constant variables are per default in flash memory to decrease the memory footprint of the library, if the libraries used or the board itself don't support PROGMEM. This can cause crashes to mitigate that simply add a #define THINGSBOARD_ENABLE_PROGMEM 0 before including the ThingsBoard header file.

// If not set otherwise the value is 1 per default if the pgmspace include exists,
// set to 0 if the board has problems with PROGMEM variables and does not seem to work correctly.
#define THINGSBOARD_ENABLE_PROGMEM 0
#include <ThingsBoard.h>

Dynamic ThingsBoard usage

The received JSON payload, as well as the sendAttributes and sendTelemetry methods use the StaticJsonDocument by default, this furthermore requires the MaxFieldsAmt template argument passed in the constructor. To set the size the buffer should have, where bigger messages will cause not sent / received key-value pairs or failed de-/serialization.

To remove the need for the MaxFieldsAmt template argument in the constructor and ensure the size the buffer should have is always enough to hold the sent or received messages, instead #define THINGSBOARD_ENABLE_DYNAMIC 1 can be set before including the ThingsBoard header file. This makes the library use the DynamicJsonDocument instead of the default StaticJsonDocument.

// If not set otherwise the value is 0 per default,
// set to 1 if the MaxFieldsAmt template argument should not be required.
#define THINGSBOARD_ENABLE_DYNAMIC 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 size originally passed in the constructor as a template argument (PayLoadSize). Respective logs in the "Serial Monitor" window will indicate the condition:

[TB] Buffer size (64) to small for the given payloads size (83), increase with setBufferSize accordingly or set THINGSBOARD_ENABLE_STREAM_UTILS to 1 before including ThingsBoard

If that's a case, the buffer size for serialization should be increased. To do so, setBufferSize() method can be used or alternatively the bufferSize passed to the constructor can be increased as illustrated below:

// For the sake of example
WiFiEspClient espClient;

// The SDK setup with 64 bytes for JSON buffer
// ThingsBoard tb(espClient);

// The SDK setup with 128 bytes for JSON buffer
ThingsBoard tb(espClient, 128);

void setup() {
  // Increase internal buffer size after inital creation.
  tb.setBufferSize(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.

// Enable skipping usage of the buffer for sends that are bigger than the internal buffer size
#define THINGSBOARD_ENABLE_STREAM_UTILS 1

// For the sake of example
WiFiEspClient espClient;

// The SDK setup with 64 bytes for JSON buffer
ThingsBoard tb(espClient);

Too much data fields must be serialized

A buffer allocated internally by ArduinoJson library is fixed and is capable for processing not more than 8 fields. If you are trying to send more than that, you will get a respective log showing an error in the "Serial Monitor" window:

[TB] Too many JSON fields passed (26), increase MaxFieldsAmt (8) accordingly

The solution is to use ThingsBoardSized class instead of ThingsBoard. Note that the serialized JSON buffer size must be specified explicitly, as described here. See Dynamic ThingsBoard Usage above if the usage of MaxFieldsAmt, should be replaced with automatic detection of the needed size.

// For the sake of example
WiFiEspClient espClient;

// The SDK setup with 8 fields for JSON object
// ThingsBoard tb(espClient);

// The SDK setup with 128 bytes for JSON payload and 32 fields for JSON object.
ThingsBoardSized<32> tb(espClient, 128);

Tips and Tricks

To use your own logger you have to create a class and pass it as second template parameter Logger to your ThingsBoardSized class instance.

For example:

class CustomLogger {
public:
  static void log(const char *error) {
    Serial.print("[Custom Logger] ");
    Serial.println(error);
  }
};

Your class must have method log with the same prototype as in the example. It will be called, if the library needs to print any log messages.

After that, you can use it in place of regular ThingsBoard class. Note that the serialized JSON buffer size must be specified explicitly, as described here.

// For the sake of example
WiFiEspClient espClient;

// The SDK setup with 8 fields for JSON object
// ThingsBoard tb(espClient);

// The SDK setup with 128 bytes for JSON payload and 32 fields for JSON object.
ThingsBoardSized<32, CustomLogger> tb(espClient, 128);

Have a question or proposal?

You are welcomed in our issues and Q&A forum.

License

This code is released under the MIT License.

thingsboard-arduino-sdk's People

Contributors

mathewhdyt avatar oekosolvemg avatar imbeacon avatar forgge avatar nhuhs1887 avatar ashvayka avatar r0g-dev avatar traceyflanders avatar yuliia-vyl avatar adrienadb avatar ferchinas avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.