#include <WiFi.h>
#include <PubSubClient.h>
#include <ArduinoJson.h>
// WiFi configuration
const char* ssid = "Wokwi-GUEST"; // Change as necessary
const char* password = ""; // Change as necessary
// MQTT configuration
const char* mqttServer = "glittercondor583.cloud.shiftr.io";
const char* mqttUser = "glittercondor583";
const char* mqttPassword = "Tam70fsCGh4qNQW1";
const int mqttPort = 1883;
const char* mqttControlTopic = "led/control"; // Topic to receive control messages
const char* mqttStatusTopic = "wokwi/led/status"; // Topic to publish LED status
// ESP32 pin configuration for LEDs
const int kitchenLedPin = 2;
const int livingRoomLedPin = 4;
const int diningRoomLedPin = 33;
const int toiletLedPin = 14;
WiFiClient espClient;
PubSubClient client(espClient);
// Function to set the status of an LED
void setLedStatus(const char* led, bool isOn) {
int pin;
// Match the LED name with the appropriate pin
if (strcmp(led, "kitchen") == 0) pin = kitchenLedPin;
else if (strcmp(led, "living-room") == 0) pin = livingRoomLedPin;
else if (strcmp(led, "dining-room") == 0) pin = diningRoomLedPin;
else if (strcmp(led, "toilet") == 0) pin = toiletLedPin;
else return; // If no match, exit the function
// Set the LED status
digitalWrite(pin, isOn ? HIGH : LOW);
// Publish the current status to the status topic
StaticJsonDocument<200> doc;
doc["led"] = led;
doc["isOn"] = isOn;
char buffer[128];
serializeJson(doc, buffer);
client.publish(mqttStatusTopic, buffer);
}
// Callback function to handle incoming MQTT messages
void callback(char* topic, byte* payload, unsigned int length) {
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("]: ");
for (int i = 0; i < length; i++) {
Serial.print((char)payload[i]);
}
Serial.println();
// Parse the JSON payload
StaticJsonDocument<200> doc;
DeserializationError error = deserializeJson(doc, payload, length);
if (error) {
Serial.print("JSON deserialization failed: ");
Serial.println(error.c_str());
return;
}
// Extract the LED name and status
const char* led = doc["led"];
bool isOn = doc["isOn"];
setLedStatus(led, isOn);
}
// Function to reconnect to the MQTT broker if disconnected
void reconnect() {
// Loop until connected
while (!client.connected()) {
Serial.print("Connecting to MQTT...");
// Attempt to connect
if (client.connect("ESP32Client", mqttUser, mqttPassword)) {
Serial.println("connected");
client.subscribe(mqttControlTopic); // Subscribe to the control topic
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
delay(5000);
}
}
}
void setup() {
// Initialize LED pins
pinMode(kitchenLedPin, OUTPUT);
pinMode(livingRoomLedPin, OUTPUT);
pinMode(diningRoomLedPin, OUTPUT);
pinMode(toiletLedPin, OUTPUT);
Serial.begin(115200);
// Connect to WiFi
Serial.print("Connecting to WiFi");
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println(" WiFi connected");
// Set up MQTT client
client.setServer(mqttServer, mqttPort);
client.setCallback(callback);
// Connect to MQTT broker
reconnect();
}
void loop() {
// Reconnect if the client is disconnected
if (!client.connected()) {
reconnect();
}
client.loop(); // Maintain MQTT connection
}