Cirkit Designer LogoCirkit DesignerYour all-in-one circuit design IDE Launch Cirkit Designer App

Wi-Fi Controlled Smart Lock with ESP32 and Ultrasonic Sensor

View and Edit Project In Cirkit DesignerImage of Wi-Fi Controlled Smart Lock with ESP32 and Ultrasonic Sensor

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

12V Power Supply

12V Solenoid Lock

Step-Down Buck Converter

5V Relay

ESP32 (30 Pin)

2Pin Push Switch

Wiring Details

HC-SR04 Ultrasonic Sensor

12V Power Supply

12V Solenoid Locks

Step-Down Buck Converter

5V Relay

ESP32 (30 Pin)

2Pin Push Switch

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.