#include <WiFi.h>
#include <ThingsBoard.h>
#define WIFI_SSID "Wokwi-GUEST"
#define WIFI_PASSWORD ""
#define THINGSBOARD_SERVER "ihub.vloginnovations.com"
#define TOKEN "pfj6qqab44awd3naexui"
// GPIO pins for LEDs
const int led1Pin = 23; // Adjust pin numbers according to your setup
const int led2Pin = 19;
const int led3Pin = 18; // Additional LED
WiFiClient wifiClient;
ThingsBoard tb(wifiClient);
void setup() {
Serial.begin(115200);
pinMode(led1Pin, OUTPUT);
pinMode(led2Pin, OUTPUT);
pinMode(led3Pin, OUTPUT); // Initialize third LED pin
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
Serial.print("Connecting to WiFi");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println(" Connected to WiFi");
reconnectToThingsBoard();
}
void loop() {
if (!tb.connected()) {
reconnectToThingsBoard();
}
tb.loop();
}
void reconnectToThingsBoard() {
while (!tb.connected()) {
Serial.print("Connecting to ThingsBoard node ...");
if (!tb.connect(THINGSBOARD_SERVER, TOKEN, 1883)) {
Serial.println("Failed, retrying in 3 seconds");
delay(3000);
} else {
Serial.println("Connected");
tb.sendKeyValue("setLed1", handleSetLed1);
tb.sendKeyValue("setLed2", handleSetLed2);
tb.sendKeyValue("setLed3", handleSetLed3); // Setup RPC for third LED
}
}
}
void handleSetLed1(const RPC_Data &data) {
Serial.println("Received RPC to set LED 1");
digitalWrite(led1Pin, &data );
//Serial.println(&data ); //"LED 1 set to ON" : "LED 1 set to OFF"
}
void handleSetLed2(const RPC_Data &data) {
Serial.println("Received RPC to set LED 2");
digitalWrite(led2Pin, &data);//.asBool() ? HIGH : LOW
//Serial.println(&data);//.asBool() ? "LED 2 set to ON" : "LED 2 set to OFF"
}
void handleSetLed3(const RPC_Data &data) {
Serial.println("Received RPC to set LED 3");
digitalWrite(led3Pin, &data);//.asBool() ? HIGH : LOW
//Serial.println(&data);//.asBool() ? "LED 3 set to ON" : "LED 3 set to OFF"
}