Skip to content

Commit

Permalink
Merge pull request thingsboard#1530 from imbeacon/feature/updated-dev…
Browse files Browse the repository at this point in the history
…ice-library-code

Updated code for device library examples due to new ThingsBoard Client SDK version
  • Loading branch information
ashvayka authored Jul 30, 2024
2 parents ab34f38 + 358feb2 commit c0e4b2b
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ To do this you can change the value of variable **DEMO_MODE** to **1**:
#include "soc/soc.h"
#include "soc/rtc_cntl_reg.h"

#define THINGSBOARD_ENABLE_DYNAMIC 1
#define DEMO_MODE {% if page.docsPrefix == "pe/" or page.docsPrefix == "paas/" %}0{% else %}1{% endif %}

#include <ThingsBoard.h>
#include <esp_heap_caps.h>
Expand Down Expand Up @@ -53,6 +53,10 @@ constexpr size_t MAX_MESSAGE_SIZE = 100U * 1024;
// If the Serial output is mangled, ensure to change the monitor speed accordingly to this variable
constexpr uint32_t SERIAL_DEBUG_BAUD = 115200U;

// Maximum amount of attributs we can request or subscribe, has to be set both in the ThingsBoard template list and Attribute_Request_Callback template list
// and should be the same as the amount of variables in the passed array. If it is less not all variables will be requested or subscribed
constexpr size_t MAX_ATTRIBUTES = 2U;

// Definitions for camera pins
#define PWDN_GPIO_NUM -1
#define RESET_GPIO_NUM 15
Expand All @@ -76,7 +80,7 @@ WiFiClient wifiClient;
// Initalize the Mqtt client instance
Arduino_MQTT_Client mqttClient(wifiClient);
// Initialize ThingsBoard instance with the maximum needed buffer size
ThingsBoard tb(mqttClient, MAX_MESSAGE_SIZE);
ThingsBoardSized<Default_Fields_Amount, Default_Subscriptions_Amount, MAX_ATTRIBUTES> tb(mqttClient, MAX_MESSAGE_SIZE);

// Attribute names for attribute request and attribute updates functionality

