Hello,
I have Esp32 d1 r32 card. With this card, I want to send an e-mail when the temperature is over 23 degrees. But I don't know how to send mail. Can you give detailed information about the subject?
#include <WiFiClientSecure.h>
#include <LiquidCrystal_I2C.h>
#include <Wire.h>
#include "DHT.h"
#include <WiFi.h>
#define DHTPIN 17
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
LiquidCrystal_I2C lcd(0x27, 16, 2);
const char* ssid = "test"; // Wi-Fi ağınızın SSID'si
const char* password = "TEST!"; // Wi-Fi ağınızın şifresi
const char* smtpServer = "smtp.office365.com"; // SMTP sunucu adresi
const int smtpPort = 587; // SMTP sunucu bağlantı noktası
const char* senderEmail = "test@test.com"; // Gönderen e-posta adresi
const char* senderPassword = "asdas"; // Gönderen e-posta şifresi
const char* recipientEmail = myemail@test.com"; // Alıcı e-posta adresi
void setup() {
Serial.begin(9600);
lcd.begin();
lcd.backlight();
dht.begin();
lcd.setCursor(0, 0);
lcd.print("EKO ENDUSTRI");
delay(8000);
// Wi-Fi bağlantısını başlat
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("WiFi bağlanıyor...");
}
Serial.println("WiFi bağlantısı başarıyla kuruldu!");
Serial.print("IP adresi: ");
Serial.println(WiFi.localIP());
}
void loop() {
float temperature = dht.readTemperature();
float humidity = dht.readHumidity();
lcd.setCursor(0, 0);
lcd.print("Sicaklik: ");
lcd.print(temperature);
lcd.print("C");
lcd.setCursor(0, 1);
lcd.print("Nem: ");
lcd.print(humidity);
lcd.print("%");
if (temperature > 23) {
sendEmail("Sıcaklık Uyarısı", "Sıcaklık 23 derecenin üzerine çıktı!");
}
delay(3000);
lcd.clear();
}
void sendEmail(const char* subject, const char* body) {
WiFiClientSecure client;
client.println("From: " + String(senderEmail));
client.println("To: " + String(recipientEmail));
client.println("Subject: " + String(subject));
client.println();
client.println(body);
client.println(".");
}
have a look at arduino-send-email
although it uses ethernet it should give you some ideas
Wawa
May 31, 2023, 10:44am
3
In IDE 1.18.19,
tools>Manage Libraries>type "ESP32 Mail Client" in the search box.
Install the Mobizt library..
You should now have email examples in the IDE.
Leo..
Hello,
I'm new to this business, where I work asked me to do something like this. I downloaded the ESP32 Mail Client library but I don't know how to write the code. Can you add it above the code I sent?
I tried, but when I uploaded the code to the ESP32 D1 R32 Wemos card, the mail did not come, but I was able to send a test mail via IFTTT.
Wawa
June 2, 2023, 7:12am
6
Better fix that first.
Buy an Arduino starter kit and work through the examples.
Coding en email client is not hard, but certainly not a job for a beginner.
Leo..
hello, I don't have much time to relearn, so I asked for help on the forum. I measure values such as temperature, humidity, water level. All I want is to be able to send them via e-mail.
Wawa
June 2, 2023, 8:37am
8
Post the code that you have tried.
Did you generate an app password for your email account, and did you use that in your code.
Leo..
Yes, I entered the e-mail and password that we use to send mail from the printer within the company. My code is as below
#include <LiquidCrystal_I2C.h>
#include <WiFi.h>
#include <WiFiClientSecure.h>
#include "MQ135.h"
#include "DHT.h"
const char* ssid = "asd";
const char* password = "asd";
// SMTP sunucu ve kimlik bilgileri
const char* smtpServer = "smtp.office365.com";
const int smtpPort = 25;
const char* smtpUsername = "test@test.com";
const char* smtpPassword = "test";
#define DHTPIN 17
#define DHTTYPE DHT11
#define MQ135PIN 34
MQ135 mq135(MQ135PIN);
LiquidCrystal_I2C lcd(0x27, 16, 2);
DHT dht(DHTPIN, DHTTYPE);
WiFiClientSecure client;
const int analogInPin = 14; // HW-038 sensörünü analog pine bağlayın
int sensorValue = 0; // sensör verisi
float voltage = 0; // sensör voltajı
float waterLevel = 0; // su seviyesi
float minVoltage = 0.20; // sensör minimum voltajı
float maxVoltage = 4.00; // sensör maksimum voltajı
void setup() {
Serial.begin(9600);
// Wi-Fi bağlantısı
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
Serial.print("IP Address: ");
Serial.println(WiFi.localIP());
dht.begin();
lcd.begin();
lcd.backlight();
dht.begin();
lcd.setCursor(0, 0);
lcd.print("EKO ENDUSTRI");
delay(8000);
}
void sendEmail() {
if (client.connect(smtpServer, smtpPort)) {
// E-posta gönderme işlemi
String senderAddress = "sender@test.com";
String recipientAddress = "recipient@test.com";
String subject = "Test Email";
String body = "This is a test email sent from ESP32 D1 R32!";
client.print("MAIL FROM:<");
client.print(senderAddress);
client.println(">");
client.print("RCPT TO:<");
client.print(recipientAddress);
client.println(">");
client.println("DATA");
client.print("From: ");
client.println(senderAddress);
client.print("To: ");
client.println(recipientAddress);
client.print("Subject: ");
client.println(subject);
client.println();
client.println(body);
client.println(".");
// E-posta gönderme sonlandırma
client.stop();
Serial.println("Email sent successfully!");
} else {
Serial.println("Could not connect to SMTP server");
}
}
void loop() {
float temperature = dht.readTemperature();
float humidity = dht.readHumidity();
int air_quality = mq135.getPPM();
sensorValue = analogRead(analogInPin); // analog pinden veri oku
voltage = sensorValue * (3.3 / 4095.0); // voltaj hesapla
if (voltage > minVoltage) { // minimum voltaj kontrolü
waterLevel = 100; // su seviyesi 0%
} else if (voltage < maxVoltage) { // maksimum voltaj kontrolü
waterLevel = 0; // su seviyesi 100%
} else {
waterLevel = 100 - (voltage - minVoltage) * 100 / (maxVoltage - minVoltage); // su seviyesi hesapla
}
lcd.setCursor(0, 0);
lcd.print("Sicaklik: ");
lcd.print(temperature);
lcd.print("C");
lcd.setCursor(0, 1);
lcd.print("Nem: ");
lcd.print(humidity);
lcd.print("%");
delay(3000);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Hava:");
lcd.print(air_quality/100);
lcd.print("ppm");
if (air_quality > 1000) {
Serial.print("Gaz Uyarisi:");
Serial.print(air_quality);
}
lcd.setCursor(0, 1);
lcd.print("Su seviyesi: ");
lcd.print(waterLevel);
lcd.print("%");
delay(3000);
lcd.clear();
if (temperature > 23.0) {
sendEmail();
}
delay(30000);
}
Wawa
June 2, 2023, 10:58am
10
emre3570:
smtpPort = 25
Shouldn't that be 587.
Leo..
Wawa:
Shouldn't that be 587.
I tried it as 587. The mail did not go. Is there a library I need to add? Or do I need to add something to the code?
Wawa
June 2, 2023, 10:46pm
12
Did you install the "ESP32 Mail Client" (post#3), and did you try one of the basic examples that come with the library. It's always best to start simple, and add the things you want one at the time.
Leo..
Email sending works flawlessly. But WiFi.begin(ssid, password); Because of the command, my water level value is 100. how can i fix this problem
#include "ESP32_MailClient.h"
#include "MQ135.h"
#include "DHT.h"
#include <LiquidCrystal_I2C.h>
const char* ssid = "test";
const char* password = "test";
#define emailSenderAccount "sdf@sdfsf.com"
#define emailSenderPassword "dgfhdh"
#define emailRecipient "sdf.ghf@fgd.com"
#define smtpServer "smtp.office365.com"
#define smtpServerPort 587
#define emailSubject "IT ODASI UYARI"
#define DHTPIN 17
#define DHTTYPE DHT11
#define MQ135PIN 34
MQ135 mq135(MQ135PIN);
LiquidCrystal_I2C lcd(0x27, 16, 2);
DHT dht(DHTPIN, DHTTYPE);
SMTPData smtpData;
const int analogInPin = 14; // HW-038 sensörünü analog pine bağlayın
int sensorValue = 0; // sensör verisi
float voltage = 0; // sensör voltajı
float waterLevel = 0; // su seviyesi
float minVoltage = 0.20; // sensör minimum voltajı
float maxVoltage = 4.00; // sensör maksimum voltajı
void sendCallback(SendStatus info);
void setup(){
Serial.begin(9600);
Serial.println();
Serial.print("Connecting");
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(200);
}
Serial.println();
Serial.println("WiFi connected.");
Serial.println();
Serial.println("Preparing to send email");
Serial.println();
dht.begin();
lcd.begin();
lcd.backlight();
dht.begin();
lcd.setCursor(0, 0);
lcd.print("sdf sdf");
delay(8000);
}
void loop() {
float temperature = dht.readTemperature();
float humidity = dht.readHumidity();
int air_quality = mq135.getPPM();
sensorValue = analogRead(analogInPin); // analog pinden veri oku
voltage = sensorValue * (3.3 / 4095.0); // voltaj hesapla
if (voltage > minVoltage) { // minimum voltaj kontrolü
waterLevel = 100; // su seviyesi 0%
} else if (voltage < maxVoltage) { // maksimum voltaj kontrolü
waterLevel = 0; // su seviyesi 100%
} else {
waterLevel = 100 - (voltage - minVoltage) * 100 / (maxVoltage - minVoltage); // su seviyesi hesapla
}
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Sicaklik: ");
lcd.print(temperature);
lcd.print("C");
lcd.setCursor(0, 1);
lcd.print("Nem: ");
lcd.print(humidity);
lcd.print("%");
delay(5000);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Hava:");
lcd.print(air_quality/1000);
lcd.print("ppm");
lcd.setCursor(0, 1);
lcd.print("Su seviyesi: ");
lcd.print(waterLevel);
lcd.print("%");
delay(5000);
lcd.clear();
if(temperature > 27.0)
{
smtpData.setLogin(smtpServer, smtpServerPort, emailSenderAccount, emailSenderPassword);
smtpData.setSender("ESP32", emailSenderAccount);
smtpData.setPriority("High");
smtpData.setSubject(emailSubject);
smtpData.setMessage("<div><h1 style=\"color:red;\">Sıcaklık Uyarısı!</h1><p>Lokasyon:EKO ENDUSTRI IT ODASI</p><p>Sensör:Sıcaklık</p><p> Sıcaklık Değeri:23 °C Üzerinde </p></div>", true);
smtpData.addRecipient(emailRecipient);
smtpData.setSendCallback(sendCallback);
if (!MailClient.sendMail(smtpData))
Serial.println("Error sending Email, " + MailClient.smtpErrorReason());
}
smtpData.empty();
if(humidity > 65.0)
{
smtpData.setLogin(smtpServer, smtpServerPort, emailSenderAccount, emailSenderPassword);
smtpData.setSender("ESP32", emailSenderAccount);
smtpData.setPriority("High");
smtpData.setSubject(emailSubject);
smtpData.setMessage("<div><h1 style=\"color:red;\">Nem Uyarısı!</h1><p>Lokasyon:EKO ENDUSTRI IT ODASI</p><p>Sensör:Nem</p><p>Nem Değeri:65(%RH) Değerinin Üzerinde</p></div>", true);
smtpData.addRecipient(emailRecipient);
smtpData.setSendCallback(sendCallback);
if (!MailClient.sendMail(smtpData))
Serial.println("Error sending Email, " + MailClient.smtpErrorReason());
}
smtpData.empty();
if(air_quality/1000 > 2500.0)
{
smtpData.setLogin(smtpServer, smtpServerPort, emailSenderAccount, emailSenderPassword);
smtpData.setSender("ESP32", emailSenderAccount);
smtpData.setPriority("High");
smtpData.setSubject(emailSubject);
smtpData.setMessage("<div><h1 style=\"color:red;\">Hava Kalite Uyarısı!</h1><p>Lokasyon:EKO ENDUSTRI IT ODASI</p><p>Sensör:Hava Kalitesi</p><p>Hava Kalitesi:4000 ppm Değerinin Üzerinde</p></div>", true);
smtpData.addRecipient(emailRecipient);
smtpData.setSendCallback(sendCallback);
if (!MailClient.sendMail(smtpData))
Serial.println("Error sending Email, " + MailClient.smtpErrorReason());
}
smtpData.empty();
if(waterLevel < 99)
{
smtpData.setLogin(smtpServer, smtpServerPort, emailSenderAccount, emailSenderPassword);
smtpData.setSender("ESP32", emailSenderAccount);
smtpData.setPriority("High");
smtpData.setSubject(emailSubject);
smtpData.setMessage("<div><h1 style=\"color:red;\">Su Seviyesi Uyarısı!</h1><p>Lokasyon:EKO ENDUSTRI IT ODASI</p><p>Sensör:Su</p><p>Su değeri:Belirlenen Seviyenin Üzerinde</p></div>", true);
smtpData.addRecipient(emailRecipient);
smtpData.setSendCallback(sendCallback);
if (!MailClient.sendMail(smtpData))
Serial.println("Error sending Email, " + MailClient.smtpErrorReason());
}
smtpData.empty();
}
void sendCallback(SendStatus msg) {
Serial.println(msg.info());
float temperature = dht.readTemperature();
float humidity = dht.readHumidity();
int air_quality = mq135.getPPM();
sensorValue = analogRead(analogInPin); // analog pinden veri oku
voltage = sensorValue * (3.3 / 4095.0); // voltaj hesapla
if (voltage > minVoltage) { // minimum voltaj kontrolü
waterLevel = 100; // su seviyesi 0%
} else if (voltage < maxVoltage) { // maksimum voltaj kontrolü
waterLevel = 0; // su seviyesi 100%
} else {
waterLevel = 100 - (voltage - minVoltage) * 100 / (maxVoltage - minVoltage); // su seviyesi hesapla
}
lcd.setCursor(0, 0);
lcd.print("Sicaklik: ");
lcd.print(temperature);
lcd.print("C");
lcd.setCursor(0, 1);
lcd.print("Nem: ");
lcd.print(humidity);
lcd.print("%");
delay(5000);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Hava:");
lcd.print(air_quality/1000);
lcd.print("ppm");
lcd.setCursor(0, 1);
lcd.print("Su seviyesi: ");
lcd.print(waterLevel);
lcd.print("%");
delay(5000);
lcd.clear();
if (msg.success())
{
lcd.setCursor(0, 0);
lcd.print("UYARI MAILI GONDERILIYOR");
lcd.setCursor(0, 1);
lcd.print("GONDERILIYOR");
delay(5000);
lcd.clear();
}
}
Wawa
June 6, 2023, 9:16pm
14
Don't know enough to properly help you with this.
I do know that ADC2 (pin14) has restrictions with WiFi.
Try to use ADC1 (32-39).
Maybe safer to force the processor to calculate with floats.
waterLevel = 100.0 - (voltage - minVoltage) * 100.0 / (maxVoltage - minVoltage);
Leo..
How can WiFi.begin() work when #include <WiFi.h>
is missing (post#13)?
How can Email sending work flawlessly when #include <WiFi.h>
(post#13) is not being loaded?
Using the 2nd ADC on the ESP32 will be quite complicated when WiFi is being used. Use the ADC pins of the 1st ADC with the ESP32's Arduino core.
Was a resistor used to connect the MQ135, Connect MQ135 Air Quality Sensor and ESP32 to the Cloud over MQTT | AskSensors Blog ?
system
Closed
December 3, 2023, 9:53pm
16
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.