Anyone have or know where I could find complete code to use an ESP32 with a DHT22 sensor and have it linked for viewing with Blynk and also have code in it to send via Gmail alerts when the temperature falls below a set threshold? This is the only project I want do build and don’t want to learn how to code just to do this. (hope this does not sound lazy but just doesn’t make sense to learn this skill just for one project)
unfortunately i am also lazy, and you will probably get an answer like this: “It’s worth noting that this forum isn’t a code factory”
but you could just try Chatgpt for fun? and probably you will figure this out by.
i usually try to find a similar project on youtube and start from there.
her is Chatgpts answer:
To achieve your goal, you need to:
Set up the hardware (ESP32, DHT22 sensor).
Install and configure libraries (DHT22, Blynk, and Gmail).
Write code that:
Reads data from the DHT22 sensor.
Sends the data to Blynk for remote monitoring.
Sends an email alert via Gmail when the temperature falls below a set threshold.
Here’s a step-by-step guide:
Materials:
ESP32 board
DHT22 temperature and humidity sensor
Jumper wires
Breadboard
Wiring:
DHT22:
VCC to 3.3V (on ESP32)
GND to GND (on ESP32)
Data Pin to a GPIO pin (e.g., GPIO 15 on ESP32)
Step 1: Set up Blynk
Create a Blynk project on the Blynk app.
Get your Blynk Auth Token from the Blynk app and note it down.
Step 2: Enable Gmail SMTP Access
You need to allow “Less secure apps” on your Gmail account, or ideally, generate an App Password if you have 2FA enabled. Follow Google’s guide to generate the app password.
Step 3: Install Required Libraries
You need to install the following libraries in the Arduino IDE:
DHT sensor library: DHT sensor library by Adafruit
Blynk library: Blynk by Volodymyr Shymanskyy
ESP32 Mail Client: ESP32 Mail Client (for sending Gmail via SMTP)
You can install these libraries through the Arduino IDE by going to Sketch > Include Library > Manage Libraries… and then searching for them.
Step 4: Write the Code
#include <WiFi.h>
#include <DHT.h>
#include <BlynkSimpleEsp32.h>
#include <ESP32_MailClient.h>
// Pin Definitions
#define DHTPIN 15 // DHT22 Data Pin connected to GPIO15
#define DHTTYPE DHT22 // Sensor type
DHT dht(DHTPIN, DHTTYPE);
// WiFi credentials
const char* ssid = "your-SSID"; // Replace with your WiFi SSID
const char* password = "your-PASSWORD"; // Replace with your WiFi password
// Blynk Authorization Token
char auth[] = "Your-Blynk-Auth-Token"; // Replace with your Blynk Auth Token
// Set the threshold temperature for alert
float thresholdTemp = 25.0; // Set the temperature threshold (in Celsius)
// Email credentials
#define SMTP_SERVER "smtp.gmail.com"
#define SMTP_PORT 465
#define AUTHOR_EMAIL "your-email@gmail.com" // Replace with your email
#define AUTHOR_PASSWORD "your-app-password" // Replace with your app password
#define RECIPIENT_EMAIL "recipient-email@gmail.com" // Replace with recipient email
// Blynk Virtual Pin for displaying temperature
#define VIRTUAL_PIN 1
WiFiClient client;
void setup() {
Serial.begin(115200);
dht.begin();
// Connect to WiFi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi!");
// Connect to Blynk
Blynk.begin(auth, WiFi.localIP(), ssid, password);
// Configure Email SMTP
ESP32_Mail_Client::begin(AUTHOR_EMAIL, AUTHOR_PASSWORD, SMTP_SERVER, SMTP_PORT);
}
void loop() {
Blynk.run(); // Keep the Blynk connection alive
// Read temperature from DHT22 sensor
float temp = dht.readTemperature();
// Send temperature to Blynk
Blynk.virtualWrite(VIRTUAL_PIN, temp);
// Check if temperature is below the threshold and send an alert via email
if (temp < thresholdTemp) {
sendEmailAlert(temp);
}
delay(2000); // Read every 2 seconds
}
void sendEmailAlert(float temperature) {
String subject = "Temperature Alert";
String message = "Warning! Temperature has dropped below the threshold.\n";
message += "Current Temperature: " + String(temperature) + "C";
ESP32_Mail_Client::sendMail(RECIPIENT_EMAIL, subject, message);
Serial.println("Email alert sent!");
}
Explanation:
WiFi setup: The ESP32 connects to your WiFi network.
DHT22 sensor: Reads temperature data from the DHT22 sensor.
Blynk: Sends the temperature value to Blynk app so you can monitor it remotely.
Gmail alerts: If the temperature falls below the threshold (25°C in this case), the system sends an email via Gmail using the SMTP protocol.
Step 5: Upload and Test
Make sure to replace:
your-SSID and your-PASSWORD with your WiFi credentials.
Your-Blynk-Auth-Token with the Blynk Auth Token from your project.
your-email@gmail.com and your-app-password with your Gmail credentials and app password.
recipient-email@gmail.com with the email address where you want to receive alerts.
Upload the code to your ESP32, and you should start seeing the temperature displayed on your Blynk app. If the temperature falls below the set threshold, you should receive an email alert.
Troubleshooting:
Make sure your WiFi credentials are correct and your ESP32 is connected to the internet.
Ensure you have enabled less secure apps or generated an app password for Gmail to work properly.
Let me know if you need more help with any of the steps!
Every programmer i know stands on the shoulders of those who came and coded before them. Use your imagination to find script pieces already out on the Internet and cobble them together with the “Hello World” sample Blynk demo program that already works. Sometimes the journey is more exciting along the way to a finished product. I started out as a neophyte here in my quest along the way to create two significant Blynk based solutions; I got a lot of suggestions and assistance from Pete and others along the way, but now I’m better off because I can actually support what I created. Good luck!
Cheers, Mike S.
The Blynk sketch builder will give you the basic code…
However, you should be aware that if you have a new Free subscription (one where the billing screen shows up to 10 devices/templates and a message counter that goes upto 30k) the you are limited to 30,000 messages per month.
This code sends two messages per second (1000ms as defined here)…
timer.setInterval(1000L, sendSensor);
so will use-up your 30,000 message allowance in around 4 hours.
Before you look at adding-in the email functionality, which uses Events in Blynk, via the Blynk.logEvent() command you should ask yourself if your current Blynk subscription is going to work for you.
BTW, don’t waste your time with the ChatGPT code above, it won’t work for a variety of reasons.
I am on the new free account so I do try and set it to send once per 10 minutes. I think the free subscription will work for me but I do have a question for you. I tried to set up automation thru Blynk but when I go to automations, select my device state trigger there are no option at all (such as select virtual pin 0 for my temperature). I use virtual pin 0 to transmit data to Blynk and it works fine displayed in the app. Is this because the free subscription limits this function or am I missing something in the setup? If Blynk would email me an alert I would not need the code for the email client. Better yet I would prefer a text if that’s available in the free subscription.
And yes tried for 8 hours with AI to write code and it worked for real simple tasks but always had errors with once adding the email client tasks.
I may have to continue to try your suggestion but have not had much success combining code from 2 separate projects as I always run into errors that I don’t have the knowledge to overcome. With some help I am sure I can get thru it eventually!
OMG, enabling the Condition Switch fixed the issue! I spent hours on this issue and gave up on it yesterday. Should have asked here sooner. Thank for the help!
Also to possibly help others for the no SMS option I have found that most cell carriers allow email to SMS so I will use that method here. As an example if you have Verizon and your mobile phone number is 123-456-7890, you send an email to 1234567890@vtext.com to receive a text. Not sure if it works with Blynk but if not can be added to the code.
I have had some luck with basic code and AI and that’s how I was able to setup the DHT22 sensor and connect to Blynk but then when I tried to add the email client in the code to send email alerts I had nothing but errors. Regardless I now have the built in email alerts from Blynk working so it all works as I would like now.