thingsboard-client-sdk: [Question] Problem subscribing to shared attributes
Hey guys,
i want to subscribe my client to shared attributes and RPC methods simultaneously, like here #63. The problem is that i don’t get that far, because of the code not compiling. The error lays in the code section where define the subsricption to shared attribute updates. I followed the example 0006 from the v0.8.0 release. Did i miss something?
Any advice is very appriciated! Thank you in advance.
Greetings Reno
Here is my code:
#include "Pinout.h" // Pinbelegung des NodeMCUs
#include "ESP8266WiFi.h" // "File" > "Preferences..." > "Additional board manager URls:" > http://arduino.esp8266.com/stable/package_esp8266com_index.json
#include "ThingsBoard.h" // Version v0.8.0 by ThingsBoard
// Intenet-Zugangsdaten:
#define WIFI_AP "XXX"
#define WIFI_PASSWORD "XXX"
// ThingsBoard (TB) Zugangsdaten:
#define TB_TOKEN "XXX" // Access Token des TB-Devices (Aktuator)
#define TB_SERVER "demo.thingsboard.io" // Alternativ IPv4 des Servers über den die TB gehostet wird (Bsp.: Docker)
#define TB_PORT 1883
constexpr std::array<const char*, 7U> SUBSCRIBED_SHARED_ATTRIBUTES = {
"fw_checksum",
"fw_checksum_algorithm",
"fw_size",
"fw_tag",
"fw_title",
"fw_version",
"test"
};
// Initialisierung
WiFiClient wifiClient; // Erstellung einer ESP8266WiFi-Instanz des Typs
ThingsBoard client(wifiClient); // Instanz wird zu einem TB-Client (?) Sized<256>
// Weitere Parameter:
int status = WL_IDLE_STATUS;
/* unsigned long lastSend; // Updatetimer */
void setup() {
pinMode(LED_BUILTIN, OUTPUT); // Auf NodeMCU integrierte LED wird angemacht (Betreibszustandanzeige)
Serial.begin(9600); // Initiierung des seriellen Monitors (SM) zum Debugen (Baudrate)
InitWiFi(); // Aufbau der Internetverbindung
client.connect(TB_SERVER, TB_TOKEN, TB_PORT); // Verbindung mit dem MQTT-Broker von TB (Port:)
/* lastSend = 0; // Initialisierung des Updatetimers */
}
void loop() {
// Wiederherstellung der Verbindung zu TB (+ Prüfung der Internetverbindung)
if (!client.connected()) {
reconnect();
}
subscribeRPC(); // Aufbau und Aufrechterhaltung der RPC-Kommunikation mit TB
subscribeSharedAttributes(); // Subscribt Änderungen von Shared Attributes
client.loop();
}
///////////////////// RPC-Kommunikation /////////////////////
RPC_Response processSwitchChange(const RPC_Data& data) {
Serial.println("Received 'processSwitchChange'-method: ");
char params[10];
serializeJson(data, params);
String _params = params;
if (_params == "true") {
Serial.println("SWITCH: ON");
client.sendAttributeBool("ledStatus", true);
return RPC_Response("processSwitchChange", "true");
} else if (_params == "false") {
Serial.println("SWITCH: OFF");
client.sendAttributeBool("ledStatus", false);
return RPC_Response("processSwitchChange", "false");
}
}
const size_t RPCCallbackSize = 1;
RPC_Callback RPCCallbacks[RPCCallbackSize] = {
{ "processSwitchChange", processSwitchChange }
};
bool rpc_subscribed = false; //RPC-Subscribe Status
///////////////////// Shared Attributes /////////////////////
void processSharedAttributeUpdate(const Shared_Attribute_Data &data) {
for (auto it = data.begin(); it != data.end(); ++it) {
Serial.println(it->key().c_str());
// Shared attributes have to be parsed by their type.
Serial.println(it->value().as<const char*>());
}
int jsonSize = JSON_STRING_SIZE(measureJson(data));
char buffer[jsonSize];
serializeJson(data, buffer, jsonSize);
Serial.println(buffer);
}
const Shared_Attribute_Callback SharedAttributesCallback(SUBSCRIBED_SHARED_ATTRIBUTES.cbegin(), SUBSCRIBED_SHARED_ATTRIBUTES.cend(), processSharedAttributeUpdate);
/* const size_t SharedCallbackSize = 1;
Shared_Attribute_Callback SharedAttributesCallback[SharedCallbackSize] = {
processSharedAttributeUpdate
}; */ // TB SDK v0.6
bool att_subscribed = false;
//////////////////// Weitere Funktionen /////////////////////
// Funktion - Aufbau der Internetverbindung
void InitWiFi() {
Serial.print("Connecting to Access Point...");
WiFi.begin(WIFI_AP, WIFI_PASSWORD); // Eingabe der Zugangsdaten
while (WiFi.status() != WL_CONNECTED) {
delay(500); // "." bis Verbindung hergestellt ist
Serial.print(".");
}
Serial.println();
Serial.print("Connected! IP address: ");
Serial.println(WiFi.localIP());
}
// Funktion - Wiederherstellung der Verbindung zu TB
void reconnect() {
while (!client.connected()) {
status = WiFi.status();
if (status != WL_CONNECTED) { // Prüft zunächst die Internetverbindung
InitWiFi();
}
// Wiederherstellung der Verbindung mit dem festgelegten TB-Device
Serial.print("Connecting to ThingsBoard Device ...");
if (client.connect(TB_SERVER, TB_TOKEN, TB_PORT)) {
Serial.println("[DONE]");
} else {
Serial.print("[FAILED] Retrying in 5 seconds]");
delay(5000);
//Serial.print(client.state());
//Serial.println(" : retrying in 5 seconds]");
}
}
}
// Funktion - Aufbau und Aufrechterhaltung der RPC-Kommunikation
void subscribeRPC() {
if (!rpc_subscribed) {
Serial.println("Subscribing for RPC...");
if (!client.RPC_Subscribe(RPCCallbacks, RPCCallbackSize)) {
Serial.println("Failed to subscribe for RPC");
return;
}
Serial.println("RPC-Subscription done");
rpc_subscribed = true;
}
}
// Funktion - Subsribt Änderungen
void subscribeSharedAttributes() {
if (!att_subscribed) {
Serial.println("Subscribing for shared attribute updates...");
if (!client.Shared_Attributes_Subscribe(SharedAttributesCallback)) {
Serial.println("Failed to subscribe for shared attribute updates");
return;
}
Serial.println("Shared Attributes-Subscription done");
att_subscribed = true;
}
}
And this is the error i get:
C:\Users\Renaud Kenfack\Documents\Arduino\Code_Aktuator_TBv080\Code_Aktuator_TBv080.ino:98:162: error: no matching function for call to 'Shared_Attribute_Callback::Shared_Attribute_Callback(std::array<const char*, 7>::const_iterator, std::array<const char*, 7>::const_iterator, void (&)(const Shared_Attribute_Data&))'
98 | }
| ^
In file included from C:\Users\Renaud Kenfack\Documents\Arduino\Code_Aktuator_TBv080\Code_Aktuator_TBv080.ino:3:
c:\Users\Renaud Kenfack\Documents\Arduino\libraries\ThingsBoard\src/ThingsBoard.h:145:12: note: candidate: 'Shared_Attribute_Callback::Shared_Attribute_Callback(Shared_Attribute_Callback::processFn)'
145 | inline Shared_Attribute_Callback(processFn cb)
| ^~~~~~~~~~~~~~~~~~~~~~~~~
c:\Users\Renaud Kenfack\Documents\Arduino\libraries\ThingsBoard\src/ThingsBoard.h:145:12: note: candidate expects 1 argument, 3 provided
c:\Users\Renaud Kenfack\Documents\Arduino\libraries\ThingsBoard\src/ThingsBoard.h:140:12: note: candidate: 'Shared_Attribute_Callback::Shared_Attribute_Callback()'
140 | inline Shared_Attribute_Callback()
| ^~~~~~~~~~~~~~~~~~~~~~~~~
c:\Users\Renaud Kenfack\Documents\Arduino\libraries\ThingsBoard\src/ThingsBoard.h:140:12: note: candidate expects 0 arguments, 3 provided
c:\Users\Renaud Kenfack\Documents\Arduino\libraries\ThingsBoard\src/ThingsBoard.h:131:7: note: candidate: 'constexpr Shared_Attribute_Callback::Shared_Attribute_Callback(const Shared_Attribute_Callback&)'
131 | class Shared_Attribute_Callback {
| ^~~~~~~~~~~~~~~~~~~~~~~~~
c:\Users\Renaud Kenfack\Documents\Arduino\libraries\ThingsBoard\src/ThingsBoard.h:131:7: note: candidate expects 1 argument, 3 provided
c:\Users\Renaud Kenfack\Documents\Arduino\libraries\ThingsBoard\src/ThingsBoard.h:131:7: note: candidate: 'constexpr Shared_Attribute_Callback::Shared_Attribute_Callback(Shared_Attribute_Callback&&)'
c:\Users\Renaud Kenfack\Documents\Arduino\libraries\ThingsBoard\src/ThingsBoard.h:131:7: note: candidate expects 1 argument, 3 provided
C:\Users\Renaud Kenfack\Documents\Arduino\Code_Aktuator_TBv080\Code_Aktuator_TBv080.ino: In function 'void subscribeSharedAttributes()':
C:\Users\Renaud Kenfack\Documents\Arduino\Code_Aktuator_TBv080\Code_Aktuator_TBv080.ino:167:69: error: no matching function for call to 'ThingsBoardSized<>::Shared_Attributes_Subscribe(const Shared_Attribute_Callback&)'
167 |
| ^
In file included from C:\Users\Renaud Kenfack\Documents\Arduino\Code_Aktuator_TBv080\Code_Aktuator_TBv080.ino:3:
c:\Users\Renaud Kenfack\Documents\Arduino\libraries\ThingsBoard\src/ThingsBoard.h:613:10: note: candidate: 'bool ThingsBoardSized<PayloadSize, MaxFieldsAmt, Logger>::Shared_Attributes_Subscribe(const Shared_Attribute_Callback*, size_t) [with unsigned int PayloadSize = 64; unsigned int MaxFieldsAmt = 8; Logger = ThingsBoardDefaultLogger; size_t = unsigned int]'
613 | bool Shared_Attributes_Subscribe(const Shared_Attribute_Callback *callbacks, size_t callbacks_size) {
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~
c:\Users\Renaud Kenfack\Documents\Arduino\libraries\ThingsBoard\src/ThingsBoard.h:613:10: note: candidate expects 2 arguments, 1 provided
exit status 1
Compilation error: no matching function for call to 'Shared_Attribute_Callback::Shared_Attribute_Callback(std::array<const char*, 7>::const_iterator, std::array<const char*, 7>::const_iterator, void (&)(const Shared_Attribute_Data&))'
About this issue
- Original URL
- State: closed
- Created a year ago
- Comments: 36 (27 by maintainers)
Commits related to this issue
- Removed PROGMEM for sent strings for non ESP32 #109 — committed to MathewHDYT/thingsboard-client-sdk by MathewHDYT a year ago
- Remove need for PayLoadSize and fixed #109. — committed to MathewHDYT/thingsboard-client-sdk by MathewHDYT a year ago
Hi @MathewHDYT,
I have done some experiments today and discovered that only moving all of strings gives ability to work stable. (In other cases after try to process the saved string in physical memory it produces runtime exception). Also I discovered that we have an issue with std::string::replace method - it also produces the error, I’m currently working with it.
Unfortunately cannot test your variant of the code, because have no electricity right now.
Unfortunately I cannot say that it is every variable, it was a quick investigation. I also thought to separate strings initialisation depending on board. At least for quick fix.
Okay, that’s good to know.
It wasn’t issue so far, but I will resort in case of a problem 😃
I didn’t changed that part of the code yet, but this will also be corrected
I checked and my Arduino Sketch enviroment was still including the older TB library (0.6.1). Thanks for the hint!
I’ll report back 😃