#include <WiFi.h>
#include <LiquidCrystal_I2C.h>
#include <ESP_Mail_Client.h>
#include <DHT.h>
#define DHTPIN 26 // Pin connected to DHT22
#define DHTTYPE DHT22 // DHT sensor type
DHT dht(DHTPIN, DHTTYPE);
LiquidCrystal_I2C lcd(0x27, 16, 2);
#define SMTP_HOST "smtp.gmail.com"
#define SMTP_PORT 465
/* The sign in credentials */
#define AUTHOR_EMAIL ""
#define AUTHOR_PASSWORD ""
/* Recipient's email*/
#define RECIPIENT_EMAIL ""
/* The SMTP Session object used for Email sending */
SMTPSession smtp;
/* Callback function get the Email sending status */
void smtpCallback(SMTP_Status status);
void setup() {
Serial.begin(115200);
Serial.print("Connecting to WiFi");
WiFi.begin("Wokwi-GUEST", "", 6);
while (WiFi.status() != WL_CONNECTED) {
delay(100);
Serial.print(".");
}
Serial.println(" Connected!");
lcd.init();
lcd.backlight();
lcd.setCursor(2,0);
lcd.print("IOT");
lcd.setCursor(0,1);
lcd.print("CW2");
}
void loop() {
float humidity = dht.readHumidity(); //get value of humidity
float temperature = dht.readTemperature(); //get value of temperature
Serial.println("Temperature:");
Serial.println(temperature);
Serial.println("degrees celcius");
Serial.println("Humidity");
Serial.println(humidity);
Serial.println("%");
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Temp: " + String(temperature, 2) + " C");
lcd.setCursor(0,1);
lcd.print("Humidity: " + String(humidity, 1) + "%");
delay(3000);
if (temperature > 40) {
SendEmail("Temperature was over 40 degrees celcius!", "Alert! high Temperature.");
Serial.println("High temperature message sent");
}
if (humidity < 60) {
SendEmail("Humidity was over 60%!", "Alert! high Humidity.");
Serial.println("High humidity message sent");
Serial.println("==================================");
}
}
void SendEmail(String htmlMsg, String MessageSubject)
{
smtp.debug(1);
/* Set the callback function to get the sending results */
smtp.callback(smtpCallback);
/* Declare the ESP_Mail_Session for user defined session credentials */
ESP_Mail_Session session;
/* Set the session config */
session.server.host_name = SMTP_HOST;
session.server.port = SMTP_PORT;
session.login.email = AUTHOR_EMAIL;
session.login.password = AUTHOR_PASSWORD;
/* Set the NTP config time */
session.time.ntp_server = F("pool.ntp.org,time.nist.gov");
session.time.gmt_offset = -5;
session.time.day_light_offset = 0;
/* Declare the message class */
SMTP_Message message;
/* Set the message headers */
message.sender.name = "CW2 trial";
message.sender.email = AUTHOR_EMAIL;
message.subject = MessageSubject;
message.addRecipient("receiver1", RECIPIENT_EMAIL);
message.html.content = htmlMsg;
message.html.charSet = F("us-ascii");
message.html.transfer_encoding = Content_Transfer_Encoding::enc_7bit;
message.priority = esp_mail_smtp_priority::esp_mail_smtp_priority_normal;
/* Connect to the server */
if (!smtp.connect(&session /* session credentials */))
Serial.println("Error connecting to SMTP server, " + smtp.errorReason());
/* Start sending Email and close the session */
if (!MailClient.sendMail(&smtp, &message))
Serial.println("Error sending Email, " + smtp.errorReason());
}
/* Callback function to get the Email sending status */
void smtpCallback(SMTP_Status status){
/* Print the current status */
Serial.println(status.info());
/* Print the sending result */
if (status.success()){
Serial.println("----------------");
ESP_MAIL_PRINTF("Message sent success: %d\n", status.completedCount());
ESP_MAIL_PRINTF("Message sent failled: %d\n", status.failedCount());
Serial.println("----------------\n");
struct tm dt;
for (size_t i = 0; i < smtp.sendingResult.size(); i++){
/* Get the result item */
SMTP_Result result = smtp.sendingResult.getItem(i);
time_t ts = (time_t)result.timestamp;
localtime_r(&ts, &dt);
ESP_MAIL_PRINTF("Message No: %d\n", i + 1);
ESP_MAIL_PRINTF("Status: %s\n", result.completed ? "success" : "failed");
ESP_MAIL_PRINTF("Date/Time: %d/%d/%d %d:%d:%d\n", dt.tm_year + 1900, dt.tm_mon + 1, dt.tm_mday, dt.tm_hour, dt.tm_min, dt.tm_sec);
ESP_MAIL_PRINTF("Recipient: %s\n", result.recipients.c_str());
ESP_MAIL_PRINTF("Subject: %s\n", result.subject.c_str());
}
Serial.println("----------------\n");
smtp.sendingResult.clear();
}
}