//IOT project:MQ2 senor simulation on ESP32
//Member name: Jayson Tham, Afif,Isaac Xin
//Libraries and Definitions
//Includes necessary libraries for WiFi, LCD display, and MQTT.
#include <WiFi.h>
#include <LiquidCrystal_I2C.h>
#include <WiFiClient.h>
#include <PubSubClient.h>
// Defines sensor pin, buzzer pin, ADC reference voltage, ADC resolution, and LED pin.
#define POT_PIN 34 // Analog pin where the potentiometer is connected
#define BUZZER_PIN 2 // Pin number where the buzzer is connected
#define RED_LED_PIN 23 // Pin number where the red LED is connected
#define ADC_REF 3.3 // ADC reference voltage
#define ADC_RESOLUTION 4095 // ADC resolution for ESP32
// Declares various variables including adc_value and Red_Led_state.
int adc_value;
float ppm;
char Red_Led_state = LOW;
// Sets WiFi credentials and MQTT broker details.
const char* ssid = "Wokwi-GUEST"; // SSID and password for connecting to the WiFi.
const char* password = "";
const char* hostname = "broker.hivemq.com";
// Initializes the LCD display with I2C address 0x27 and 16x2 dimensions.
LiquidCrystal_I2C lcd(0x27, 16, 2);
WiFiClient espClient;
PubSubClient client(espClient);
const char* espClientName = "esp32Client_4770080G"; // Unique client ID for MQTT broker
int PORTNUM = 1883;
// Connects the ESP32 to the specified WiFi network.
// It keeps retrying until it gets connected, then prints the assigned IP address.
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("IP address: ");
Serial.println(WiFi.localIP());
}
// Attempts to connect to the MQTT broker. It keeps retrying until it gets connected.
void connectMQTT() {
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
if (client.connect("esp32Client_4770080G")) {
Serial.println("connected");
} else {
Serial.print("Failed with state ");
Serial.print(client.state());
delay(2000);
}
}
}
// Sets the MQTT broker's server and port.
void setup_MQTT() {
client.setServer(hostname, PORTNUM);
}
// Main setup function
// Initializes serial communication, LCD display, and pins for the buzzer and LED.
// Calls WiFi and MQTT setup functions. Displays "Gas Detector" message on the LCD.
void setup() {
Serial.begin(115200);
delay(5000);
lcd.init();
lcd.backlight();
pinMode(BUZZER_PIN, OUTPUT);
pinMode(RED_LED_PIN, 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();
}
// Function to convert ADC value to ppm (assuming a linear relationship for demonstration)
// You may need to adjust the calibration values based on your sensor's datasheet and environment.
float convertToPPM(int adc_value) {
float voltage = adc_value * (ADC_REF / ADC_RESOLUTION);
// Assuming a linear relationship: ppm = k * voltage (you should calibrate this properly)
float ppm = voltage * 1000; // Adjust the factor based on calibration
return ppm;
}
// Function to read gas level, update the LCD, and handle MQTT communication
void GASLevel() {
adc_value = analogRead(POT_PIN); // Read the analog value from the potentiometer
Serial.print("ADC Value: "); // Print the ADC value for debugging
Serial.println(adc_value);
ppm = convertToPPM(adc_value);
Serial.print("PPM Value: "); // Print the PPM value for debugging
Serial.println(ppm);
lcd.clear(); // Clear the LCD
lcd.setCursor(0, 0);
lcd.print("GAS Level: ");
lcd.print(ppm);
lcd.print(" ppm");
if (ppm >= 500) { // Adjust threshold as needed
digitalWrite(BUZZER_PIN, HIGH);
tone(BUZZER_PIN, 200);
lcd.setCursor(0, 1);
lcd.print("Gas detected!");
Red_Led_state = HIGH;
Serial.println("Gas detected");
client.publish("IOT/group5/project", "Gas detected");
} else if (ppm > 500 && ppm < 3000) {
digitalWrite(BUZZER_PIN, LOW);
noTone(BUZZER_PIN);
lcd.setCursor(0, 1);
lcd.print("Normal ");
Red_Led_state = LOW;
Serial.println("Normal");
client.publish("IOT/group5/project", "Normal");
} else {
lcd.setCursor(0, 1);
lcd.print("Safe ");
}
digitalWrite(RED_LED_PIN, Red_Led_state);
}
// Main loop
void loop() {
if (!client.connected()) {
connectMQTT();
}
client.loop();
GASLevel();
delay(2000); // Adjust delay as needed
}