Expand Down Expand Up @@ -222,44 +226,50 @@ void encode(const uint8_t *data, size_t length) { {% unless page.docsPrefix == "
/// RPC_Data is a JSON variant, that can be queried using operator[]
/// See https://arduinojson.org/v5/api/jsonvariant/subscript/ for more details
/// @param data Data containing the rpc data that was called and its current value
/// @return Response that should be sent to the cloud. Useful for getMethods
RPC_Response processSetLedMode(const RPC_Data &data) {
void processSetLedMode(const JsonVariantConst &data, JsonDocument &response) {
Serial.println("Received the set led state RPC method");

// Process data
int new_mode = data;

Serial.print("Mode to change: ");
Serial.println(new_mode);
StaticJsonDocument<1> response_doc;

if (new_mode != 0 && new_mode != 1) {
return RPC_Response("error", "Unknown mode!");
response_doc["error"] = "Unknown mode!";
response.set(response_doc);
return;
}

ledMode = new_mode;

attributesChanged = true;

// Returning current mode
return RPC_Response("newMode", (int)ledMode);
response_doc["newMode"] = (int)ledMode;
response.set(response_doc);
}

/// @brief Processes function for RPC call "setLedMode"
/// RPC_Data is a JSON variant, that can be queried using operator[]
/// See https://arduinojson.org/v5/api/jsonvariant/subscript/ for more details
/// @param data Data containing the rpc data that was called and its current value
/// @return Response that should be sent to the cloud. Useful for getMethods
RPC_Response processTakePicture(const RPC_Data &data) {
void processTakePicture(const JsonVariantConst &data, JsonDocument &response) {
Serial.println("Received the take picture RPC method");
StaticJsonDocument<1> response_doc;

if (!captureImage()) {
return RPC_Response("error", "Cannot take a picture!");
response_doc["error"] = "Cannot take a picture!";
response.set(response_doc);
return;
}

sendPicture = true;

// Returning current mode
return RPC_Response("size", strlen(imageBuffer));
// Returning picture size
response_doc["size"] = strlen(imageBuffer);
response.set(response_doc);
}

// Optional, keep subscribed shared attributes empty instead,
Expand All @@ -273,7 +283,7 @@ const std::array<RPC_Callback, 2U> callbacks = {
/// @brief Update callback that will be called as soon as one of the provided shared attributes changes value,
/// if none are provided we subscribe to any shared attribute change instead
/// @param data Data containing the shared attributes that were changed and their current value
void processSharedAttributes(const Shared_Attribute_Data &data) {
void processSharedAttributes(const JsonObjectConst &data) {
for (auto it = data.begin(); it != data.end(); ++it) {
if (strcmp(it->key().c_str(), BLINKING_INTERVAL_ATTR) == 0) {
const uint16_t new_interval = it->value().as<uint16_t>();
Expand All @@ -292,7 +302,7 @@ void processSharedAttributes(const Shared_Attribute_Data &data) {
attributesChanged = true;
}

void processClientAttributes(const Shared_Attribute_Data &data) {
void processClientAttributes(const JsonObjectConst &data) {
for (auto it = data.begin(); it != data.end(); ++it) {
if (strcmp(it->key().c_str(), LED_MODE_ATTR) == 0) {
const uint16_t new_mode = it->value().as<uint16_t>();
Expand All @@ -301,9 +311,9 @@ void processClientAttributes(const Shared_Attribute_Data &data) {
}
}

const Shared_Attribute_Callback attributes_callback(&processSharedAttributes, SHARED_ATTRIBUTES_LIST.cbegin(), SHARED_ATTRIBUTES_LIST.cend());
const Attribute_Request_Callback attribute_shared_request_callback(&processSharedAttributes, SHARED_ATTRIBUTES_LIST.cbegin(), SHARED_ATTRIBUTES_LIST.cend());
const Attribute_Request_Callback attribute_client_request_callback(&processClientAttributes, CLIENT_ATTRIBUTES_LIST.cbegin(), CLIENT_ATTRIBUTES_LIST.cend());
const Shared_Attribute_Callback<MAX_ATTRIBUTES> attributes_callback(&processSharedAttributes, SHARED_ATTRIBUTES_LIST.cbegin(), SHARED_ATTRIBUTES_LIST.cend());
const Attribute_Request_Callback<MAX_ATTRIBUTES> attribute_shared_request_callback(&processSharedAttributes, SHARED_ATTRIBUTES_LIST.cbegin(), SHARED_ATTRIBUTES_LIST.cend());
const Attribute_Request_Callback<MAX_ATTRIBUTES> attribute_client_request_callback(&processClientAttributes, CLIENT_ATTRIBUTES_LIST.cbegin(), CLIENT_ATTRIBUTES_LIST.cend());

void setup() {
WRITE_PERI_REG(RTC_CNTL_BROWN_OUT_REG, 0);
Expand Down Expand Up @@ -476,15 +486,16 @@ Don’t forget to replace placeholders with your real WiFi network SSID, passwor
Necessary variables for connection:
| Variable name | Default value | Description |
|-|------------------------------------------------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------|
| WIFI_SSID | **YOUR_WIFI_SSID** | Your WiFi network name. |
| WIFI_PASSWORD | **YOUR_WIFI_PASSWORD** | Your WiFi network password. |
| TOKEN | **YOUR_DEVICE_ACCESS_TOKEN** | Access token from device. Obtaining process described in #connect-device-to-thingsboard |
| THINGSBOARD_SERVER | **{% if page.docsPrefix == "pe/" or page.docsPrefix == "paas/" %}thingsboard.cloud{% else %}demo.thingsboard.io{% endif %}** | Your ThingsBoard host or ip address. |
| THINGSBOARD_PORT | **1883U** | ThingsBoard server MQTT port. Can be default for this guide. |
| MAX_MESSAGE_SIZE | **100U*1024** | Maximal size of MQTT messages. Should be more than picture size + ~1024 or more. |
| SERIAL_DEBUG_BAUD | **1883U** | Baud rate for serial port. Can be default for this guide. |
| Variable name | Default value | Description |
|-|------------------------------------------------------------------------------------------------------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------|
| WIFI_SSID | **YOUR_WIFI_SSID** | Your WiFi network name. |
| WIFI_PASSWORD | **YOUR_WIFI_PASSWORD** | Your WiFi network password. |
| TOKEN | **YOUR_DEVICE_ACCESS_TOKEN** | Access token from device. Obtaining process described in #connect-device-to-thingsboard |
| THINGSBOARD_SERVER | **{% if page.docsPrefix == "pe/" or page.docsPrefix == "paas/" %}thingsboard.cloud{% else %}demo.thingsboard.io{% endif %}** | Your ThingsBoard host or ip address. |
| THINGSBOARD_PORT | **1883U** | ThingsBoard server MQTT port. Can be default for this guide. |
| MAX_MESSAGE_SIZE | **100U*1024** | Maximal size of MQTT messages. Should be more than picture size + ~1024 or more. |
| SERIAL_DEBUG_BAUD | **1883U** | Baud rate for serial port. Can be default for this guide. |
| DEMO_MODE | **1** | If you want to use **demo.thingsboard.io** set this value to **1**. This reduces the resolution, quality and cut the encoded photo to avoid size limit. |
```cpp
...
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,16 @@ constexpr uint32_t MAX_MESSAGE_SIZE = 1024U;
// If the Serial output is mangled, ensure to change the monitor speed accordingly to this variable
constexpr uint32_t SERIAL_DEBUG_BAUD = 115200U;

// Maximum amount of attributs we can request or subscribe, has to be set both in the ThingsBoard template list and Attribute_Request_Callback template list
// and should be the same as the amount of variables in the passed array. If it is less not all variables will be requested or subscribed
constexpr size_t MAX_ATTRIBUTES = 2U;

// Initialize underlying client, used to establish a connection
WiFiClient wifiClient;
// Initalize the Mqtt client instance
Arduino_MQTT_Client mqttClient(wifiClient);
// Initialize ThingsBoard instance with the maximum needed buffer size
ThingsBoard tb(mqttClient, MAX_MESSAGE_SIZE);
ThingsBoardSized<Default_Fields_Amount, Default_Subscriptions_Amount, MAX_ATTRIBUTES> tb(mqttClient, MAX_MESSAGE_SIZE);

// Attribute names for attribute request and attribute updates functionality

Expand Down Expand Up @@ -116,26 +119,29 @@ const bool reconnect() {
/// RPC_Data is a JSON variant, that can be queried using operator[]
/// See https://arduinojson.org/v5/api/jsonvariant/subscript/ for more details
/// @param data Data containing the rpc data that was called and its current value
/// @return Response that should be sent to the cloud. Useful for getMethods
RPC_Response processSetLedMode(const RPC_Data &data) {
void processSetLedMode(const JsonVariantConst &data, JsonDocument &response) {
Serial.println("Received the set led state RPC method");

// Process data
int new_mode = data;

Serial.print("Mode to change: ");
Serial.println(new_mode);
StaticJsonDocument<1> response_doc;

if (new_mode != 0 && new_mode != 1) {
return RPC_Response("error", "Unknown mode!");
response_doc["error"] = "Unknown mode!";
response.set(response_doc);
return;
}

ledMode = new_mode;

attributesChanged = true;

// Returning current mode
return RPC_Response("newMode", (int)ledMode);
response_doc["newMode"] = (int)ledMode;
response.set(response_doc);
}


Expand All @@ -150,7 +156,7 @@ const std::array<RPC_Callback, 1U> callbacks = {
/// @brief Update callback that will be called as soon as one of the provided shared attributes changes value,
/// if none are provided we subscribe to any shared attribute change instead
/// @param data Data containing the shared attributes that were changed and their current value
void processSharedAttributes(const Shared_Attribute_Data &data) {
void processSharedAttributes(const JsonObjectConst &data) {
for (auto it = data.begin(); it != data.end(); ++it) {
if (strcmp(it->key().c_str(), BLINKING_INTERVAL_ATTR) == 0) {
const uint16_t new_interval = it->value().as<uint16_t>();
Expand All @@ -171,7 +177,7 @@ void processSharedAttributes(const Shared_Attribute_Data &data) {
attributesChanged = true;
}

void processClientAttributes(const Shared_Attribute_Data &data) {
void processClientAttributes(const JsonObjectConst &data) {
for (auto it = data.begin(); it != data.end(); ++it) {
if (strcmp(it->key().c_str(), LED_MODE_ATTR) == 0) {
const uint16_t new_mode = it->value().as<uint16_t>();
Expand All @@ -180,12 +186,12 @@ void processClientAttributes(const Shared_Attribute_Data &data) {
}
}

const Shared_Attribute_Callback attributes_callback(&processSharedAttributes, SHARED_ATTRIBUTES_LIST.cbegin(), SHARED_ATTRIBUTES_LIST.cend());
const Attribute_Request_Callback attribute_shared_request_callback(&processSharedAttributes, SHARED_ATTRIBUTES_LIST.cbegin(), SHARED_ATTRIBUTES_LIST.cend());
const Attribute_Request_Callback attribute_client_request_callback(&processClientAttributes, CLIENT_ATTRIBUTES_LIST.cbegin(), CLIENT_ATTRIBUTES_LIST.cend());
const Shared_Attribute_Callback<MAX_ATTRIBUTES> attributes_callback(&processSharedAttributes, SHARED_ATTRIBUTES_LIST.cbegin(), SHARED_ATTRIBUTES_LIST.cend());
const Attribute_Request_Callback<MAX_ATTRIBUTES> attribute_shared_request_callback(&processSharedAttributes, SHARED_ATTRIBUTES_LIST.cbegin(), SHARED_ATTRIBUTES_LIST.cend());
const Attribute_Request_Callback<MAX_ATTRIBUTES> attribute_client_request_callback(&processClientAttributes, CLIENT_ATTRIBUTES_LIST.cbegin(), CLIENT_ATTRIBUTES_LIST.cend());

void setup() {
// Initalize serial connection for debugging
// Initialize serial connection for debugging
Serial.begin(SERIAL_DEBUG_BAUD);
if (LED_BUILTIN != 99) {
pinMode(LED_BUILTIN, OUTPUT);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ To do this, you can use the code below. It contains all required functionality f
#include <Adafruit_SSD1306.h>

#define THINGSBOARD_ENABLE_PSRAM 0
#define THINGSBOARD_ENABLE_DYNAMIC 1

#include <ThingsBoard.h>

Expand Down Expand Up @@ -45,12 +44,16 @@ constexpr uint32_t MAX_MESSAGE_SIZE = 1024U;
// If the Serial output is mangled, ensure to change the monitor speed accordingly to this variable
constexpr uint32_t SERIAL_DEBUG_BAUD = 115200U;

// Maximum amount of attributs we can request or subscribe, has to be set both in the ThingsBoard template list and Attribute_Request_Callback template list
// and should be the same as the amount of variables in the passed array. If it is less not all variables will be requested or subscribed
constexpr size_t MAX_ATTRIBUTES = 3U;

// Initialize underlying client, used to establish a connection
WiFiClient wifiClient;
// Initalize the Mqtt client instance
Arduino_MQTT_Client mqttClient(wifiClient);
// Initialize ThingsBoard instance with the maximum needed buffer size
ThingsBoard tb = ThingsBoard(mqttClient, MAX_MESSAGE_SIZE, false, 1024);
ThingsBoardSized<Default_Fields_Amount, Default_Subscriptions_Amount, MAX_ATTRIBUTES> tb(mqttClient, MAX_MESSAGE_SIZE);

// Attribute names for attribute request and attribute updates functionality

Expand Down Expand Up @@ -127,26 +130,29 @@ const bool reconnect() {
/// RPC_Data is a JSON variant, that can be queried using operator[]
/// See https://arduinojson.org/v5/api/jsonvariant/subscript/ for more details
/// @param data Data containing the rpc data that was called and its current value
/// @return Response that should be sent to the cloud. Useful for getMethods
RPC_Response processSetLedMode(const RPC_Data &data) {
void processSetLedMode(const JsonVariantConst &data, JsonDocument &response) {
Serial.println("Received the set led state RPC method");

// Process data
int new_mode = data;

Serial.print("Mode to change: ");
Serial.println(new_mode);
StaticJsonDocument<1> response_doc;

if (new_mode != 0 && new_mode != 1) {
return RPC_Response("error", "Unknown mode!");
response_doc["error"] = "Unknown mode!";
response.set(response_doc);
return;
}

ledMode = new_mode;

attributesChanged = true;

// Returning current mode
return RPC_Response("newMode", (int)ledMode);
response_doc["newMode"] = (int)ledMode;
response.set(response_doc);
}


Expand All @@ -161,7 +167,7 @@ const std::array<RPC_Callback, 1U> callbacks = {
/// @brief Update callback that will be called as soon as one of the provided shared attributes changes value,
/// if none are provided we subscribe to any shared attribute change instead
/// @param data Data containing the shared attributes that were changed and their current value
void processSharedAttributes(const Shared_Attribute_Data &data) {
void processSharedAttributes(const JsonObjectConst &data) {
for (auto it = data.begin(); it != data.end(); ++it) {
if (strcmp(it->key().c_str(), BLINKING_INTERVAL_ATTR) == 0) {
const uint16_t new_interval = it->value().as<uint16_t>();
Expand All @@ -185,7 +191,7 @@ void processSharedAttributes(const Shared_Attribute_Data &data) {
attributesChanged = true;
}

void processClientAttributes(const Shared_Attribute_Data &data) {
void processClientAttributes(const JsonObjectConst &data) {
for (auto it = data.begin(); it != data.end(); ++it) {
if (strcmp(it->key().c_str(), LED_MODE_ATTR) == 0) {
const uint16_t new_mode = it->value().as<uint16_t>();
Expand All @@ -194,12 +200,12 @@ void processClientAttributes(const Shared_Attribute_Data &data) {
}
}

const Shared_Attribute_Callback attributes_callback(&processSharedAttributes, SHARED_ATTRIBUTES_LIST.cbegin(), SHARED_ATTRIBUTES_LIST.cend());
const Attribute_Request_Callback attribute_shared_request_callback(&processSharedAttributes, SHARED_ATTRIBUTES_LIST.cbegin(), SHARED_ATTRIBUTES_LIST.cend());
const Attribute_Request_Callback attribute_client_request_callback(&processClientAttributes, CLIENT_ATTRIBUTES_LIST.cbegin(), CLIENT_ATTRIBUTES_LIST.cend());
const Shared_Attribute_Callback<MAX_ATTRIBUTES> attributes_callback(&processSharedAttributes, SHARED_ATTRIBUTES_LIST.cbegin(), SHARED_ATTRIBUTES_LIST.cend());
const Attribute_Request_Callback<MAX_ATTRIBUTES> attribute_shared_request_callback(&processSharedAttributes, SHARED_ATTRIBUTES_LIST.cbegin(), SHARED_ATTRIBUTES_LIST.cend());
const Attribute_Request_Callback<MAX_ATTRIBUTES> attribute_client_request_callback(&processClientAttributes, CLIENT_ATTRIBUTES_LIST.cbegin(), CLIENT_ATTRIBUTES_LIST.cend());

void setup() {
// Initalize serial connection for debugging
// Initialize serial connection for debugging
Serial.begin(115200);
pinMode(LED_BUILTIN, OUTPUT);
Wire.begin(5, 4);
Expand Down
Loading

0 comments on commit c0e4b2b

Please sign in to comment.