#include <WiFi.h>
#include <PubSubClient.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Keypad.h>
const char* ssid = "Wokwi-GUEST"; // Change this to your WiFi network name
const char* password = ""; // Change this to your WiFi network password
const char* mqtt_server = "broker.hivemq.com";
const int mqtt_port = 1883;
const char* temperature_topic = "worker_temperature01";
const char* motion_topic = "motion_detection";
const char* door_unlock_topic = "door_unlock"; // MQTT topic for door unlock
LiquidCrystal_I2C lcd(0x27, 16, 2);
const int ledPin = 2;
const int PIR_PIN = 4;
WiFiClient espClient;
PubSubClient client(espClient);
unsigned long previousMillis = 0;
const long interval = 5000; // Refresh interval for temperature (5 seconds)
bool motionDetected = false;
int flashCount = 0;
const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
char keys[ROWS][COLS] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
byte rowPins[ROWS] = {13, 12, 14, 27}; // R1, R2, R3, R4 pins
byte colPins[COLS] = {26, 25, 33, 32}; // C1, C2, C3, C4 pins
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
String enteredCode = "";
bool codeEntered = false;
void setup_wifi() {
Serial.println("Connecting to WiFi...");
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("WiFi Connected.");
}
void mqtt_callback(char* topic, byte* payload, unsigned int length) {
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("] ");
char msg[length + 1];
for (unsigned int i = 0; i < length; i++) {
msg[i] = (char)payload[i];
}
msg[length] = '\0';
Serial.println(msg);
if (strcmp(topic, temperature_topic) == 0) {
// Display temperature on the first line of the LCD
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Temp: ");
lcd.print(msg);
lcd.print(" C");
} else if (strcmp(topic, motion_topic) == 0) {
if (strcmp(msg, "Motion Detected") == 0) {
// Display motion detection message on the second line of the LCD
lcd.clear();
lcd.setCursor(0, 1);
lcd.print("Motion Detected");
motionDetected = true;
flashCount = 0; // Reset the flash count
}
}
}
void reconnect() {
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
if (client.connect("ESP32_LCD_Display")) {
Serial.println("connected");
client.subscribe(temperature_topic);
client.subscribe(motion_topic);
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
delay(5000);
}
}
}
void setup() {
Serial.begin(115200);
setup_wifi();
client.setServer(mqtt_server, mqtt_port);
client.setCallback(mqtt_callback);
lcd.init();
lcd.backlight();
pinMode(ledPin, OUTPUT);
// Initialize LCD with a welcome message
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Enter Code:");
}
void loop() {
if (!client.connected()) {
reconnect();
}
client.loop();
unsigned long currentMillis = millis();
// Check for motion detection
if (motionDetected) {
if (flashCount < 5) {
if (currentMillis - previousMillis >= 500) {
previousMillis = currentMillis;
digitalWrite(ledPin, !digitalRead(ledPin)); // Toggle the LED state
flashCount++;
}
} else {
// Motion detected, flashed 5 times, reset
motionDetected = false;
digitalWrite(ledPin, LOW); // Turn off the LED
}
}
// Check for keypad input
char key = keypad.getKey();
if (key != NO_KEY) {
if (key == '#') {
// Check if the entered code is correct
if (enteredCode == "1234") {
// Send MQTT message to unlock the door
client.publish(door_unlock_topic, "unlock");
// Display "Welcome Home" on the LCD
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Welcome Home");
delay(2000); // Display for 2 seconds
// Reset the LCD to the initial state
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Enter Code:");
} else {
// Display "Wrong Password" on the LCD
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Wrong Password");
delay(2000); // Display for 2 seconds
// Reset the LCD to the initial state
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Enter Code:");
}
// Clear the entered code
enteredCode = "";
} else {
// Append the key to the entered code
enteredCode += key;
// Display the entered code on the LCD
lcd.setCursor(0, 1);
lcd.print(" "); // Clear the line
lcd.setCursor(0, 1);
lcd.print(enteredCode);
}
}
// Display temperature every 5 seconds
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
client.publish(temperature_topic, "22.5"); // Replace with your temperature reading
}
}