// #include <WiFi.h>
// #include <ThingsBoard.h>
// #include <Arduino_MQTT_Client.h>
// #include <ArduinoJson.h>
// #define SERVER "demo.thingsboard.io"
// #define TOKEN "iAARSTrwDfTCeBqqoHlx"
// #define SSID "Wokwi-GUEST"
// #define PASS ""
// constexpr uint16_t MAX_MESSAGE_SIZE = 256U;
// WiFiClient espClient;
// Arduino_MQTT_Client mqttClient(espClient);
// ThingsBoard tb(mqttClient, MAX_MESSAGE_SIZE);
// int relayPin = 32;
// int swtPin = 27;
// int softswitch = LOW;
// int presoftswitch = LOW;
// int preswtstate = LOW;
// void onRpcCall(const JsonVariantConst &data, JsonDocument &doc) {
// Serial.println("RPC call received.");
// if (data.containsKey("softswitch")) {
// softswitch = data["softswitch"].as<int>();
// Serial.printf("Received softswitch value: %d\n", softswitch);
// }
// }
// const std::array<RPC_Callback, 1U> callbacks = {
// RPC_Callback("setSoftSwitchValue", onRpcCall)
// };
// void connectToThingsBoard() {
// if (!tb.connected()) {
// Serial.println("Connecting to ThingsBoard...");
// if (!tb.connect(SERVER, TOKEN)) {
// Serial.println("Failed to connect to ThingsBoard.");
// } else {
// Serial.println("Connected to ThingsBoard.");
// if (!tb.RPC_Subscribe(callbacks.cbegin(), callbacks.cend())) {
// Serial.println("Failed to subscribe to RPC.");
// }
// }
// }
// }
// void setupWiFi() {
// Serial.print("Connecting to ");
// Serial.println(SSID);
// WiFi.mode(WIFI_STA);
// WiFi.begin(SSID, PASS);
// while (WiFi.status() != WL_CONNECTED) {
// delay(100);
// Serial.print(".");
// }
// Serial.println("\nConnected to WiFi.");
// }
// void setup() {
// Serial.begin(115200);
// pinMode(swtPin, INPUT_PULLUP);
// pinMode(relayPin, OUTPUT);
// setupWiFi();
// }
// void loop() {
// if (!tb.connected()) {
// connectToThingsBoard();
// }
// tb.loop();
// // Handle relay control
// bool switchStatus = digitalRead(swtPin);
// if (switchStatus == HIGH && preswtstate == LOW) {
// preswtstate = HIGH;
// presoftswitch = HIGH;
// Serial.println("Switch ON");
// relayMode(true);
// } else if (switchStatus == LOW && preswtstate == HIGH) {
// preswtstate = LOW;
// presoftswitch = LOW;
// Serial.println("Switch OFF");
// relayMode(false);
// } else if (softswitch == HIGH && presoftswitch == LOW) {
// presoftswitch = HIGH;
// Serial.println("Softswitch ON");
// relayMode(true);
// } else if (softswitch == LOW && presoftswitch == HIGH) {
// presoftswitch = LOW;
// Serial.println("Softswitch OFF");
// relayMode(false);
// }
// tb.sendTelemetryData("HI", 1);
// delay(500);
// }
// void relayMode(bool state) {
// digitalWrite(relayPin, state ? HIGH : LOW);
// }
//-----------------------------------------------------------------
// #include <WiFi.h>
// #include <ThingsBoard.h>
// #include <Arduino_MQTT_Client.h>
// #include <ArduinoJson.h>
// #define SERVER "demo.thingsboard.io"
// #define TOKEN "iAARSTrwDfTCeBqqoHlx"
// #define SSID "Wokwi-GUEST"
// #define PASS ""
// constexpr uint16_t MAX_MESSAGE_SIZE = 256U;
// WiFiClient espClient;
// Arduino_MQTT_Client mqttClient(espClient);
// ThingsBoard tb(mqttClient, MAX_MESSAGE_SIZE);
// int relayPin = 32;
// int swtPin = 27;
// int softswitch = LOW;
// int presoftswitch = LOW;
// int preswtstate = LOW;
// void onRpcCall(const JsonVariantConst &data, JsonDocument &doc) {
// Serial.println("RPC call received.");
// if (data.containsKey("softswitch")) {
// softswitch = data["softswitch"].as<int>();
// Serial.printf("Received softswitch value: %d\n", softswitch);
// }
// }
// const std::array<RPC_Callback, 1U> callbacks = {
// RPC_Callback("setSoftSwitchValue", onRpcCall)
// };
// void connectToThingsBoard() {
// if (!tb.connected()) {
// Serial.println("Connecting to ThingsBoard...");
// if (!tb.connect(SERVER, TOKEN)) {
// Serial.println("Failed to connect to ThingsBoard.");
// } else {
// Serial.println("Connected to ThingsBoard.");
// if (!tb.RPC_Subscribe(callbacks.cbegin(), callbacks.cend())) {
// Serial.println("Failed to subscribe to RPC.");
// }
// }
// }
// }
// void setupWiFi() {
// Serial.print("Connecting to ");
// Serial.println(SSID);
// WiFi.mode(WIFI_STA);
// WiFi.begin(SSID, PASS);
// while (WiFi.status() != WL_CONNECTED) {
// delay(100);
// Serial.print(".");
// }
// Serial.println("\nConnected to WiFi.");
// }
// void setup() {
// Serial.begin(115200);
// pinMode(swtPin, INPUT_PULLUP);
// pinMode(relayPin, OUTPUT);
// setupWiFi();
// }
// void loop() {
// if (!tb.connected()) {
// connectToThingsBoard();
// }
// tb.loop();
// // Handle physical switch control
// bool switchStatus = digitalRead(swtPin);
// if (switchStatus == HIGH && preswtstate == LOW) {
// preswtstate = HIGH;
// Serial.println("Physical switch ON");
// relayMode(true);
// } else if (switchStatus == LOW && preswtstate == HIGH) {
// preswtstate = LOW;
// Serial.println("Physical switch OFF");
// relayMode(false);
// }
// // Handle softswitch control
// if (softswitch != presoftswitch) {
// Serial.printf("Softswitch state changed to: %d\n", softswitch);
// relayMode(softswitch == HIGH);
// presoftswitch = softswitch;
// }
// tb.sendTelemetryData("HI", 1);
// delay(500);
// }
// void relayMode(bool state) {
// digitalWrite(relayPin, state ? HIGH : LOW);
// Serial.printf("Relay %s\n", state ? "ON" : "OFF");
// }
// ---------------------------------------------------------------------
// #include <WiFi.h>
// #include <ThingsBoard.h>
// #include <Arduino_MQTT_Client.h>
// #include <ArduinoJson.h>
// #define SERVER "demo.thingsboard.io"
// #define TOKEN "iAARSTrwDfTCeBqqoHlx"
// #define SSID "Wokwi-GUEST"
// #define PASS ""
// constexpr uint16_t MAX_MESSAGE_SIZE = 256U;
// WiFiClient espClient;
// Arduino_MQTT_Client mqttClient(espClient);
// ThingsBoard tb(mqttClient, MAX_MESSAGE_SIZE);
// int relayPin = 32;
// int swtPin = 27;
// int softswitch = LOW;
// int presoftswitch = LOW;
// int preswtstate = LOW;
// void onRpcCall(const JsonVariantConst &data, JsonDocument &doc) {
// Serial.println("RPC call received.");
// if (data.containsKey("softswitch")) {
// softswitch = data["softswitch"].as<int>();
// Serial.printf("Received softswitch value: %d\n", softswitch);
// }
// }
// const std::array<RPC_Callback, 1U> callbacks = {
// RPC_Callback("setSoftSwitchValue", onRpcCall)
// };
// void connectToThingsBoard() {
// if (!tb.connected()) {
// Serial.println("Connecting to ThingsBoard...");
// if (!tb.connect(SERVER, TOKEN)) {
// Serial.println("Failed to connect to ThingsBoard.");
// } else {
// Serial.println("Connected to ThingsBoard.");
// if (!tb.RPC_Subscribe(callbacks.cbegin(), callbacks.cend())) {
// Serial.println("Failed to subscribe to RPC.");
// }
// }
// }
// }
// void setupWiFi() {
// Serial.print("Connecting to ");
// Serial.println(SSID);
// WiFi.mode(WIFI_STA);
// WiFi.begin(SSID, PASS);
// while (WiFi.status() != WL_CONNECTED) {
// delay(100);
// Serial.print(".");
// }
// Serial.println("\nConnected to WiFi.");
// }
// void setup() {
// Serial.begin(115200);
// pinMode(swtPin, INPUT_PULLUP);
// pinMode(relayPin, OUTPUT);
// setupWiFi();
// }
// void loop() {
// if (!tb.connected()) {
// connectToThingsBoard();
// }
// tb.loop();
// // Handle physical switch control
// bool switchStatus = digitalRead(swtPin);
// if (switchStatus == HIGH && preswtstate == LOW) {
// preswtstate = HIGH;
// presoftswitch = HIGH;
// Serial.println("Physical switch ON");
// relayMode(true);
// } else if (switchStatus == LOW && preswtstate == HIGH) {
// preswtstate = LOW;
// presoftswitch = LOW;
// Serial.println("Physical switch OFF");
// relayMode(false);
// }
// // Handle softswitch control
// if (softswitch != presoftswitch) {
// Serial.printf("Softswitch state changed to: %d\n", softswitch);
// relayMode(softswitch == HIGH);
// presoftswitch = softswitch;
// }
// tb.sendTelemetryData("HI", 1);
// delay(500);
// }
// void relayMode(bool state) {
// digitalWrite(relayPin, state ? HIGH : LOW);
// Serial.printf("Relay %s\n", state ? "ON" : "OFF");
// }
// -------------------------------------------------------------------------------
#include <WiFi.h>
#include <ThingsBoard.h>
#include <Arduino_MQTT_Client.h>
#include <ArduinoJson.h>
#define SERVER "demo.thingsboard.io"
#define TOKEN "iAARSTrwDfTCeBqqoHlx"
#define SSID "Wokwi-GUEST"
#define PASS ""
constexpr uint16_t MAX_MESSAGE_SIZE = 256U;
WiFiClient espClient;
Arduino_MQTT_Client mqttClient(espClient);
ThingsBoard tb(mqttClient, MAX_MESSAGE_SIZE);
int relayPin = 32;
int swtPin = 27;
int softswitch = LOW;
int presoftswitch = LOW;
int preswtstate = LOW;
// void onRpcCall(const JsonVariantConst &data, JsonDocument &doc) {
// Serial.println("RPC call received.");
// if (data.containsKey("softswitch")) {
// softswitch = data["softswitch"].as<int>();
// Serial.printf("Received softswitch value: %d\n", softswitch);
// }
// }
void onRpcCall(const JsonVariantConst &data, JsonDocument &doc) {
Serial.println("RPC call received.");
serializeJson(data, Serial);
Serial.println();
if (data.is<bool>()) {
bool newValue = data.as<bool>();
Serial.printf("Received softswitch value: %d\n", newValue);
// Update the softswitch variable
// softswitch = newValue ? HIGH : LOW;
softswitch = newValue;
} else {
Serial.println("Received data is not a Boolean value.");
}
}
const std::array<RPC_Callback, 1U> callbacks = {
RPC_Callback("setSoftSwitchValue", onRpcCall)
};
void connectToThingsBoard() {
if (!tb.connected()) {
Serial.println("Connecting to ThingsBoard...");
if (!tb.connect(SERVER, TOKEN)) {
Serial.println("Failed to connect to ThingsBoard.");
} else {
Serial.println("Connected to ThingsBoard.");
if (!tb.RPC_Subscribe(callbacks.cbegin(), callbacks.cend())) {
Serial.println("Failed to subscribe to RPC.");
}
}
}
}
void setupWiFi() {
Serial.print("Connecting to ");
Serial.println(SSID);
WiFi.mode(WIFI_STA);
WiFi.begin(SSID, PASS);
while (WiFi.status() != WL_CONNECTED) {
delay(100);
Serial.print(".");
}
Serial.println("\nConnected to WiFi.");
}
void setup() {
Serial.begin(115200);
pinMode(swtPin, INPUT_PULLUP);
pinMode(relayPin, OUTPUT);
setupWiFi();
}
void loop() {
if (!tb.connected()) {
connectToThingsBoard();
}
tb.loop();
// Handle relay control
bool switchStatus = digitalRead(swtPin);
if (switchStatus == HIGH && preswtstate == LOW) {
preswtstate = HIGH;
presoftswitch = HIGH;
Serial.println("Switch ON");
relayMode(true);
} else if (switchStatus == LOW && preswtstate == HIGH) {
preswtstate = LOW;
presoftswitch = LOW;
Serial.println("Switch OFF");
relayMode(false);
} else if (softswitch == HIGH && presoftswitch == LOW) {
presoftswitch = HIGH;
Serial.println("Softswitch ON");
relayMode(true);
} else if (softswitch == LOW && presoftswitch == HIGH) {
presoftswitch = LOW;
Serial.println("Softswitch OFF");
relayMode(false);
}
tb.sendTelemetryData("HI", 1);
delay(500);
}
void relayMode(bool state) {
digitalWrite(relayPin, state ? HIGH : LOW);
}