//This is like a smart door lock which can be used with blynk and the date and time of opening and closing the door can also be recorded.
//With the implementation of this application you need not carry your key everywhere and have a fear of losing the key
//This can be implemented in houses where old age people stay, cause they might have a tendency to forget things, so keeping a tab of the timing can help them.
//On top of this we can even make this lock even more smarter by adding features like NFC, which will help in taping and opening the lock, we can add alerts to notify whether they have left it open or not, enhance home automation as well.
// Instead of giving duplicate keys to someone we can crate a temporary password which can be used for a certain time, set by the user.
#define BLYNK_TEMPLATE_ID "TMPL34wSWSs-J"
#define BLYNK_TEMPLATE_NAME "switch"
#define BLYNK_AUTH_TOKEN "KlqDstYjy4zLgdA7YkHx9MX9p06I7zUm"
#define BLYNK_PRINT Serial
#include <WiFi.h>
#include <BlynkSimpleEsp32.h>
#include <ESP32Servo.h>
#include <Wire.h> // Include Wire library for I2C communication
#include <RTClib.h> // Include RTC library for RTC functionalities
#include <Adafruit_MQTT.h>
#include <Adafruit_MQTT_Client.h>
char auth[] = BLYNK_AUTH_TOKEN;
char ssid[] = "Wokwi-GUEST";
char pass[] = "";
Servo lockServo;
RTC_DS3231 rtc; // Create an instance of the RTC_DS3231 class
#define AIO_SERVER "io.adafruit.com"
#define AIO_SERVERPORT 1883
#define AIO_USERNAME "justvarma"
#define AIO_KEY "aio_DFAG18zD7bA7neBjW1gBwyt6bBet"
WiFiClient client;
Adafruit_MQTT_Client mqtt(&client, AIO_SERVER, AIO_SERVERPORT, AIO_USERNAME, AIO_KEY);
// Adafruit MQTT topics
#define TOPIC_LOCK_STATE "justvarma/feeds/lock_state"
void setup() {
Serial.begin(115200);
Serial.println("Connecting to Wi-Fi...");
WiFi.begin(ssid, pass);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("\nConnected to Wi-Fi.");
Blynk.begin(auth, ssid, pass);
Serial.println("Connected to Blynk.");
lockServo.attach(13); // Attach servo to GPIO 13
Serial.println("Servo attached.");
if (!rtc.begin()) { // Initialize the RTC module
Serial.println("Couldn't find RTC");
while (1);
}
if (rtc.lostPower()) { // If the RTC lost power, set the time to the last known time
Serial.println("RTC lost power, setting time from last saved time");
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
}
// Connect to Adafruit MQTT
connectToMQTT();
}
void loop() {
Blynk.run();
mqttProcess();
}
void mqttProcess() {
Adafruit_MQTT_Client *mqttClient = &mqtt;
// Ensure the connection to Adafruit MQTT is alive
if (!mqttClient->connected()) {
connectToMQTT();
}
mqttClient->processPackets(5000); // Process MQTT packets
}
void connectToMQTT() {
Serial.print("Connecting to MQTT...");
int8_t ret;
while ((ret = mqtt.connect()) != 0) { // Connect to Adafruit MQTT
Serial.println(mqtt.connectErrorString(ret));
Serial.println("Retrying MQTT connection in 5 seconds...");
mqtt.disconnect();
delay(5000);
}
Serial.println("MQTT Connected!\n\n");
}
BLYNK_WRITE(V0) { // Button Widget for Lock/Unlock
int lockState = param.asInt(); // Get state of button (0 or 1)
if (lockState == HIGH) {
lock(); // Lock the door
printTime(); // Print the current time when the door is locked
// Publish lock state and timestamp to Adafruit MQTT
publishToMQTT(TOPIC_LOCK_STATE, "locked");
} else {
unlock(); // Unlock the door
printTime(); // Print the current time when the door is unlocked
// Publish lock state and timestamp to Adafruit MQTT
publishToMQTT(TOPIC_LOCK_STATE, "unlocked");
}
}
void publishToMQTT(const char *topic, const char *data) {
if (!mqtt.connected()) {
connectToMQTT();
}
Serial.print("Publishing to MQTT topic: ");
Serial.println(topic);
if (mqtt.publish(topic, data)) {
Serial.println("Publish success!\n");
} else {
Serial.println("Publish failed\n");
}
}
void lock() {
Serial.println("Door locked.");
lockServo.write(0); // Set servo to lock position
}
void unlock() {
Serial.println("Door unlocked.");
lockServo.write(90); // Set servo to unlock position
}
void printTime() {
DateTime now = rtc.now(); // Get the current time
Serial.print("Current time: ");
Serial.print(now.year(), DEC);
Serial.print('/');
Serial.print(now.month(), DEC);
Serial.print('/');
Serial.print(now.day(), DEC);
Serial.print(' ');
Serial.print(now.hour(), DEC);
Serial.print(':');
Serial.print(now.minute(), DEC);
Serial.print(':');
Serial.print(now.second(), DEC);
Serial.print("\n\n");
}
String getCurrentTimestamp() {
DateTime now = rtc.now();
char timestamp[20];
sprintf(timestamp, "%04d-%02d-%02d %02d:%02d:%02d",
now.year(), now.month(), now.day(),
now.hour(), now.minute(), now.second());
return String(timestamp);
}