Wi-Fi Controlled Smart Lock with ESP32 and Ultrasonic Sensor
View and Edit Project In Cirkit Designer
Circuit Documentation
Summary
The circuit in question is designed to control a parcel box using an ESP32 microcontroller, which communicates with a Telegram bot for remote locking and unlocking. The system includes an HC-SR04 Ultrasonic Sensor for parcel detection, a 5V relay to control the solenoid locks, a step-down buck converter to regulate voltage, and a push switch for manual control. The circuit is powered by a 12V power supply.
Component List
HC-SR04 Ultrasonic Sensor
- Description: A sensor that measures distance using ultrasonic waves.
- Pins: VCC, TRIG, ECHO, GND
12V Power Supply
- Description: Provides the necessary power to the circuit.
- Pins: +, -
12V Solenoid Lock
- Description: An electromagnetic lock used for securing the parcel box.
- Pins: VCC, GND
Step-Down Buck Converter
- Description: Converts the input voltage to a lower output voltage.
- Pins: IN +, IN - GND, OUT +, OUT - GND
5V Relay
- Description: An electromechanical switch used to control the solenoid lock.
- Pins: Normally Open, Common terminal, Normally Closed, In, GND, VCC
ESP32 (30 Pin)
- Description: A microcontroller with Wi-Fi capabilities for controlling the circuit and communicating with the Telegram bot.
- Pins: EN, VP, VN, D34, D35, D32, D33, D25, D26, D27, D14, D12, D13, GND, Vin, D23, D22, TX0, RX0, D21, D19, D18, D5, TX2, RX2, D4, D2, D15, 3V3
2Pin Push Switch
- Description: A manual switch for controlling the circuit.
- Pins: Input +, Output +
Wiring Details
HC-SR04 Ultrasonic Sensor
- VCC connected to 5V supply from the Step-Down Buck Converter
- TRIG connected to ESP32 pin D13
- ECHO connected to ESP32 pin D25
- GND connected to common ground
12V Power Supply
-
- connected to the Step-Down Buck Converter IN + and the Common terminal of the 5V Relay
-
- connected to common ground
12V Solenoid Locks
- VCC connected to Normally Open terminal of the 5V Relay
- GND connected to common ground
Step-Down Buck Converter
- IN + connected to 12V Power Supply +
- IN - GND connected to common ground
- OUT + connected to VCC of HC-SR04, ESP32 Vin, and 5V Relay VCC
- OUT - GND connected to common ground
5V Relay
- Normally Open connected to VCC of 12V Solenoid Locks
- Common terminal connected to 12V Power Supply +
- Normally Closed not used in this circuit
- In connected to ESP32 pin D14
- GND connected to common ground
- VCC connected to 5V supply from the Step-Down Buck Converter
ESP32 (30 Pin)
- Vin connected to 5V supply from the Step-Down Buck Converter
- GND connected to common ground
- D13 connected to TRIG of HC-SR04
- D25 connected to ECHO of HC-SR04
- D14 connected to In of 5V Relay
- D12 connected to Input + of 2Pin Push Switch
2Pin Push Switch
- Input + connected to ESP32 pin D12
- Output + connected to common ground
Documented Code
#include <WiFi.h>
#include <UniversalTelegramBot.h>
#include <WiFiClientSecure.h>
// Wi-Fi credentials
const char* ssid = "your-SSID";
const char* password = "your-PASSWORD";
// Telegram bot details
const String BOT_TOKEN = "your-Telegram-Bot-Token";
const String CHAT_ID = "your-Telegram-Chat-ID";
// Relay and ultrasonic sensor pins
#define RELAY_PIN 14
#define TRIG_PIN 13
#define ECHO_PIN 12
// Time between checking the ultrasonic sensor (milliseconds)
#define CHECK_INTERVAL 1000
WiFiClientSecure client;
UniversalTelegramBot bot(BOT_TOKEN, client);
// Variables for distance measurement
long duration;
float distance;
unsigned long lastCheck = 0;
bool isLocked = true;
// Function to connect to Wi-Fi
void connectToWiFi() {
Serial.print("Connecting to Wi-Fi");
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("\nConnected to Wi-Fi.");
}
// Function to measure distance using ultrasonic sensor
float getDistance() {
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
duration = pulseIn(ECHO_PIN, HIGH);
distance = duration * 0.034 / 2; // Convert time to distance in cm
return distance;
}
// Function to control the solenoid lock (lock/unlock)
void controlLock(bool lock) {
if (lock) {
digitalWrite(RELAY_PIN, LOW); // Lock (LOW to activate relay)
isLocked = true;
Serial.println("Box Locked.");
} else {
digitalWrite(RELAY_PIN, HIGH); // Unlock (HIGH to deactivate relay)
isLocked = false;
Serial.println("Box Unlocked.");
}
}
// Function to handle incoming Telegram commands
void handleTelegramMessages(int numNewMessages) {
for (int i = 0; i < numNewMessages; i++) {
String text = bot.messages[i].text;
String from = bot.messages[i].from_name;
Serial.println("Message received from: " + from);
if (text == "/lock") {
controlLock(true);
bot.sendMessage(CHAT_ID, "The parcel box is now locked.", "");
} else if (text == "/unlock") {
controlLock(false);
bot.sendMessage(CHAT_ID, "The parcel box is now unlocked.", "");
} else {
bot.sendMessage(CHAT_ID, "Unknown command. Use /lock or /unlock.", "");
}
}
}
void setup() {
// Initialize serial communication
Serial.begin(115200);
// Set up relay and ultrasonic sensor pins
pinMode(RELAY_PIN, OUTPUT);
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
// Set initial lock state
controlLock(true); // Start by locking the box
// Connect to Wi-Fi
connectToWiFi();
// Connect to Telegram
client.setInsecure();
bot.sendMessage(CHAT_ID, "Parcel box system is online.", "");
}
void loop() {
// Handle incoming Telegram messages
int numNewMessages = bot.getUpdates(bot.last_message_received + 1);
while (numNewMessages) {
handleTelegramMessages(numNewMessages);
numNewMessages = bot.getUpdates(bot.last_message_received + 1);
}
// Check the distance at regular intervals
unsigned long currentMillis = millis();
if (currentMillis - lastCheck >= CHECK_INTERVAL) {
lastCheck = currentMillis;
float distance = getDistance();
Serial.println("Distance: " + String(distance) + " cm");
if (distance < 10 && isLocked) { // Adjust the threshold distance for detecting parcels
Serial.println("Parcel detected!");
bot.sendMessage(CHAT_ID, "Parcel has been delivered!", "");
}
}
delay(100); // Delay to avoid spamming messages
}
This code is designed to run on an ESP32 microcontroller. It connects to Wi-Fi and listens for commands from a Telegram bot to lock or unlock a parcel box. It also periodically checks the distance to an object using an ultrasonic sensor and sends a notification when a parcel is detected. The relay is controlled based on the commands received or the sensor's input.