#include <LiquidCrystal_I2C.h>
#include <ESP32_MailClient.h>
#include <WiFi.h>
#include <HTTPClient.h>
LiquidCrystal_I2C lcd(0x27, 16, 2); // Địa chỉ I2C 0x27, 16 cột và 2 hàng
const int prsPin = 35;
float flamelevel = 0; // Giá trị ánh xạ và đảo ngược % phạm vi cảm biến
const int ledPin = 5;
const int waterPin = 18;
// Tương quan thực nghiệm giữa % lửa và tình trạng lửa thực tế (TBD)
const int minSurvive = 15; // Mức tối thiểu cho trạng thái chờ đợi, dưới đó là tắt lửa
const int idleLow = 20; // Đọc thấp nhất cho trạng thái chờ đợi lành mạnh
const int idleTarget = 30; // Giá trị mục tiêu cho trạng thái chờ đợi nghỉ ngơi
const int firingLow = 70; // Đọc thấp nhất cho trạng thái đốt cháy hoạt động
const int firingHigh = 90; // Đọc cho trạng thái đốt cháy đầy đủ
#define emailSenderAccount "[email protected]"
#define emailSenderPassword "tnma juwe nxxo tlym"
#define emailRecipient "[email protected]"
#define smtpServer "74.125.143.108"
#define emailSubject "Cảnh báo cháy"
#define smtpServerPort 465
SMTPData smtpData;
const char* ssid = "Wokwi-GUEST";
const char* password = "";
bool isSentMail = false;
void setup() {
lcd.init();
lcd.clear();
lcd.backlight();
WiFi.begin(ssid, password);
Serial.begin(115200);
pinMode(ledPin, OUTPUT); // Đặt chân đèn xanh là đầu ra
pinMode(waterPin, OUTPUT);
}
void loop() {
float analogValue = analogRead(prsPin);
flamelevel = map(analogValue, 0, 1024, 100, 0);
Serial.print(flamelevel, 0);
Serial.println("%");
// Vô hiệu hóa các lệnh điều khiển màn hình để in ra Serial Monitor
lcd.setCursor(0, 0);
lcd.print(F("Flame: "));
if (flamelevel >= firingHigh) { // Đang đốt cháy đầy đủ
lcd.print("Full Fire");
digitalWrite(ledPin, HIGH); // Bật đèn xanh
delay(300);
digitalWrite(ledPin, LOW);
digitalWrite(waterPin, HIGH); // Tắt đèn xanh để nhấp nháy
if(!isSentMail)
sendMail();
}
if ((flamelevel >= firingLow) && (flamelevel < firingHigh)) { // Đang đốt cháy
lcd.print("Firing ");
digitalWrite(ledPin, HIGH); // Bật đèn xanh
digitalWrite(waterPin, HIGH);
if(!isSentMail)
sendMail();
}
if ((flamelevel < firingLow) && (flamelevel > idleLow)) { // Đốt cháy ở trạng thái chờ đợi
lcd.print("Idle fire ");
digitalWrite(waterPin, HIGH);
}
if ((flamelevel <= idleLow) && (flamelevel >= minSurvive)) { // Lửa yếu
lcd.print("Low fire ");
// Kích hoạt bộ đếm thời gian chạy đốt (ví dụ: 2 phút?)
}
if (flamelevel < minSurvive) { // Lửa tắt
lcd.print("FIRE OUT! ");
digitalWrite(ledPin, LOW); // Bật đèn xanh
digitalWrite(waterPin, LOW);
isSentMail = false;
// Gửi cảnh báo
}
lcd.setCursor(0, 1);
lcd.print(" Level: ");
lcd.print(flamelevel, 0);
lcd.print("% ");
delay(200);
}
void sendMail(){
isSentMail = true;
smtpData.setLogin(smtpServer, smtpServerPort, emailSenderAccount, emailSenderPassword);
smtpData.setSender("NodeWifi32", emailSenderAccount);
smtpData.setPriority("Fire");
smtpData.setSubject(emailSubject);
smtpData.setMessage("Đã có cháy", true);
smtpData.addRecipient(emailRecipient);
if (!MailClient.sendMail(smtpData)) {
Serial.println("Error sending Email: " + MailClient.smtpErrorReason());
isSentMail = false;
}
smtpData.empty();
}