In the world of IoT. real-time communication is vital. Whether you want to receive alerts from sensors, get notified about certain events, or just automate regular updates, an email-sending mechanism can be incredibly useful. This project demonstrates how to use the ESP32 microcontroller to send emails automatically based on specific triggers. The system is easy to set up, highly customizable, and can be integrated into various IoT applications.
Components Required:- ESP32 Development Board: The brain of the project that handles the Wi-Fi connectivity and email sending.
- Micro-USB Cable: for programming the ESP32 and providing power
- Wi-Fi Network: To enable the ESP32 to connect to the internet and send emails.
- Arduino IDE: For coding and uploading the firmware to the ESP32.
- Install Arduino IDE: Download and install the Arduino IDE from the official site
- Install ESP32 Board Support:
- Open Arduino IDE, go to FILE > PREFERENCES.
- In the “ADDITIONAL BOARD MANAGER URLs” field, paste this URL ‘https://dl.espressif.com/dl/package_esp32_index.json’.
- Go to TOOLS > BOARD > BOARD MANAGER, search for ESP32 and install the latest version.
- Install Required Libraries:
- In the Arduino IDE, got to “SKETCH > INCLUDE LIBRARY > MANAGE LIBRARIES”. Search for and install the following libraries:ESP Mail Client WiFi (should be pre-installed with ESP32)
This project uses the ESP_Mail_Client library to handle the SMTP protocol, allowing ESP32 to send emails. The code provided below is designed to connect to a Wi-Fi network, log in to an SMTP server, and send an email with a simple message
Code Explanation:#include <Arduino.h>
#include <WiFi.h>
#include <ESP_Mail_Client.h>
#define WIFI_SSID "your_SSID"
#define WIFI_PASSWORD "your_PASSWORD"
#define SMTP_HOST "smtp.gmail.com"
#define SMTP_PORT 465
#define AUTHOR_EMAIL "your_email@gmail.com"
#define AUTHOR_PASSWORD "your_app_password"
#define RECIPIENT_EMAIL "recipient_email@gmail.com"
SMTPSession smtp;
void smtpCallback(SMTP_Status status);
void setup() {
Serial.begin(115200);
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(300);
}
smtp.debug(1);
smtp.callback(smtpCallback);
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.time.ntp_server = F("pool.ntp.org,time.nist.gov");
config.time.gmt_offset = 3;
config.time.day_light_offset = 0;
SMTP_Message message;
message.sender.name = F("ESP32");
message.sender.email = AUTHOR_EMAIL;
message.subject = F("ESP32 Test Email");
message.addRecipient(F("Recipient"), RECIPIENT_EMAIL);
message.text.content = "Hello World! - Sent from ESP32";
message.text.charSet = "us-ascii";
message.text.transfer_encoding = Content_Transfer_Encoding::enc_7bit;
if (!smtp.connect(&config)) {
Serial.printf("Connection error: %d, %s\n", smtp.statusCode(), smtp.errorReason().c_str());
return;
}
if (!MailClient.sendMail(&smtp, &message))
Serial.printf("Error: %d, %s\n", smtp.statusCode(), smtp.errorReason().c_str());
}
void loop() {}
void smtpCallback(SMTP_Status status) {
Serial.println(status.info());
if (status.success()) {
Serial.println("Email sent successfully.");
smtp.sendingResult.clear();
} else {
Serial.println("Failed to send email.");
}
}
How It Works:- Wi-Fi Connection: The ESP32 connects to the Wi-Fi network using the provided SSID and password.
- SMTP Server Connection: The ESP32 connects to the SMTP server using the specified host and port.
- Email Composition: An email is composed with a subject, sender, recipient, and text content.
- Email Sending: The email is sent using the “MailClient.sendMail()” function.
- Status Callback: The status of the email sending process is displayed in the serial monitor.
- Check Serial Monitor: Open the Serial Monitor in Arduino IDE to view the connection status, email sending status, and any potential errors.
- Verify Wi-Fi Credentials: Ensure that the SSID and password are correct for the WI-FI network.
- Check SMTP Credentials: Ensure that the email and app password are correct for the SMTP server.
- Security Systems: Automatically send an email when a sensor is triggered.
- Remote Monitoring: Send periodic updates or alerts from remote sensors.
- Notification Systems: Alert users when certain threshold are met (e.g. temperature, humidity etc.).
Conclusion:
This project provides a straightforward way to add functionality to your IoT projects using the ESP32. Whether you need to send alerts, notifications, or just want to experiment with email automation, this guide covers the essential steps to get you started. With a few modifications, you can customize the system to fit your specific needs. If you loved it don't forget to like the project and video and also follow me on hackster.io and also subscribe me on YouTube meet you soon with another project
Comments