Rohan Barnwal
Published

ESP32 Alcohol Detection and Notification System

Detect alcohol with precision! ESP32, MQ3, OLED & buzzer combo sends email alerts instantly. Powered by JLCPCB's custom PCB

IntermediateFull instructions provided1 hour571
ESP32 Alcohol Detection and Notification System

Things used in this project

Hardware components

ESP32 (38 Pin) WiFi + Bluetooth NodeMCU-32 Development Board
×1
Buzzer
Buzzer
×1
0.96 Inch Blue OLED Display Module SPI/I2C - 4pin
×1
MQ-3 Alcohol Sensor
×1

Hand tools and fabrication machines

Soldering iron (generic)
Soldering iron (generic)
Solder Wire, Lead Free
Solder Wire, Lead Free
Solder Flux, Soldering
Solder Flux, Soldering

Story

Read more

Schematics

Gerber_file

Code

Code

Arduino
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <WiFi.h>
#include <ESP_Mail_Client.h>

// Wi-Fi credentials
#define WIFI_SSID "Your_SSID"
#define WIFI_PASSWORD "Your_PASSWORD"

// Email credentials
#define SMTP_HOST "smtp.gmail.com"
#define SMTP_PORT 465
#define AUTHOR_EMAIL "Your_Email@gmail.com"
#define AUTHOR_PASSWORD "Your_Email_App_Password"
#define RECIPIENT_EMAIL "Recipient_Email@gmail.com"

// GPIO pins
#define MQ3_SENSOR_PIN 35
#define BUZZER_PIN 25

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

// SMTPSession for email
SMTPSession smtp;

// Global variables
bool emailSent = false;

// Function to handle email sending status
void smtpCallback(SMTP_Status status) {
  if (status.success()) {
    Serial.println("Email sent successfully!");
    emailSent = true;
  } else {
    Serial.printf("Failed to send email: %s\n", status.errorReason().c_str());
  }
}

// Function to initialize OLED display with an animation
void initializeDisplay() {
  if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
    Serial.println("OLED initialization failed!");
    while (true);
  }

  // Welcome animation
  int rectWidth = 10, rectHeight = 10;
  for (int i = 0; i < 20; i++) {
    display.clearDisplay();
    display.fillRect((SCREEN_WIDTH - rectWidth) / 2, (SCREEN_HEIGHT - rectHeight) / 2, rectWidth, rectHeight, SSD1306_WHITE);
    display.display();
    rectWidth += 5;
    rectHeight += 3;
    delay(50);
  }

  // Display project title
  display.clearDisplay();
  display.setTextSize(1);
  display.setTextColor(SSD1306_WHITE);
  display.setCursor(15, 20);
  display.println("Alcohol Detector");
  display.display();
  delay(2000);
}

// Function to send an email alert
void sendEmail() {
  // Configure SMTP session
  Session_Config config;
  config.server.host_name = SMTP_HOST;
  config.server.port = SMTP_PORT;
  config.login.email = AUTHOR_EMAIL;
  config.login.password = AUTHOR_PASSWORD;
  config.secure.startTLS = true;

  // Compose the email message
  SMTP_Message message;
  message.sender.name = "ESP32 Alcohol Detector";
  message.sender.email = AUTHOR_EMAIL;
  message.subject = "Alert: Alcohol Detected!";
  message.addRecipient("User", RECIPIENT_EMAIL);
  message.text.content = "Alcohol has been detected by the ESP32 system.";
  message.text.charSet = "utf-8";
  message.text.transfer_encoding = Content_Transfer_Encoding::enc_7bit;

  // Connect to SMTP server and send email
  if (!smtp.connect(&config)) {
    Serial.printf("SMTP connection failed: %s\n", smtp.errorReason().c_str());
    return;
  }
  if (!MailClient.sendMail(&smtp, &message)) {
    Serial.printf("Email sending failed: %s\n", smtp.errorReason().c_str());
  }
}

// Function to handle buzzer alert
void beepBuzzer() {
  digitalWrite(BUZZER_PIN, HIGH);
  delay(200);
  digitalWrite(BUZZER_PIN, LOW);
}

// Setup function
void setup() {
  Serial.begin(115200);

  // Initialize OLED display
  initializeDisplay();

  // Setup GPIO pins
  pinMode(MQ3_SENSOR_PIN, INPUT);
  pinMode(BUZZER_PIN, OUTPUT);
  digitalWrite(BUZZER_PIN, LOW);

  // Connect to Wi-Fi
  Serial.print("Connecting to Wi-Fi...");
  WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
  while (WiFi.status() != WL_CONNECTED) {
    Serial.print(".");
    delay(300);
  }
  Serial.println("\nConnected to Wi-Fi!");

  // Setup email callback
  smtp.callback(smtpCallback);
}

// Main loop
void loop() {
  int alcoholStatus = digitalRead(MQ3_SENSOR_PIN);

  if (alcoholStatus == LOW && !emailSent) {
    // Alcohol detected
    display.clearDisplay();
    display.setCursor(0, 0);
    display.setTextSize(1);
    display.println("Alcohol Detected!");
    display.println("Sending Email...");
    display.display();

    // Send email alert
    sendEmail();

    // Wait for email to be sent
    while (!emailSent) {
      delay(100);
    }

    // Trigger buzzer and update display
    beepBuzzer();
    display.clearDisplay();
    display.setCursor(0, 0);
    display.println("Mail Sent!");
    display.display();

    // Prevent rapid re-triggering
    delay(5000);
    emailSent = false;

  } else if (alcoholStatus != LOW) {
    // No alcohol detected
    display.clearDisplay();
    display.setCursor(0, 0);
    display.setTextSize(1);
    display.println("Alcohol Not Detected");
    display.display();
  }

  delay(1000);
}

Credits

Rohan Barnwal
27 projects • 35 followers
Rohan Barnwal - maker, hacker, tech enthusiast. I explore new tech & find innovative solutions. See my projects on hackster.io!
Contact

Comments

Please log in or sign up to comment.