0% found this document useful (0 votes)
6 views4 pages

Updated code with email service

The document contains updated code for an ESP32-based smoke detection system that includes an email notification feature via a webhook. It sets up WiFi connectivity and monitors smoke levels, triggering an email alert when smoke is detected. The code also manages hardware components such as LEDs, a buzzer, and a servo motor based on sensor readings.

Uploaded by

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

Updated code with email service

The document contains updated code for an ESP32-based smoke detection system that includes an email notification feature via a webhook. It sets up WiFi connectivity and monitors smoke levels, triggering an email alert when smoke is detected. The code also manages hardware components such as LEDs, a buzzer, and a servo motor based on sensor readings.

Uploaded by

Masese Gavin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Updated code with email service.

I left the circtuit diagram as it was initially so msee aliconnect ataconnect pia.

#include <WiFi.h>

#include <HTTPClient.h>

#include <ESP32Servo.h> // Changed from Servo.h to ESP32Servo.h

// Define hardware pins and thresholds (constants preserved from your first code)

Servo Serv;

int pinIR = 5;

int pinServo = 3;

int val = 0;

int redled = 12;

int greenled = 11;

int buzzer = 10;

int smokeA0 = A5;

int sensorThres = 650;

// WiFi Credentials

const char* ssid = "STARLINK";

const char* password = "Oakley4021?!";

// Webhook URL for email notification (Google Apps Script)

const char* webhookUrl =


"https://script.google.com/macros/s/AKfycbylPuDuaUOJt97Owyntub7_f_Us6Ep26uqlcaNOfc-
ZU7198SUl55YI0-X2GGeWM6qk/exec";

// Function to send HTTP GET request to trigger an email via the webhook

void sendEmailWebhook() {
if (WiFi.status() == WL_CONNECTED) {

HTTPClient http;

http.begin(webhookUrl); // Begin connection to the webhook

int httpResponseCode = http.GET(); // Send the GET request

if (httpResponseCode > 0) {

Serial.print("Webhook response code: ");

Serial.println(httpResponseCode);

String payload = http.getString();

Serial.println("Response: " + payload);

} else {

Serial.print("Error sending webhook: ");

Serial.println(http.errorToString(httpResponseCode));

http.end(); // Close the connection

} else {

Serial.println("WiFi not connected!");

void setup() {

// Initialize pins

pinMode(pinIR, INPUT);

Serv.attach(pinServo);

pinMode(redled, OUTPUT);

pinMode(greenled, OUTPUT);

pinMode(buzzer, OUTPUT);

pinMode(smokeA0, INPUT);
// Start serial communication

Serial.begin(115200);

// Connect to WiFi

Serial.print("Connecting to WiFi");

WiFi.begin(ssid, password);

while (WiFi.status() != WL_CONNECTED) {

delay(500);

Serial.print(".");

Serial.println("\nConnected to WiFi!");

void loop() {

// Read sensor values

val = digitalRead(pinIR);

int analogSensor = analogRead(smokeA0);

Serial.print("Smoke Sensor (A5): ");

Serial.println(analogSensor);

// Check if sensor conditions are met

if (analogSensor > sensorThres && val == 1) {

digitalWrite(redled, HIGH);

digitalWrite(greenled, LOW);

tone(buzzer, 1000, 200);

Serv.write(15);

// Trigger the email notification via webhook

Serial.println("🚨 Smoke Detected! Sending Email via Webhook...");


sendEmailWebhook();

delay(5000); // Wait 5 seconds to avoid sending multiple emails too quickly

} else {

digitalWrite(redled, LOW);

digitalWrite(greenled, HIGH);

noTone(buzzer);

Serv.write(150);

delay(1000); // Main loop delay

You might also like