This IoT Project is submitted by Rohan Barnwal for IoT Contest organized by Quartz Components.
This home automation project integrates facial recognition, a Telegram bot interface, and a 4-channel relay module to automate room lighting. It can automatically control basic room lighting and allow manual control through a Telegram bot interface. When someone enters the room, it turns on basic lighting. If no one is in the room, it asks whether to keep the lights on or turn them off via Telegram.
- Arduino IDE
- OpenCV: Utilized for webcam access and image processing.
- dlib: Provides face detection and facial landmarks.
- Telebot: A Python library for Telegram bot creation.
- Serial: Used for serial communication with the Arduino.
Connect the Arduino to your computer via USB. Wire the 4-channel relay module to the Arduino as follows:
Relay Pin "light1" → Arduino Pin D2.
Relay Pin "light2" → Arduino Pin D3.
Relay Pin "light3" → Arduino Pin D4.
Relay Pin "light4" → Arduino Pin D5.
Note: over here the pins of 4-channel relay module is referred as "light 1" "light 2" "light 3" "light 4"
STEPS SET-UP FOR TELEGRAM BOT
STEP 1:- Create Telegram Account: if you don't have a telegram account, download the Telegram app on your phone and sign up
STEP 2:- In the Telegram app, search for the "BotFather". This is the official bot that helps you create and manage the bots
STEP 3:- Start a chat with the BotFather by clicking on it in the search results and then clicking the "Start" button
STEP 4:- In the chat with BotFather, send the command '/newbot' BotFather will guide you through the process of creating a new bot.
STEP 5:- You will be prompted to choose a name for your bot. This is the name that users will see when interacting with your bot. After that, you will need to choose a unique username that ends with the word "bot" (e.g., "myawesomebot").
STEP 6:- Once you've successfully created the bot, BotFather will provide you with an API token. This token is used to authenticate your bot and interact with the Telegram API. Keep this token safe and private; do not share it with anyone.
STEP 7:- First go to your bot and click on START to start the bot.
OBTAIN CHAT ID
- Open a web browser and enter the following URL replacing with your API token https://api.telegram.org/bot/getUpdates
- Now send a message to your bot You will receive a JSON response containing information about recent messages sent to your bot. Look for the chat object inside the message object. The id value inside the chat object is your chat ID.
- Now add the CHAT ID and BOT TOKEN to the Python code
ARDUINO CODE FOR Smart Home Lighting Control
- Open the Arduino IDE
- Load the provided Arduino code onto the Arduino board.
ARDUINO CODE EXPLANATION
The Arduino code is responsible for controlling the lights in your home automation system based on commands received over serial communication. It uses a 4-channel relay module to control the lights connected to it.
Here's a breakdown of the code's functionality:
1. Initialization: In the setup() function, the code initializes serial communication, sets the pins connected to the relay module as outputs, and initially turns off all the lights by setting all relay pins to HIGH.
2. Main Loop: The loop() function continuously checks for incoming serial data from the computer.
3. Command Processing: When serial data is available (Serial.available() > 0), it reads the incoming command character. Depending on the received command, the code performs the following actions: 'D':
Turns off all lights by setting all relay pins to HIGH.
'P', 'Q', 'E', 'T': Turns on the respective light (e.g., 'P' turns on "light1").
'M', 'N', 'Y', 'I': Turns off the respective light (e.g., 'M' turns off "light1").
Delay: The code includes a small delay after turning on a light (e.g., delay(1000)). This delay keeps the light on for a set period (1 second) before turning it off. You can adjust this delay according to your preferences.
- Upload the code PYTHON CODE
- Ensure you have the necessary Python libraries installed. You can install them using pip.
- Open the Python code in your preferred IDE or text editor.
- Set the correct Arduino serial port and Telegram bot token in the Python code.
- Run the Python code to start the automation system.
- When a person enters the room, the system detects their presence using the webcam and turns on basic room lighting.
- If no one is detected, the system sends a message via Telegram, asking whether to keep the lights on or turn them off.
- You can control the lights manually by sending specific commands to the Telegram bot. Use commands like "turn on light1," "turn off light2," etc.
If you encounter any issues with the system's operation, consider the following:
- Check the hardware connections, ensuring that the relay module and lights are properly connected to the Arduino.
- Verify that the Arduino code is correctly uploaded to the microcontroller.
- Ensure the Python code has the correct serial port and Telegram bot token.
- Review the Python code for any errors or exceptions.