#include <WiFi.h>
#include <ESP_Mail_Client.h>
//const char* ssid = "YourWiFiSSID";
//const char* password = "YourWiFiPassword";
/*const char* smtpServer = "smtp.example.com";
const int smtpPort = 587;
const char* smtpUsername = "[email protected]";
const char* smtpPassword = "your_email_password";
*/
const int TRIG_PIN = 4;
const int ECHO_PIN = 2;
const int RELAY_PIN = 19;
const int LED_PIN = 5;
const long REPORT_INTERVAL_MS = 86400000; // 24 hours in milliseconds
ESP_Mail_Client mailClient;
unsigned long lastReportTime = 0;
void setup() {
Serial.begin(115200);
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
pinMode(RELAY_PIN, OUTPUT);
pinMode(LED_PIN, OUTPUT);
//connectToWiFi();
//setupSMTP();
}
/*void loop() {
unsigned long currentTime = millis();
// Check if it's time to send a report
if (currentTime - lastReportTime >= REPORT_INTERVAL_MS) {
sendReport();
lastReportTime = currentTime;
}*/
// Measure oil level
float oilLevel = measureOilLevel();
// Control relay and LED based on oil level
if (oilLevel > 20) {
digitalWrite(RELAY_PIN, HIGH); // Activate relay if oil level is sufficient
digitalWrite(LED_PIN, HIGH); // Turn on LED
} else {
digitalWrite(RELAY_PIN, LOW); // Deactivate relay if oil level is low
digitalWrite(LED_PIN, LOW); // Turn off LED
}
delay(1000);
}
/*void connectToWiFi() {
Serial.println("Connecting to WiFi...");
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting...");
}
Serial.println("Connected to WiFi!");
}*/
float measureOilLevel() {
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
float duration = pulseIn(ECHO_PIN, HIGH);
float distance = duration * 0.034 / 2;
return distance;
}
/*void setupSMTP() {
// Initialize the mail client
mailClient.init(smtpServer, smtpPort, smtpUsername, smtpPassword);
}
void sendReport() {
Serial.println("Sending report...");
String report = "Oil Level Report\n";
report += "Oil Level: " + String(measureOilLevel()) + " cm\n";
String subject = "Oil Level Report";
// Send email
//mailClient.Send(subject.c_str(), report.c_str(), smtpUsername, "[email protected]");
Serial.println("Email sent!");
}*/