#include <WiFi.h>
#include <PubSubClient.h>
#include <ArduinoJson.h> // Required for parsing JSON
// Update these with your network credentials
const char* ssid = "your_SSID";
const char* password = "your_PASSWORD";
// Update the MQTT broker IP address and port
const char* mqttServer = "MQTT_BROKER_IP";
const int mqttPort = 1883;
// LED pins
const int ledPin1 = 13;
const int ledPin2 = 12;
// Variables
int P1 = 0;
int P2 = 0;
// Create an instance of the WiFiClient class to establish the MQTT connection
WiFiClient wifiClient;
PubSubClient mqttClient(wifiClient);
// Callback function to handle incoming MQTT messages
void callback(char* topic, byte* payload, unsigned int length) {
// Parse the incoming JSON payload
StaticJsonDocument<200> doc;
DeserializationError error = deserializeJson(doc, payload, length);
// Check for parsing errors
if (error) {
Serial.print("JSON parsing failed: ");
Serial.println(error.c_str());
return;
}
// Retrieve values from the parsed JSON
const char* messageType = doc["type"];
// Handle the received message
if (strcmp(messageType, "set_var") == 0) {
JsonObject payloadObj = doc["payload"].as<JsonObject>();
// Check if L1 value is present
if (payloadObj.containsKey("L1")) {
int l1Value = payloadObj["L1"];
digitalWrite(ledPin1, l1Value);
Serial.print("Received set_var message with L1 value: ");
Serial.println(l1Value);
P1 = l1Value;
}
// Check if L2 value is present
if (payloadObj.containsKey("L2")) {
int l2Value = payloadObj["L2"];
digitalWrite(ledPin2, l2Value);
Serial.print("Received set_var message with L2 value: ");
Serial.println(l2Value);
P2 = l2Value;
}
}
}
void setup() {
// Connect to Wi-Fi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
// Set up the LED pins
pinMode(ledPin1, OUTPUT);
digitalWrite(ledPin1, LOW);
pinMode(ledPin2, OUTPUT);
digitalWrite(ledPin2, LOW);
// Set up the MQTT broker
mqttClient.setServer(mqttServer, mqttPort);
mqttClient.setCallback(callback);
// Connect to MQTT broker
while (!mqttClient.connected()) {
Serial.println("Connecting to MQTT broker...");
if (mqttClient.connect("ESP32Client")) {
Serial.println("Connected to MQTT broker");
} else {
Serial.print("Failed, rc=");
Serial.print(mqttClient.state());
Serial.println(" Retrying in 5 seconds...");
delay(5000);
}
}
// Subscribe to the desired topic
mqttClient.subscribe("your_topic");
}
void loop() {
// Maintain the MQTT connection
if (!mqttClient.connected()) {
Serial.println("Reconnecting to MQTT broker...");
while (!mqttClient.connect("ESP32Client")) {
Serial.print("Failed, rc=");
Serial.print(mqttClient.state());
Serial.println(" Retrying in 5 seconds...");
delay(5000);
}
Serial.println("Connected to MQTT broker");
mqttClient.subscribe("your_topic");
}
mqttClient.loop();
// Update P1 and P2 variables accordingly
if (digitalRead(ledPin1) == HIGH) {
P1 = 1;
} else {
P1 = 0;
}
if (digitalRead(ledPin2) == HIGH) {
P2 = 1;
} else {
P2 = 0;
}
// Perform other tasks or logic based on the values of P1 and P2
}