#include <Keypad.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <WiFi.h>
#include <WebServer.h>
// ==============================
// WIFI CHO PHÒNG ĐIỂM DANH
// ==============================
const char* ssid = "Wokwi-GUEST";
const char* password = "";
WebServer server(80);
// Thông báo từ phòng bơm
String pumpMessage = "";
// ==============================
// LCD
// ==============================
LiquidCrystal_I2C lcd(0x27, 16, 2);
// ==============================
// KEYPAD
// ==============================
const byte ROWS = 4;
const byte COLS = 4;
char keys[ROWS][COLS] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
byte rowPins[ROWS] = {32, 33, 25, 26};
byte colPins[COLS] = {27, 14, 12, 13};
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
// ==============================
// PINS
// ==============================
const int LDR_PIN = 34;
const int LED_PIN = 2;
// ==============================
// LOGIC BIẾN
// ==============================
bool autoMode = true;
int lightThreshold = 300;
String currentEntry = "";
unsigned long lastLCDupdate = 0;
const unsigned long LCD_INTERVAL = 500;
// ATTENDANCE
struct Attendee {
String code;
String name;
};
Attendee attendees[] = {
{"1234", "Nguyen A"},
{"2345", "Tran B"},
{"3456", "Le C"}
};
const int NUM_KNOWN = sizeof(attendees) / sizeof(attendees[0]);
#define MAX_CHECKIN 50
String checkedInCodes[MAX_CHECKIN];
int checkedInCount = 0;
// ==============================
// SERVER HANDLER NHẬN THÔNG BÁO
// ==============================
void handleNotify() {
if (server.hasArg("n")) {
pumpMessage = server.arg("n");
Serial.print("==> THONG BAO TU PHONG BOM: ");
Serial.println(pumpMessage);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Thong bao tu");
lcd.setCursor(0,1);
if (pumpMessage == "full") {
lcd.print("Nuoc: FULL !!!");
} else if (pumpMessage == "low") {
lcd.print("Nuoc: THAP !!! ");
} else {
lcd.print("Msg: " + pumpMessage);
}
delay(2000); // Hiển thị cảnh báo 2 giây
lcd.clear();
}
server.send(200, "text/plain", "OK");
}
// ==============================
// SETUP
// ==============================
void setup() {
Serial.begin(115200);
pinMode(LED_PIN, OUTPUT);
// LCD
lcd.init();
lcd.backlight();
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Diem danh ESP32");
delay(700);
lcd.clear();
// WIFI
WiFi.begin(ssid, password);
Serial.print("Dang ket noi WiFi...");
while (WiFi.status() != WL_CONNECTED) delay(100);
Serial.println("OK!");
Serial.print("IP phong diem danh: ");
Serial.println(WiFi.localIP());
server.on("/notify", handleNotify);
server.begin();
}
// ==============================
// LOOP
// ==============================
void loop() {
server.handleClient();
// Lấy keypad
char k = keypad.getKey();
if (k) handleKey(k);
// Auto Light
int raw = analogRead(LDR_PIN);
static bool ledState = false;
if (autoMode) {
ledState = (raw < lightThreshold);
digitalWrite(LED_PIN, ledState ? HIGH : LOW);
}
// Update LCD
if (millis() - lastLCDupdate > LCD_INTERVAL) {
updateLCD(raw);
lastLCDupdate = millis();
}
}
// ==============================
// KEYPAD XỬ LÝ
// ==============================
void handleKey(char k) {
if (k == '#') {
if (currentEntry.length() == 4) {
processCode(currentEntry);
}
currentEntry = "";
}
else if (k == '*') {
currentEntry = "";
lcd.setCursor(0,1);
lcd.print("Cleared ");
}
else {
if (currentEntry.length() < 4) {
currentEntry += k;
lcd.setCursor(0,1);
lcd.print("Code: " + currentEntry);
}
}
}
// ==============================
// CHECK CODE
// ==============================
void processCode(String code) {
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Checking...");
int idx = -1;
for (int i=0;i<NUM_KNOWN;i++) {
if (attendees[i].code == code) idx = i;
}
if (idx >= 0) {
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Welcome:");
lcd.setCursor(0,1);
lcd.print(attendees[idx].name);
checkedInCodes[checkedInCount++] = code;
} else {
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Invalid code");
}
delay(1500);
lcd.clear();
}
// ==============================
// LCD UPDATE
// ==============================
void updateLCD(int raw) {
lcd.setCursor(0,0);
lcd.print("L:");
lcd.print(raw);
lcd.print(" LED:");
lcd.print(digitalRead(LED_PIN) ? "ON " : "OFF");
lcd.setCursor(0,1);
lcd.print("Mode:");
lcd.print(autoMode ? "AUTO" : "MAN");
lcd.print(" C:");
lcd.print(checkedInCount);
}