Are you ready to dive into an exciting project that combines safety and technology? Let’s create a gas leak detector using the Beetle ESP32-C3, the DFRobot MEMS gas sensor, an LED indicator, and a Telegram bot for notifications. This project is perfect for ensuring safety in your home or workspace by detecting harmful gases and alerting you in real time. Let’s get started! 🎉
🛠️ What You’ll Need- Beetle ESP32-C3: 🐞 A compact, powerful microcontroller with built-in Wi-Fi and Bluetooth. It’s perfect for IoT projects due to its small size and robust capabilities.
- DFRobot MEMS Gas Sensor: 🌬️ Capable of detecting various gases like CO, CH4, C2H5OH, C3H8, C4H10, H2, H2S, NH3, NO, and NO2. This sensor is highly sensitive and provides accurate readings.
- Breadboard and Jumper Wires: 🔌 For easy and flexible connections between components.
- LED: 💡 To visually indicate gas leaks. LEDs are great for immediate visual feedback.
- Resistor: 🛡️ To limit the current through the LED, preventing it from burning out.
- OLED Display: 📟 To show gas concentration levels. OLED displays are bright and easy to read.
- USB Cable: 🔌 To connect the Beetle ESP32-C3 to your computer for programming.
- Arduino IDE: 💻 For writing and uploading code to the Beetle ESP32-C3.
- Telegram Bot: 📲 For sending notifications directly to your phone.
Connect the Gas Sensor:
- Connect the VCC pin of the gas sensor to the 3.3V pin on the Beetle ESP32-C3.
- Connect the GND pin of the gas sensor to the GND pin on the Beetle ESP32-C3.
- Connect the SDA and SCL pins of the gas sensor to the corresponding pins on the Beetle ESP32-C3.
Connect the LED:
- Connect the anode (long leg) of the LED to a digital pin (e.g., 0) on the Beetle ESP32-C3 through a resistor (e.g., 220 ohms).
- Connect the cathode (short leg) of the LED to the GND pin.
Next, we need to download and install the following Arduino libraries in our Arduino IDE.
DFRobot_MICS.h: For the MEMS gas sensor.
upload the following simple sketch to check the gas leakage.
#include "DFRobot_MICS.h"
#define CALIBRATION_TIME 1
#define Mics_I2C_ADDRESS 0x75
DFRobot_MICS_I2C mics(&Wire, Mics_I2C_ADDRESS);
#define ADC_PIN A0
#define POWER_PIN 10
void setup()
{
Serial.begin(115200);
while (!mics.begin()) {
Serial.println("NO Deivces !");
delay(1000);
} Serial.println("Device connected successfully !");
uint8_t mode = mics.getPowerState();
if (mode == SLEEP_MODE) {
mics.wakeUpMode();
Serial.println("wake up sensor success!");
} else {
Serial.println("The sensor is wake up mode");
}
while (!mics.warmUpTime(CALIBRATION_TIME)) {
Serial.println("Please wait until the warm-up time is over!");
delay(1000);
}
}
void loop()
{
int8_t gasFlag = mics.getGasExist(C4H10);
if (gasFlag == EXIST) {
Serial.println("The gas exists!");
} else {
Serial.println("The gas does not exist!");
}
delay(1000);
}
this sketch can detect the C4H10 Butane.
int8_t gasFlag = mics.getGasExist(C4H10);
if (gasFlag == EXIST) {
Serial.println("The gas exists!");
} else {
Serial.println("The gas does not exist!");
}
This above piece of code can be changed based on the gas that we want to detect. You can choose between CO, C2H5OH(Alcohol), H2, NO2, NH3, CH4.
Additional InfoButane (C4H10) is a highly flammable hydrocarbon gas commonly used in lighters and portable stoves. It exists in two isomeric forms: n-butane and isobutane. Both are colorless gases that can be easily liquefied.
Key Properties of Butane:
- Chemical Formula: C4H10
- Molecular Weight: 58.12 g/mol
- Boiling Point: -1°C
- Melting Point: -138°C
- Density: 2.48 kg/m³
- Odor: Gasoline-like or natural gas-like
Uses of Butane:
- Fuel for Lighters: Butane is the primary fuel used in cigarette lighters and portable camping stoves due to its efficient combustion properties.
- Gasoline Blending: It is often blended with propane and marketed as LPG (liquefied petroleum gas) for heating appliances and vehicles.
- Feedstock: Used in the production of ethylene and butadiene, which are important in the manufacture of plastics and synthetic rubber.
Safety Information:
- Highly Flammable: Butane can form explosive mixtures with air. It should be handled with care and stored in well-ventilated areas.
- Health Hazards: Inhalation of butane can cause dizziness, drowsiness, and in extreme cases, asphyxiation.
Butane is a versatile and widely used gas, but it’s important to handle it safely due to its flammability and potential health risks.
Step 3: Simple DemoHere I have used the gas lighter to simulate the system.
Create a Telegram Bot:
- Open Telegram and search for
BotFather
.
- Start a chat with BotFather and use the command
/newbot
to create a new bot.
- Follow the instructions to get your bot token.
Get Your Chat ID:
- Start a chat with your bot and send any message.
- Look for the
chat
object in the response to find your chat ID.
Open the Arduino IDE and install the necessary libraries:
- Wire.h: For I2C communication.
- WiFi.h: For connecting to Wi-Fi.
- UniversalTelegramBot.h: For Telegram bot communication.
Here’s the code to get you started:
#include "DFRobot_MICS.h"
#include <WiFi.h>
#include <WiFiClientSecure.h>
#include <UniversalTelegramBot.h> // Universal Telegram Bot Library written by Brian Lough: https://github.com/witnessmenow/Universal-Arduino-Telegram-Bot
#include <ArduinoJson.h>
// Replace with your network credentials
const char* ssid = "";
const char* password = "";
// Use @myidbot to find out the chat ID of an individual or a group
// Also note that you need to click "start" on a bot before it can
#define CHAT_ID ""
// Initialize Telegram BOT
#define BOTtoken "" // your Bot Token (Get from Botfather)
#define CALIBRATION_TIME 1
#define Mics_I2C_ADDRESS 0x75
WiFiClientSecure client;
UniversalTelegramBot bot(BOTtoken, client);
//Checks for new messages every 1 second.
int botRequestDelay = 1000;
unsigned long lastTimeBotRan;
DFRobot_MICS_I2C mics(&Wire, Mics_I2C_ADDRESS);
#define ADC_PIN A0
#define POWER_PIN 10
static int led = 0;
static int led1 = 10;
//Handle what happens when you receive new messages
void handleNewMessages(int numNewMessages) {
Serial.println("handleNewMessages");
Serial.println(String(numNewMessages));
for (int i = 0; i < numNewMessages; i++) {
// Chat id of the requester
String chat_id = String(bot.messages[i].chat_id);
if (chat_id != CHAT_ID) {
bot.sendMessage(chat_id, "Unauthorized user", "");
continue;
}
// Print the received message
String text = bot.messages[i].text;
Serial.println(text);
String from_name = bot.messages[i].from_name;
if (text == "/start") {
String welcome = "Welcome, " + from_name + ".\n";
welcome += "Use the following command to get current readings.\n\n";
welcome += "/readings \n";
bot.sendMessage(chat_id, welcome, "");
}
}
}
void setup()
{
Serial.begin(115200);
pinMode(led, OUTPUT);
pinMode(led1, OUTPUT);
while (!mics.begin()) {
Serial.println("NO Deivces !");
delay(1000);
} Serial.println("Device connected successfully !");
uint8_t mode = mics.getPowerState();
if (mode == SLEEP_MODE) {
mics.wakeUpMode();
Serial.println("wake up sensor success!");
} else {
Serial.println("The sensor is wake up mode");
}
while (!mics.warmUpTime(CALIBRATION_TIME)) {
Serial.println("Please wait until the warm-up time is over!");
digitalWrite(led1, LOW);
delay(500);
digitalWrite(led1, HIGH);
delay(500);
}
// Connect to Wi-Fi
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
#ifdef ESP32
client.setCACert(TELEGRAM_CERTIFICATE_ROOT); // Add root certificate for api.telegram.org
#endif
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi..");
}
// Print ESP32 Local IP Address
Serial.println(WiFi.localIP());
}
void loop()
{
int numNewMessages = bot.getUpdates(bot.last_message_received + 1);
while (numNewMessages) {
Serial.println("got response");
handleNewMessages(numNewMessages);
numNewMessages = bot.getUpdates(bot.last_message_received + 1);
}
digitalWrite(led1, HIGH);
int8_t gasFlag = mics.getGasExist(C4H10);
if (gasFlag == EXIST) {
Serial.println("The gas exists!");
String message = "Gas Leak Detected";
bot.sendMessage(CHAT_ID, message, "");
digitalWrite(led, HIGH);
} else {
Serial.println("The gas does not exist!");
digitalWrite(led, LOW);
}
delay(1000);
}
Here is the final output from the Gas Leak detector:
Here is the telegram bot response
Congratulations! You’ve built a gas leak detector using the Beetle ESP32-C3, the DFRobot MEMS gas sensor, an LED indicator, and a Telegram bot for notifications. This project not only enhances your safety but also gives you hands-on experience with IoT and sensor integration. Feel free to customize the project further by adding more sensors or integrating it with a cloud service for remote monitoring.
Happy building! 🛠️🔧 If you have any questions or need further assistance, feel free to ask. 😊
Comments