#include <WiFi.h>
#include <PubSubClient.h>
// Replace with your network credentials
const char* ssid = "Wokwi-GUEST";
const char* password = "";
// Replace with your MQTT broker details
const char* mqttServer = "localhost";
const int mqttPort = 1883;
// Replace with your LED pin
const int ledPin = 5;
WiFiClient espClient;
PubSubClient client(espClient);
void setup() {
// Start Serial Monitor
Serial.begin(115200);
// Connect to Wi-Fi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(250);
}
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
// Set the LED pin as an output
pinMode(ledPin, OUTPUT);
// Set up MQTT client
client.setServer(mqttServer, mqttPort);
}
void loop() {
if (!client.connected()) {
digitalWrite(ledPin, HIGH); // turn the LED on
delay(500); // wait for 500 milliseconds
digitalWrite(ledPin, LOW); // turn the LED off
delay(500);
}
}