#include <WiFi.h>
#include <FirebaseESP32.h>
#include "time.h"
#include <ESP_Mail_Client.h>
// LDR Characteristics
const float GAMMA = 0.7;
const float RL10 = 50;
const char* ntpServer = "pool.ntp.org";
const long gmtOffset_sec = 21600;
const int daylightOffset_sec = 3600;
int val;
int valNew;
String pathData = "data";
String path = "/";
String pathTime = "/time";
String pathLux = "/lux";
int i = 0;
#define FIREBASE_HOST "https://projectiot-50bf2-default-rtdb.firebaseio.com/"
#define FIREBASE_AUTH "AIzaSyBKsntCGEPvdU04evvxlRC2nedNvVmUNf0"
#define SMTP_HOST "smtp.gmail.com"
#define SMTP_PORT 465
/* The sign in credentials */
#define AUTHOR_EMAIL "[email protected]"
#define AUTHOR_PASSWORD "wixbzxgzxnmnemtr"
/* Recipient's email*/
#define RECIPIENT_EMAIL "[email protected]"
/* The SMTP Session object used for Email sending */
SMTPSession smtp;
void smtpCallback(SMTP_Status status);
WiFiClient wifiClient;
FirebaseData firebaseData;
FirebaseESP32 firebase;
FirebaseJson firebaseJson;
void setup() {
Serial.begin(9600);
Serial.print("Connecting to WiFi");
WiFi.begin("Wokwi-GUEST", "", 6);
while (WiFi.status() != WL_CONNECTED) {
delay(100);
Serial.print(".");
}
Serial.println(" Connected!");
// Init and get the time
configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);
pinMode(2, OUTPUT);
smtp.debug(1);
smtp.callback(smtpCallback);
/* Declare the session config data */
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;
session.login.user_domain = "";
/* Declare the message class */
SMTP_Message message;
/* Set the message headers */
message.sender.name = "ESP";
message.sender.email = AUTHOR_EMAIL;
message.subject = "ESP Test Email";
message.addRecipient("Sara", RECIPIENT_EMAIL);
//Send raw text message
String textMsg = "Hello World! - Sent from ESP board";
message.text.content = textMsg.c_str();
message.text.charSet = "us-ascii";
message.text.transfer_encoding = Content_Transfer_Encoding::enc_7bit;
if (!smtp.connect(&session))
return;
/* Start sending Email and close the session */
if (!MailClient.sendMail(&smtp, &message))
Serial.println("Error sending Email, " + smtp.errorReason());
}
void loop() {
struct tm timeinfo;
if(!getLocalTime(&timeinfo)){
Serial.println("Failed to obtain time");
return;
}
int analogValue = analogRead(34);
float voltage = analogValue / 4095. * 5;
float resistance = 2000 * voltage / (1 - voltage / 5);
float lux = pow(RL10 * 1e3 * pow(10, GAMMA) / resistance, (1 / GAMMA));
val = analogRead(32);
analogWrite(2, val);
if(lux>250)
{
if(timeinfo.tm_hour >= 9){
i = i + 1;
val = analogRead(32);
analogWrite(32,0);
analogWrite(2, 0);
Firebase.begin(FIREBASE_HOST, FIREBASE_AUTH);
Serial.println(timeinfo.tm_hour);
// Ghi một giá trị dạng chuỗi lên Firebase
Firebase.RTDB.setInt(&firebaseData, pathData + path + i + pathTime, timeinfo.tm_hour);
Firebase.RTDB.setInt(&firebaseData, pathData + path + i + pathLux, lux);
}
}
delay(5000);
}
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");
}
}