Step 1: Components Required
To begin with, you will need the following components:
- ESP32 development board
- DHT11 or DHT22 temperature sensor
- Jumper wires
- Breadboard
- Power supply (e.g., USB cable)
Step 2: Setting Up the Hardware
Start by connecting the temperature sensor to the ESP32. Use the following connections:
- VCC (sensor) to 3.3V (ESP32)
- GND (sensor) to GND (ESP32)
- Data (sensor) to GPIO pin (e.g., GPIO 4)
Step 3: Programming the ESP32
For this project, you’ll need to program the ESP32 using the Arduino IDE. Install the necessary libraries:
Next, set up your Wi-Fi credentials, DHT sensor, and email settings within the code. You’ll need an SMTP server, like Gmail’s SMTP, to send emails.
Step 4: Creating the Web Server Interface
The web server allows you to change the temperature threshold in real-time. Here’s how to set it up:
AsyncWebServer server(80);
void setup() {
// Initialize Serial Monitor
Serial.begin(115200);
// Connect to Wi-Fi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println(“Connecting to WiFi…”);
}
Serial.println(“Connected to WiFi”);
// Initialize DHT sensor
dht.begin();
// Define routes for the web server
server.on(“/”, HTTP_GET, [](AsyncWebServerRequest *request){
request->send(200, “text/plain”, “ESP32 Email Alert System”);
});
// Route to change temperature threshold
server.on(“/setThreshold”, HTTP_GET, [](AsyncWebServerRequest *request){
String inputParam;
if (request->hasParam(“value”)) {
inputParam = request->getParam(“value”)->value();
thresholdValue = inputParam.toInt();
}
request->send(200, “text/plain”, “Threshold updated”);
});
// Start server
server.begin();
}
Step 5: Implementing the Email Alert Function
Within your loop()
function, continuously read the temperature and compare it to the threshold. If the temperature exceeds the threshold, trigger the email alert:
void loop() {
float temperature = dht.readTemperature();
if (temperature > thresholdValue) {sendEmailAlert(temperature);
}
delay(2000); // Delay between readings}
void sendEmailAlert(float temperature) {SMTPData smtpData;
smtpData.setLogin(smtpServer, smtpPort, emailSenderAccount, emailSenderPassword);smtpData.setSender(“ESP32”, emailSenderAccount);
smtpData.setPriority(“High”);
smtpData.setSubject(“Temperature Alert!”);
smtpData.setMessage(“The current temperature is: “ + String(temperature) + “°C, which exceeds your threshold.”, false);
smtpData.addRecipient(emailRecipient);
if (!MailClient.sendMail(smtpData)) {Serial.println(“Error sending email”);
}
smtpData.empty();}
Step 6: Testing and Deploying
Once your code is uploaded to the ESP32, open the Serial Monitor to verify the Wi-Fi connection and web server status. Access the web server via your browser using the ESP32’s IP address. Adjust the temperature threshold through the interface, and trigger alerts by heating or cooling the sensor.
With this setup, you now have a versatile temperature monitoring system powered by the ESP32. The ability to receive email alerts and adjust thresholds through a web interface adds significant value to your IoT projects. Whether you’re protecting equipment or ensuring optimal conditions, this project provides a reliable and flexible solution.