//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.
/*The code is designed for an ESP32 microcontroller to monitor gas levels using a sensor,
display the information on an LCD, and communicate alerts via MQTT.
It also controls LEDs and a buzzer based on the detected gas levels.*/
#include <WiFi.h> //WiFi.h: Manages WiFi connections.
#include <LiquidCrystal_I2C.h> //LiquidCrystal_I2C.h: Controls an LCD via I2C communication.
#include <WiFiClient.h> // WiFiClient.h: Provides WiFi client functionality to enable network connections.
#include <PubSubClient.h> //Handles MQTT protocol to publish and subscribe to messages.
// Defines sensor pin, buzzer pin, ADC reference voltage, ADC resolution, and LED pin.
#define sensor 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 Green_Led_PIN 19 // Pin number for the green LED
#define Yellow_Led_PIN 18 // Pin number for the yellow LED
#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; //Stores parts per million value for gas concentration.
char Red_Led_state = LOW; //Red_Led_state, Green_Led_state, Yellow_Led_state: Store LED states.
char Green_Led_state = LOW; // Stores the state of the green LED (LOW or HIGH)
char Yellow_Led_state = LOW; // Stores the state of the yellow LED (LOW or HIGH)
long previous_millis = 0; //previous_millis: Used for timing the main loop operations.
int interval = 1000; //interval: Defines the delay between consecutive gas checks in milliseconds.
// Sets WiFi credentials and MQTT broker details.
const char* ssid = "Wokwi-GUEST"; // SSID and password for connecting to the WiFi.
const char* password = ""; // Password for the WiFi network
const char* hostname = "broker.hivemq.com"; // MQTT broker hostname
// Initializes the LCD display with I2C address 0x27 and 16x2 dimensions.
LiquidCrystal_I2C lcd(0x27, 16, 2);
WiFiClient espClient; // Create a WiFi client object
PubSubClient client(espClient); // Create an MQTT client using the WiFi client
const char* espClientName = "esp32Client_4770080G"; // Unique client ID for MQTT broker
int PORTNUM = 1883; // Default MQTT port number
// Connects the ESP32 to the specified WiFi network.
// Uses a loop to retry connection attempts until successful.
// Prints connection status and IP address to the serial monitor.
void setup_wifi()
{
WiFi.begin(ssid, password); // Start the WiFi connection using the specified SSID and password
while (WiFi.status() != WL_CONNECTED) // Loop until the WiFi is connected
{
delay(1000); // Wait for 1 second before retrying
Serial.print("."); // Print a dot to indicate connection attempt
}
Serial.println(); // Newline after successful connection
Serial.print("Connected to ");
Serial.println(ssid); // Print the connected network's SSID
Serial.print("IP address: ");
Serial.println(WiFi.localIP()); // Print the IP address assigned to the ESP32
}
// MQTT Connection Function
// Attempts to connect to the MQTT broker. It keeps retrying until it gets connected.
void connectMQTT()
{
while (!client.connected()) { // Keep trying until the connection is established
Serial.print("Attempting MQTT connection...");
if (client.connect("esp32Client_4770080G")) // Try to connect to the broker
{
Serial.println("connected"); // Connection successful
MQTTSubscribe(); // Subscribe to the topic after connection
}
else
{
Serial.print("Failed with state ");
Serial.print(client.state()); // Print the connection state/error
delay(2000); // Wait 2 seconds before retrying
}
}
}
// Callback Function
void callback(String topic, byte* payload, unsigned int length)
{
String messageTemp; // Temporary storage for the message
Serial.print("Message received in topic: ");
Serial.print(topic); // Print the topic of the received message
Serial.print(" length is: ");
Serial.println(length); // Print the length of the received message
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") // Check if the topic matches
{
Serial.print(" Gas Level == ");
if (messageTemp == "Warning") // Check message content for "Warning"
{
digitalWrite(Yellow_Led_PIN , HIGH); // Turn on the yellow LED
Serial.print(" Warning! ");
}
else if (messageTemp == "Gas detected") // Check message content for "Gas detected"
{
digitalWrite(RED_LED_PIN, HIGH); // Turn on the red LED
Serial.print(" Gas detected! ");
}
else // Default case if message is not "Warning" or "Gas detected"
{
digitalWrite(Green_Led_PIN, HIGH); // Turn on the green LED
Serial.print(" Safe ");
}
}
}
void MQTTSubscribe()
{
client.subscribe("IOT/group5/project");
}
// Sets the MQTT broker's server and port. MQTT Setup
void setup_MQTT()
{
client.setServer(hostname, PORTNUM); // Sets the MQTT broker server and port
client.setCallback(callback); // Sets the function to handle incoming MQTT messages
}
// 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); // Initializes serial communication at 115200 baud
delay(5000); // Delay for system stability (5 seconds)
lcd.init(); // Initializes the LCD
lcd.backlight(); // Turns on the LCD backlight
pinMode(BUZZER_PIN, OUTPUT); // Sets buzzer pin as an output
pinMode(RED_LED_PIN, OUTPUT); // Sets red LED pin as an output
pinMode(Green_Led_PIN, OUTPUT); // Sets green LED pin as an output
pinMode(Yellow_Led_PIN, OUTPUT); // Sets yellow LED pin as an output
setup_wifi(); // Calls function to connect to WiFi
setup_MQTT(); // Calls function to set up 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();
}
// GASLevel Function
// 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 sensorValue)
{
float voltage = sensorValue * (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()
{
int sensorValue = analogRead(sensor);
int gasLevel = map(sensorValue, 0, 4095, 0, 100);
//adc_value = analogRead(POT_PIN); // Read the analog value from the potentiometer
//Serial.print("ADC Value: "); // Print the ADC value for debugging
//Serial.println(sensorValue);
ppm = convertToPPM(sensorValue);
//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 >= 1800) //it will trigger alarm at 55%
{ // Adjust threshold as needed
digitalWrite(BUZZER_PIN, HIGH);
tone(BUZZER_PIN, 200);
lcd.setCursor(0, 1);
lcd.print("Gas detected!!!");
//Serial.println("Gas detected");
client.publish("IOT/group5/project", "Gas detected");
//Red_Led_state = HIGH;
//Green_Led_state = LOW;
//Yellow_Led_state = LOW;
}
else if ((ppm >= 900) && (ppm <= 1800)) //28% - 55% under normal
{
digitalWrite(BUZZER_PIN, LOW);
noTone(BUZZER_PIN);
lcd.setCursor(0, 1);
lcd.print("Warning ! ! ! ");
//Serial.println("Normal");
client.publish("IOT/group5/project", "Warning");
//Red_Led_state = LOW;
//Green_Led_state = LOW;
//Yellow_Led_state = HIGH;
}
else // 0% - 27% is safe and will trigger Green Led
{
digitalWrite(BUZZER_PIN, LOW);
noTone(BUZZER_PIN);
lcd.setCursor(0, 1);
lcd.print("Safe ");
//Serial.println("Safe");
client.publish("IOT/group5/project", "Safe");
//Red_Led_state = LOW;
//Green_Led_state = HIGH;
//Yellow_Led_state = LOW;
}
digitalWrite(RED_LED_PIN, Red_Led_state);
digitalWrite(Green_Led_PIN, Green_Led_state);
digitalWrite(Yellow_Led_PIN, Yellow_Led_state);
}
// Main loop
void loop()
{
if (!client.connected())
{
connectMQTT();
}
client.loop();
long currentMillis = millis();
if ( currentMillis - previous_millis >= interval)
{
previous_millis = currentMillis;
GASLevel();
}
}