0% found this document useful (0 votes)
8 views5 pages

Code - IOT

This document contains an Arduino sketch for a gas leak detector using an ESP8266 microcontroller. It integrates Wi-Fi connectivity, OLED display for user interface, and Blynk and IFTTT services for remote monitoring and alerts. The code includes functionality for detecting gas levels, activating a buzzer for alerts, and displaying information on the OLED screen based on the gas concentration detected by the MQ6 sensor.

Uploaded by

balogun cornel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views5 pages

Code - IOT

This document contains an Arduino sketch for a gas leak detector using an ESP8266 microcontroller. It integrates Wi-Fi connectivity, OLED display for user interface, and Blynk and IFTTT services for remote monitoring and alerts. The code includes functionality for detecting gas levels, activating a buzzer for alerts, and displaying information on the OLED screen based on the gas concentration detected by the MQ6 sensor.

Uploaded by

balogun cornel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

#include <ESP8266WiFi.

h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <WiFiManager.h> // Wi-Fi Manager Library

// === Blynk Credentials ===


#define BLYNK_AUTH_TOKEN "OE4uEpVBopIfPSIcSiayGrnRNzWrGaIh"
#define BLYNK_TEMPLATE_ID "TMPL241NE-FxW"
#define BLYNK_TEMPLATE_NAME "Gas detector dashboard "

#include <BlynkSimpleEsp8266.h>
#include <ESP8266HTTPClient.h>

// === OLED ===


#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

#define MQ6_PIN A0
#define buzzer D5

String name = "LPG/Butane Detector";

// Gas level thresholds in PPM


const int SAFE_LEVEL = 600; // Clean air
const int WARNING_LEVEL = 1000; // Mild leak
const int DANGER_LEVEL = 2000; // Serious leak

unsigned long warmUpStartTime;


const unsigned long warmUpDuration = 60000; // 1 minute warm-up

WiFiManager wm; // Initialize WiFiManager

char auth[] = BLYNK_AUTH_TOKEN;

// === IFTTT Webhook ===


const String IFTTT_KEY = "mHGMCjmy3HmxEw8Xt371CNughQraAWPDnyFSDfilKVS";
const String IFTTT_EVENT = "gas_alert";

// Gas level categories for IFTTT logic


enum GasLevelCategory { LEVEL_SAFE, LEVEL_DETECTED, LEVEL_WARNING, LEVEL_DANGER };
GasLevelCategory lastLevel = LEVEL_SAFE;

void setup() {
[Link](WIFI_STA); // explicitly set mode, esp defaults to STA+AP
[Link](115200);
pinMode(buzzer, OUTPUT);
digitalWrite(buzzer, LOW);

if (![Link](SSD1306_SWITCHCAPVCC, 0x3C)) {
[Link](F("SSD1306 allocation failed"));
while (true); // Halt if display fails to initialize
}

// Show "Starting AP Mode..." on OLED


[Link]();
[Link](1);
[Link](WHITE);
[Link](10, 0);
[Link]("Welcome, Starting up...");
[Link]();

[Link](WIFI_STA);
delay(500);
[Link]("GAS DETECTOR");
[Link](false);
//[Link](60);

if([Link]("GAS || CONFIG PORTAL")){


[Link]("connected...yeey :)");
}
else {
[Link]("Configportal running");
}

[Link](auth);
[Link]();

// Once the Wi-Fi configuration is done, show the appropriate message


[Link]();
[Link](1);
[Link](WHITE);
[Link](10, 0);
[Link]("OTA Configured!");
[Link]();

warmUpStartTime = millis(); // Track the start of the warm-up phase


[Link]();
printTitle(); // Display title and initialize screen
[Link]();
}

