#include <WiFi.h>
#include <LiquidCrystal_I2C.h>
#include <WiFiClient.h>
#include <PubSubClient.h>
#define sensor 34
#define buzzer 2
#define ADC_REF 3.3
#define ADC_RESOLUTION 4095
int Red_Led = 23;
int Green_Led = 19;
char Green_Led_state = LOW;
char Red_Led_state = LOW;
const char* ssid = "Wokwi-GUEST"; //SSID and password for connecting to the WiFi.
const char* password = "";
const char* hostname = "broker.hivemq.com";
// Initialize the LCD display
LiquidCrystal_I2C lcd(0x27, 16, 2);
WiFiClient espClient;
PubSubClient client(espClient);
const char* espClientName = "esp32Client_4770080G"; // yourStudentID must be unique
int PORTNUM = 1883;
void setup_wifi()
{
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.print(".");
}
Serial.println();
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("Given IP by the router to ESP32 is ");
Serial.println(WiFi.localIP());
}
void connectMQTT()
{
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
if (client.connect("IOT/group5/project"))
{
Serial.println("connected");
MQTTSubscribe();
}
else
{
Serial.print("Failed with state ");
Serial.print(client.state());
delay(2000);
}
}
}
void callback(String topic, byte* payload, unsigned int length)
{
String messageTemp;
Serial.print("Message received in topic: ");
Serial.print(topic);
Serial.print(" length is: ");
Serial.println(length);
Serial.print("Data received from broker: ");
for (int i = 0; i<length; i++)
{
Serial.print( (char)payload[i] );
messageTemp += (char)payload[i];
}
if (topic == "IOT/group5/project")
{
Serial.print(" Gas Level == ");
if (messageTemp == "Normal")
{
digitalWrite(Red_Led, LOW);
Serial.print("Normal");
}
else if (messageTemp == "Gas detected")
{
digitalWrite(Red_Led, HIGH);
Serial.print(" Gas detected ");
}
}
}
void MQTTSubscribe()
{
client.subscribe("IOT/group5/project");
}
void setup_MQTT()
{
client.setServer(hostname, PORTNUM);
client.setCallback(callback);
}
void setup()
{
Serial.begin(115200);
delay(5000);
lcd.init();
lcd.backlight();
pinMode(buzzer, OUTPUT);
pinMode(Red_Led, OUTPUT);
pinMode(Green_Led, OUTPUT);
setup_wifi();
setup_MQTT();
lcd.setCursor(1, 0);
lcd.print("Gas Detector");
for (int a = 0; a <= 15; a++)
{
lcd.setCursor(a, 1);
lcd.print(".");
delay(200);
}
lcd.clear();
}
void GASLevel()
{
int sensorValue = analogRead(sensor);
int gasLevel = map(sensorValue, 0, 4095, 0, 100);
//adc_value = analogRead(A5);
if (sensorValue >= 2457)
{ // Adjust threshold as needed
digitalWrite(buzzer, HIGH);
tone(buzzer, 200);
lcd.setCursor(0, 1);
lcd.print("Gas detected!");
client.publish("IOT/group5/project", "Gas detected");
Red_Led_state = LOW;
Green_Led_state = LOW;
}
else if ( (sensorValue >= 819) && (sensorValue <= 2048) )
{
digitalWrite(buzzer, LOW);
noTone(buzzer);
lcd.setCursor(0, 1);
lcd.print("Normal ");
client.publish("IOT/group5/project", "Normal");
Red_Led_state = LOW;
Green_Led_state = HIGH;
}
else
{
lcd.setCursor(0, 0);
lcd.print("GAS Level: ");
}
digitalWrite(Red_Led, Red_Led_state);
digitalWrite(Green_Led, Green_Led_state);
}
void loop()
{
if (!client.connected())
{
connectMQTT();
}
client.loop();
GASLevel();
delay(1000); // Adjust delay as needed
}