#include <WiFi.h>
#include <PubSubClient.h>
#include <HTTPClient.h>
#include "DHTesp.h"
// WiFi credentials
const char* ssid = "Wokwi-GUEST";
const char* password = "";
// MQTT server
const char* mqttServer = "broker.hivemq.com";
const int mqttPort = 1883;
// Topics
const char* topicPotentiometer = "fire_system/potentiometer";
const char* topicPir = "fire_system/pir";
const char* topicLed = "fire_system/led";
const char* topicDisplay = "fire_system/display";
const char* topicLedBar = "fire_system/ledBar";
const char* topicLight = "fire_system/light";
const char* topicTemperature = "fire_system/temperature";
// GPIO pins
const int potPin = 34; // GPIO for Potentiometer
const int pirPin = 14; // GPIO for PIR sensor
const int ledPin = 2; // GPIO for LED
const int lightPin = 32;
const int buzPin = 17;
const int dhtPin = 25;
DHTesp dhtSensor;
// LED bar pin
int ledPins[] = {
0, 16, 21, 22, 13, 27, 26, 25, 33, 32
}; // an array of pin numbers to which LEDs are attached
WiFiClient wifiClient;
PubSubClient mqttClient(wifiClient);
bool ledState = false;
bool ledBarState = true;
// Connect to WiFi
void wifiConnect() {
Serial.print("Connecting to WiFi");
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println(" Connected!");
}
// Connect to MQTT server
void mqttConnect() {
while (!mqttClient.connected()) {
Serial.println("Attempting MQTT connection...");
String clientId = "ESP32Client-";
clientId += String(random(0xffff), HEX);
if (mqttClient.connect(clientId.c_str())) {
Serial.println("connected");
// Subscribe to necessary topics
mqttClient.subscribe(topicLed);
mqttClient.subscribe(topicDisplay);
mqttClient.subscribe(topicLedBar);
} else {
Serial.println("try again in 5 seconds");
delay(5000);
}
}
}
// MQTT message callback
void callback(char* topic, byte* message, unsigned int length) {
Serial.print("Message arrived on topic: ");
Serial.println(topic);
String strMsg;
for (int i = 0; i < length; i++) {
strMsg += (char)message[i];
}
Serial.println("Message: " + strMsg);
// Process the received message
if (String(topic) == topicLed) {
if (strMsg == "true") {
ledState = true;
Serial.println("LED BLINK");
} else if (strMsg == "off") {
ledState = false;
Serial.println("LED OFF");
}
} else if (String(topic)== topicLedBar ) {
if (strMsg == "on") {
ledBarState = true;
} else if (strMsg == "off") {
ledBarState = false;
}
}
}
void setup() {
pinMode(potPin, INPUT);
pinMode(pirPin, INPUT);
pinMode(ledPin, OUTPUT);
pinMode(buzPin, OUTPUT);
pinMode(lightPin, INPUT);
pinMode(dhtPin, INPUT);
// LedBar
for (int thisLed = 0; thisLed < 10; thisLed++) {
pinMode(ledPins[thisLed], OUTPUT);
}
Serial.begin(9600);
wifiConnect();
mqttClient.setServer(mqttServer, mqttPort);
mqttClient.setCallback(callback);
mqttClient.setKeepAlive(90);
// setup dht22
dhtSensor.setup(dhtPin, DHTesp::DHT22);
}
void loop() {
if (!mqttClient.connected()) {
mqttConnect();
}
mqttClient.loop();
TempAndHumidity data = dhtSensor.getTempAndHumidity();
int LigValue = analogRead(lightPin);
Serial.println(LigValue);
// Publish Light sensor status to MQTT server
char lightBuffer[50];
snprintf(lightBuffer, 50, "%d", LigValue);
mqttClient.publish(topicLight, lightBuffer);
Serial.println(lightBuffer);
// Publish Temperture sensor status to MQTT server
char temperatureBuffer[50];
snprintf(temperatureBuffer, 50,"%.2f", data.temperature);
mqttClient.publish(topicTemperature, temperatureBuffer);
Serial.println(temperatureBuffer);
delay(5000);
}