190 lines
6.7 KiB
C++
190 lines
6.7 KiB
C++
#include <Wire.h>
|
|
#include <WiFi.h>
|
|
#include <WebServer.h>
|
|
#include <LiquidCrystal_I2C.h> // library LCD
|
|
#include <WiFiManager.h> // library WiFiManager
|
|
|
|
LiquidCrystal_I2C lcd(0x27, 16, 2); // Alamat I2C LCD: 0x27, 16 kolom, 2 baris
|
|
|
|
unsigned long startTime;
|
|
unsigned long countdownDuration = 0; // Durasi countdown dalam milidetik, awalnya 0
|
|
unsigned long additionalTime = 0; // Waktu tambahan yang diminta dari aplikasi Flutter
|
|
bool countdownActive = false; // Mulai countdown saat tombol "Set" ditekan
|
|
|
|
const int relayPin = 26; // Pin untuk mengontrol relay
|
|
const int buzzerPin = 12; // Pin untuk mengontrol buzzer
|
|
|
|
WebServer server(80);
|
|
|
|
void handleRoot() {
|
|
String html = "<!DOCTYPE html><html><head><title>ESP32 Timer Control</title></head><body>";
|
|
html += "<h1>ESP32 Timer Control</h1>";
|
|
html += "<form action='/set-timer' method='POST'>";
|
|
html += "Timer Duration (ms): <input type='text' name='timer'><br><br>";
|
|
html += "<input type='submit' value='Set'>";
|
|
html += "</form></body></html>";
|
|
|
|
server.send(200, "text/html", html);
|
|
}
|
|
|
|
void handleSetTimer() {
|
|
if (server.hasArg("timer")) {
|
|
// Dapatkan waktu tambahan dari permintaan dan tambahkan ke waktu countdown
|
|
additionalTime = server.arg("timer").toInt();
|
|
countdownDuration += additionalTime;
|
|
startTime = millis(); // Mulai waktu saat tombol "Set" ditekan
|
|
countdownActive = true; // Aktifkan countdown
|
|
server.send(200, "text/plain", "Timer set to: " + String(countdownDuration) + " ms");
|
|
} else {
|
|
server.send(400, "text/plain", "Invalid Request");
|
|
}
|
|
}
|
|
|
|
// Endpoint baru untuk mendapatkan waktu yang sedang berjalan
|
|
void handleGetTime() {
|
|
unsigned long currentTime = millis();
|
|
unsigned long elapsedTime = currentTime - startTime;
|
|
|
|
// Hitung sisa waktu dalam milidetik
|
|
unsigned long remainingTime = countdownDuration > elapsedTime ? countdownDuration - elapsedTime : 0;
|
|
|
|
// Kirim waktu yang sedang berjalan ke klien sebagai respon JSON
|
|
String response = "{\"hours\":";
|
|
response += remainingTime / 3600000;
|
|
response += ",\"minutes\":";
|
|
response += (remainingTime % 3600000) / 60000;
|
|
response += ",\"seconds\":";
|
|
response += (remainingTime % 60000) / 1000;
|
|
response += "}";
|
|
|
|
server.send(200, "application/json", response);
|
|
}
|
|
|
|
// Endpoint baru untuk mendapatkan status koneksi
|
|
void handleGetStatus() {
|
|
String status = (WiFi.status() == WL_CONNECTED) ? "Online" : "Offline";
|
|
server.send(200, "text/plain", status);
|
|
}
|
|
|
|
void handleResetTimer() {
|
|
countdownDuration = 0; // Reset durasi countdown menjadi 0
|
|
additionalTime = 0; // Reset waktu tambahan menjadi 0
|
|
countdownActive = false; // Nonaktifkan countdown
|
|
startTime = 0; // Reset nilai startTime
|
|
lcd.clear(); // Bersihkan layar LCD
|
|
lcd.setCursor(0, 0);
|
|
lcd.print("Timer Reset");
|
|
Serial.println("Timer Reset");
|
|
server.send(200, "text/plain", "Timer reset to 0 ms");
|
|
}
|
|
|
|
void setup() {
|
|
Serial.begin(115200);
|
|
Wire.begin(); // Inisialisasi komunikasi I2C
|
|
lcd.begin(16, 2); // Inisialisasi LCD dengan ukuran 16x2
|
|
lcd.backlight(); // Nyalakan backlight LCD
|
|
|
|
pinMode(relayPin, OUTPUT);
|
|
digitalWrite(relayPin, HIGH); // Pastikan relay mati saat awal
|
|
pinMode(buzzerPin, OUTPUT); // Inisialisasi pin buzzer
|
|
|
|
// Inisialisasi WiFiManager
|
|
WiFiManager wifiManager;
|
|
|
|
// Mencoba untuk terhubung ke jaringan WiFi yang dikonfigurasi sebelumnya
|
|
if (!wifiManager.autoConnect("ESP EV", "espev123")) {
|
|
Serial.println("Failed to connect and hit timeout");
|
|
// Reset dan mencoba kembali untuk membuat koneksi
|
|
ESP.restart();
|
|
}
|
|
|
|
Serial.println("Connected to WiFi!");
|
|
Serial.println("IP address: " + WiFi.localIP().toString());
|
|
|
|
// Tambahkan kode untuk menampilkan alamat IP ke layar LCD setelah koneksi WiFi berhasil
|
|
lcd.clear();
|
|
lcd.setCursor(0, 0);
|
|
lcd.print("ESP EV :)");
|
|
lcd.setCursor(0, 1);
|
|
lcd.print(WiFi.localIP());
|
|
|
|
server.on("/", HTTP_GET, handleRoot);
|
|
server.on("/set-timer", HTTP_POST, handleSetTimer);
|
|
server.on("/get-time", HTTP_GET, handleGetTime); // Tambahkan endpoint baru
|
|
server.on("/reset-timer", HTTP_GET, handleResetTimer); // Tambahkan endpoint reset timer
|
|
server.on("/status", HTTP_GET, handleGetStatus); // Tambahkan endpoint get status
|
|
server.on("/find", HTTP_GET, []() {
|
|
server.send(200, "text/plain", "Hello from ev :)");
|
|
}); // endpoint untuk find esp32
|
|
|
|
server.begin();
|
|
}
|
|
|
|
void loop() {
|
|
server.handleClient();
|
|
|
|
if (countdownActive) {
|
|
unsigned long currentTime = millis();
|
|
unsigned long elapsedTime = currentTime - startTime;
|
|
|
|
// Hitung sisa waktu dalam milidetik
|
|
unsigned long remainingTime = countdownDuration > elapsedTime ? countdownDuration - elapsedTime : 0;
|
|
|
|
// Tampilkan countdown pada LCD
|
|
lcd.setCursor(0, 0);
|
|
lcd.print("Countdown Timer");
|
|
lcd.setCursor(0, 1);
|
|
lcd.print("Time: ");
|
|
lcd.print(remainingTime / 3600000);
|
|
lcd.print("h ");
|
|
lcd.print((remainingTime % 3600000) / 60000);
|
|
lcd.print("m ");
|
|
lcd.print((remainingTime % 60000) / 1000);
|
|
lcd.print("s ");
|
|
|
|
Serial.print("Time: ");
|
|
Serial.print(remainingTime / 3600000);
|
|
Serial.print("h ");
|
|
Serial.print((remainingTime % 3600000) / 60000);
|
|
Serial.print("m ");
|
|
Serial.print((remainingTime % 60000) / 1000);
|
|
Serial.println("s ");
|
|
|
|
if (remainingTime <= 0) {
|
|
// Waktu habis, matikan relay, aktifkan buzzer, dan hentikan countdown
|
|
countdownDuration = 0; // Reset durasi countdown menjadi 0
|
|
additionalTime = 0; // Reset waktu tambahan menjadi 0
|
|
countdownActive = false; // Nonaktifkan countdown
|
|
startTime = 0; // Reset nilai startTime
|
|
|
|
digitalWrite(relayPin, LOW); // Matikan arus dengan relay
|
|
|
|
for (int i = 0; i < 3; i++) {
|
|
tone(buzzerPin, 1000); // Aktifkan buzzer dengan frekuensi 1000Hz
|
|
delay(1000); // Bunyi buzzer aktif selama 1 detik
|
|
noTone(buzzerPin); // Matikan bunyi buzzer setelah 1 detik
|
|
delay(500); // Berhenti sebentar sebelum bunyi lagi
|
|
}
|
|
|
|
lcd.clear();
|
|
lcd.setCursor(0, 0);
|
|
lcd.print("Time's up!");
|
|
Serial.println("Time's up!");
|
|
} else {
|
|
// Waktu masih berjalan, hidupkan relay
|
|
digitalWrite(relayPin, HIGH); // Hidupkan arus dengan relay
|
|
}
|
|
} else {
|
|
// Jika countdown tidak aktif, pastikan relay dimatikan
|
|
digitalWrite(relayPin, LOW); // Matikan arus dengan relay
|
|
|
|
// Tampilkan kembali alamat IP ESP32 setelah countdown dihentikan
|
|
lcd.setCursor(0, 0);
|
|
lcd.print("ESP EV :) ");
|
|
lcd.setCursor(0, 1);
|
|
lcd.print(WiFi.localIP());
|
|
}
|
|
|
|
delay(100); // Kurangi delay menjadi 100 ms untuk meningkatkan responsivitas
|
|
}
|