void loop() {
[Link](); // Handle WiFiManager process
[Link]();

int rawVal = analogRead(MQ6_PIN); // Read MQ6 sensor value


int ppm = map(rawVal, 0, 1023, 0, 10000); // Map value to PPM (approximate)

// Print raw value and PPM to Serial Monitor for debugging


[Link]("Raw: ");
[Link](rawVal);
[Link](" | Approx PPM: ");
[Link](ppm);

// Clear the display and re-print title


[Link]();
printTitle();

unsigned long currentTime = millis();


if (currentTime - warmUpStartTime < warmUpDuration) { // Warm-up phase
printWarming(ppm);
digitalWrite(buzzer, LOW); // No alarm during warm-up
} else { // After warm-up phase
printGasValue(ppm);
printGasLevel(ppm);
updateBlynk(ppm); // Only send ppm value to Blynk
sendIFTTT(ppm); // Send IFTTT alerts if gas level changes
}

[Link](); // Update display


delay(500); // Small delay for display refresh
}

// === Display Functions ===

void printTitle() {
[Link](1);
[Link](WHITE);
[Link](22, 0);
[Link]("Gas Leak Detector");

[Link](5, 15);
[Link](name);
}

void printWarming(int ppm) {


[Link](1);
[Link](0, 35);
[Link]("Warming up...");

[Link](0, 50);
[Link]("Sensor: ");
[Link](ppm);
[Link](" ppm");
}

void printGasValue(int ppm) {


[Link](2);
[Link](20, 25);
[Link](ppm);
[Link](" ppm");
}

void printGasLevel(int ppm) {


[Link](1);
[Link](10, 55);

if (ppm < SAFE_LEVEL) {


[Link]("Air is clean.");
digitalWrite(buzzer, LOW); // No alarm for safe levels
} else if (ppm < WARNING_LEVEL) {
[Link]("Gas Detected!");
beepBuzzer(); // Short beep for detected gas
} else if (ppm < DANGER_LEVEL) {
[Link]("Warning! Gas High");
beepBuzzer(); // Short beep for warning levels
} else {
[Link]("DANGER! Gas Leak!");
continuousBeep(); // Continuous beep for dangerous levels
}
}

// === Buzzer Functions ===

void beepBuzzer() {
digitalWrite(buzzer, HIGH);
delay(100); // Short beep
digitalWrite(buzzer, LOW);
}

void continuousBeep() {
digitalWrite(buzzer, HIGH); // Continuous beep
}

// === Blynk Update ===

void updateBlynk(int ppm) {


[Link](V0, ppm);
}

// === IFTTT Webhook ===

void sendIFTTT(int ppm) {


if ([Link]() != WL_CONNECTED) return;

GasLevelCategory currentLevel;

if (ppm < SAFE_LEVEL) {


currentLevel = LEVEL_SAFE;
} else if (ppm < WARNING_LEVEL) {
currentLevel = LEVEL_DETECTED;
} else if (ppm < DANGER_LEVEL) {
currentLevel = LEVEL_WARNING;
} else {
currentLevel = LEVEL_DANGER;
}

if (currentLevel != lastLevel) { // Only send if gas level category changed


HTTPClient http;
String url = "[Link] + IFTTT_EVENT + "/with/key/" +
IFTTT_KEY;
String value1, value2 = String(ppm), value3;

switch (currentLevel) {
case LEVEL_SAFE:
value1 = "Safe Air";
value3 = "Air is clean. No action needed.";
break;
case LEVEL_DETECTED:
value1 = "Gas Detected!";
value3 = "Ventilate the room.";
break;
case LEVEL_WARNING:
value1 = "WARNING!";
value3 = "Evacuate area. Avoid sparks.";
break;
case LEVEL_DANGER:
value1 = "DANGER!";
value3 = "Major leak! Call emergency services.";
break;
}

String payload = "{\"value1\":\"" + value1 + "\",\"value2\":\"" + value2 +


"\",\"value3\":\"" + value3 + "\"}";
[Link](url);
[Link]("Content-Type", "application/json");
int httpResponseCode = [Link](payload);
[Link]("IFTTT Response: ");
[Link](httpResponseCode);
[Link]();

lastLevel = currentLevel;
}
}

You might also like