Thingsboard , I’m having trouble with two switch. I’m trying to integrate a switch widget into the dashboard so that I can control two led, but two switch in the same function i dont know why.
i noted have done when one switch work fine.
this my code
RPC_Response setLedStatus(const RPC_Data &data);
RPC_Response setLedStatus_2(const RPC_Data &data);
void processTime(const JsonVariantConst& data);
// Define the array of RPC callbacks
const std::array<RPC_Callback, 2U> callbacks = {
RPC_Callback{ "setLedStatus", setLedStatus },
RPC_Callback{ "setLedStatus_2", setLedStatus_2 }
};
if (!tb.connected()) {
connectToThingsBoard();
// Subscribe to RPC callbacks
Serial.println("Subscribing for RPC...");
if (!tb.RPC_Subscribe(callbacks.cbegin(), callbacks.cend())) {
// Serial.println("Failed to subscribe for RPC");
return;
}
}
// Request the current time if not already subscribed
// Request the current time if not already subscribed
if (!subscribed) {
// Serial.println("Requesting RPC...");
RPC_Request_Callback timeRequestCallback("getCurrentTime", processTime);
if (!tb.RPC_Request(timeRequestCallback)) {
// Serial.println("Failed to request for RPC");
return;
}
//Serial.println("Request done");
//subscribed = true;
}
tb.loop();
RPC_Response setLedStatus(const RPC_Data &data) {
// Process the RPC request to change the LED state
int dataInt = data;
ledState = dataInt == 1; // Update the LED state based on the received data
Serial.println(ledState ? "LED ON" : "LED OFF");
return RPC_Response("setLedStatus", dataInt); // Respond with the new status
}
RPC_Response setLedStatus_2(const RPC_Data &data) {
// Process the RPC request to change the LED state
int dataInt = data;
ledState_2 = dataInt == 1; // Update the LED state based on the received data
Serial.println(ledState_2 ? "LED_2 ON" : "LED_2 OFF");
return RPC_Response("setLedStatus_2", dataInt); // Respond with the new status
}
control 2 switch seprately wih led
New contributor
Abdalrhman Ahmed is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.