Description
- Generic
- Rule Engine(?)
Description
Hey guys,
i'm currently working on a server-side rpc application. I basically want to turn on an analog LED via TB.
The later works fine (see picture), but when I also want to mirror the analog led on my dashboard, like in this tutorial. Therefore I created a server-side attribute called 'ledStatus' and linked an led indicator to its changes. To send the status i use this TB-function for example:
client.sendAttributeBool("ledStatus", ledStatus);
The problem is that TB doesn't seemt to receive the change of the ledStatus. Am I missing something? Do i maybe have to make a rule chain to process information coming from the device?
I hope my problem is kind of understandable. Suggestions are always appreciated :)
Greetings
Reno
Here is the current code. I use the Arduino IDE for coding:
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <ThingsBoard.h>
// AP-DE
#define home "XXXXX"
#define homepw "XXXXXXXX"
// Auswahl des TB-Access Points:
#define TB_TOKEN "XXXXXX"
#define TB_SERVER "demo.thingsboard.io"
//#define TB_PORT 1883
// Initialization:
WiFiClient wifiClient;
ThingsBoard client(wifiClient);
// Select AP:
const char* ssid = home;
const char* password = homepw;
// Other Variables:
const int led = D2;
bool ledStatus = false;
bool previousState = false;
int status = WL_IDLE_STATUS;
void setup() {
pinMode(LED_BUILTIN, OUTPUT);
pinMode(led, OUTPUT);
digitalWrite(led, LOW);
Serial.begin(9600);
InitWiFi();
client.connect(TB_SERVER, demo_TOKEN); // Definition des MQTT-Server
}
///////////////////// RPC-SETTINGS /////////////////////
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");
ledStatus = true;
return RPC_Response("processSwitchChange", "true");
} else if (_params == "false") {
Serial.println("SWITCH: OFF");
ledStatus = false;
return RPC_Response("processSwitchChange", "false");
}
}
const size_t callbacks_size = 1;
RPC_Callback callbacks[callbacks_size] = {
{ "processSwitchChange", processSwitchChange }
};
bool subscribed = false; //RPC-Subscribe Status
///////////////////////////////////////// //Serial.printf(previousState != ledStatus ? "true" : "false");
void loop() {
// Maintain connection
if (!client.connected()) {
reconnect();
}
checkRPC_Subscription();
// Sends
if (previousState != ledStatus) {
Serial.println();
Serial.println("[SWITCH STATE CHANGED]");
if (ledStatus == true) {
digitalWrite(led, HIGH);
Serial.println("LED: ON");
client.sendAttributeBool("ledStatus", true);
} else {
digitalWrite(led, LOW);
Serial.println("LED: OFF");
client.sendAttributeBool("ledStatus", false);
}
previousState = ledStatus;
}
client.loop();
}
/////////////////////////////// OTHER FUNCTIONS ////////////////////////////////////
void InitWiFi() {
Serial.print("Connecting to Access Point...");
WiFi.begin(ssid, 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());
}
void reconnect() {
while (!client.connected()) {
status = WiFi.status();
if (status != WL_CONNECTED) {
InitWiFi();
}
// Reconnect
Serial.print("Connecting to ThingsBoard Device ...");
if (client.connect("ESP8266 Device", TB_TOKEN, NULL)) {
Serial.println("[DONE]");
} else {
Serial.print("[FAILED] Retrying in 5 seconds]");
delay(5000);
}
}
}
void checkRPC_Subscription() {
if (!subscribed) {
Serial.println("Subscribing for RPC...");
if (!client.RPC_Subscribe(callbacks, callbacks_size)) {
Serial.println("Failed to subscribe for RPC");
return;
}
Serial.println("Subscribe done");
subscribed = true;
}
}
Environment
- OS: Win10,
- ThingsBoard: TB Demo
- Browser: Firefox