Course Content
IoT Engineering Course using ESP32 with 12 Real-World Projects

MODULE 10 – Gas Leakage Detection System

📘 Lesson 10.6 – Blynk IoT Cloud Version


🎯 Objective of This Lesson

Students will learn:

  • What is Cloud IoT

  • How to connect ESP32 to Blynk Cloud

  • How to create Datastreams

  • How to design Dashboard

  • How to monitor gas leakage from anywhere


🌐 What is Blynk IoT?

Blynk is a cloud IoT platform that allows:

  • Remote monitoring

  • Real-time data updates

  • Mobile dashboard design

  • Device control from anywhere

ESP32 sends gas data to Blynk Cloud.
Mobile app receives it from cloud.


🧾 Step 1 – Blynk Setup

1️⃣ Go to: https://blynk.cloud
2️⃣ Create account
3️⃣ Click Create Template
4️⃣ Select:

  • Hardware → ESP32

  • Connection Type → WiFi

5️⃣ Save Template


🧾 Step 2 – Create Datastreams

Go to:

Template → Datastreams → Create New Datastream

Create these:

Datastream Name Virtual Pin Type
Gas Value V0 Integer
Gas Status V1 String

Save all.


🧾 Step 3 – Create Device

1️⃣ Go to Devices
2️⃣ Click New Device
3️⃣ From Template
4️⃣ Copy:

  • Template ID

  • Template Name

  • Auth Token

You will paste these in code.


🧾 Step 4 – Dashboard Setup

Open Web Dashboard or Blynk Mobile App.

Add Widgets:

1️⃣ Gauge → V0 → Gas Value
2️⃣ Label → V1 → Gas Status

Now dashboard is ready.


🔌 Hardware Reminder

MQ3 Pin ESP32 Pin
VCC 5V
GND GND
AO GPIO 34

🧾 Complete ESP32 Blynk Code (Copy–Paste Ready)

⚠ Replace WiFi and Blynk credentials before uploading.

#define BLYNK_TEMPLATE_ID "YOUR_TEMPLATE_ID"
#define BLYNK_TEMPLATE_NAME "GasDetection"
#define BLYNK_AUTH_TOKEN "YOUR_AUTH_TOKEN"

#include <WiFi.h>
#include <BlynkSimpleEsp32.h>

#define MQ3_PIN 34

char ssid[] = "YOUR_WIFI_NAME";
char pass[] = "YOUR_WIFI_PASSWORD";

BlynkTimer timer;

int gasValue = 0;
int threshold = 1200; // Adjust after calibration

void sendGasData() {

gasValue = analogRead(MQ3_PIN);

Blynk.virtualWrite(V0, gasValue);

if (gasValue > threshold) {
Blynk.virtualWrite(V1, "⚠ GAS LEAK DETECTED!");
} else {
Blynk.virtualWrite(V1, "Environment Safe");
}
}

void setup() {

Serial.begin(115200);

Serial.println("Connecting to Blynk...");
Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);

Serial.println("Warming up sensor...");
delay(30000);
Serial.println("Sensor Ready!");

timer.setInterval(2000L, sendGasData);
}

void loop() {

Blynk.run();
timer.run();
}

🧠 Code Explanation (Step-by-Step)


1️⃣ Blynk Credentials

 
#define BLYNK_TEMPLATE_ID
#define BLYNK_AUTH_TOKEN
 

These connect ESP32 to your specific Blynk Cloud project.

Without correct credentials → device will not connect.


2️⃣ Blynk.begin()

 
Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);
 

This:

  • Connects ESP32 to WiFi

  • Connects ESP32 to Blynk Cloud server


3️⃣ Reading Gas Sensor

 
gasValue = analogRead(MQ3_PIN);
 

Reads value between:

0 – 4095


4️⃣ Sending Data to Cloud

 
Blynk.virtualWrite(V0, gasValue);
 

Sends gas value to Virtual Pin V0.

Dashboard Gauge connected to V0 will update automatically.


5️⃣ Gas Detection Logic

 
if (gasValue > threshold)
 

If gas crosses threshold:

Send alert message to V1.

Otherwise:

Environment Safe.


6️⃣ Timer Usage

 
timer.setInterval(2000L, sendGasData);
 

Runs sendGasData() every 2 seconds.

This prevents blocking loop and keeps Blynk stable.


📊 Dashboard Example Output

V0 → 350 → Environment Safe
V0 → 1800 → ⚠ GAS LEAK DETECTED!


🔧 Threshold Calibration

Tell students:

1️⃣ Observe clean air value
2️⃣ Expose sensor to alcohol vapor
3️⃣ Note value difference
4️⃣ Set threshold between both

Example:

Clean air → 400
Gas present → 1700

Set threshold = 1000


🎓 Learning Outcomes

Students now understand:

  • Cloud IoT architecture

  • Virtual Pins

  • Real-time cloud monitoring

  • Template & Device concept

  • Professional IoT deployment



Scroll to Top