? Want a device to remind you on bringing an umbrella before leaving the house? Need something to remind you to wash your hands once you get home? Why not make a smart door announcer with Raspberry Pi to do that!
In this tutorial, we are going to make a smart door announcer which can play different sound files according to the time period of the day. We will be using a camera to detect for motion (you can place it near a door) and play sounds when the motion is detected. You can use this door announcer for different purposes as well, such as:
- Give reminders
- Greet visitors/customers
- Announce important information
- Play music
- Alarm for security purposes
We will also be building a simple Graphical User Interface (GUI) so that you can update the sound files to play remotely from a computer by running the GUI application.
This tutorial assumes that you already have your Raspberry Pi being initially set up with Raspberry Pi OS. If you do not have a monitor/keyboard/mouse to set up your Raspberry Pi, see How to Setup Raspberry Pi Without Monitor and Keyboard.
We have a lot to do, lets get started! ?
What You Need
If you have difficulties in setting up your Raspberry Pi Zero due to insufficient hardware, check out the Complete Guide on Remotely Access to Raspberry Pi (Windows).
The hardware assembly of this project is very straightforward, you can refer to the GIF below:
You have to set up the speaker as well, which depends on which model you are using. Just make sure that you are taking the audio source from the HAT’s 3.5mm audio jack. Of course, you will need a power supply for the Raspberry Pi as well.
We will start by installing some of the the required libraries for the door announcer. Open terminal in your Raspberry Pi and enter the command below:
$ sudo pip3 install opencv-python
$ sudo apt-get install -y libatlas-base-dev libhdf5-dev libhdf5-serial-dev libatlas-base-dev libjasper-dev libqtgui4 libqt4-test
Verify your OpenCV installation:
>>> import cv2
You should be able to see the output similar to this:
Press ctrl + D to exit the Python script mode. Next, we need to install the driver for ReSpeaker 2-Mics Pi HAT. Refer to the official wiki for more information.
$ sudo apt-get update && sudo apt-get upgrade
$ cd ~
$ git clone https://github.com/respeaker/seeed-voicecard.git
$ cd seeed-voicecard
$ sudo ./install.sh
$ aplay -l
If your installation is successful, you should be able to see the output similar to below:
Then, we have to select the sound card as output and enable camera interface:
$ sudo raspi-config
Navigate to System Options > Audio > wm8960-hifi-0 to change the audio output to the HAT’s audio jack. Then, navigate to Interface Options > Camera and select <Yes> to enable the camera. Finally, select <Finish> and reboot your Raspberry Pi to apply the changes.
Now you have your audio and camera set up. To change the volume of the speaker, open up your Raspberry Pi terminal, and enter the following command:
Press “F6” and select “seeed-2mic-voicecard” as your output device. You can change the volume of the speaker at the leftmost slider:
Press “ESC” to exit the audio mixer.
Source Code Download
You can download the source code of this project with the following command:
$ cd ~
$ git clone https://github.com/CytronTechnologies/smart_door_announcer.git
There are several items in the folder. Refer to the table below for the file descriptions:
|main_camera.py||Python code||Main source code for running the entire system.|
|motion.py||Python code||Source code for motion detection with OpenCV.|
|sound.py||Python code||Source code for playing sounds.|
|pixels.py||Python code||Library for using the RGB LEDs.|
|apa102.py||Python code||Library for using the RGB LEDs.|
|reboot.sh||Executable script||Script for running the door announcer at the startup.|
|cytronpi.log||Log file||Store the output log for reboot.sh script.|
|crontabkill.txt||Text file||Contain the commands to stop the startup script.|
|sounds||Folder||Contain sound files to play information.|
|gui||Folder||Contain the files for GUI in remotely update sounds.|
Now let’s test out if everything we’ve done so far is working properly. Type the following command in your Raspberry Pi terminal:
$ cd smart_door_announcer
$ python3 main_camera.py --nodisplay
The first thing you will be noticing is that LEDs are blinking and piano music playing. Once the LEDs have stopped blinking, try to trigger the camera. The Raspberry Pi will play some sounds according to the current time. If all of these are working, great work! You have a working door announcer right now.
To stop the script from running, press Ctrl + C. We are only halfway there in making a SMART door announcer. So keep up with the tutorial. ?
Note: Do not change the folder location and file names. Unless you know what you are doing!
Run Door Announcer at Startup
The reboot.sh in the folder is the script to execute the python code for running the door announcer. To run the reboot.sh script at startup, enter the following command:
$ cd smart_door_announcer
$ sudo chmod +x reboot.sh
$ crontab -e
You might see the outputs like this if it is your first time running crontab. Just choose “nano” as your editor by pressing 1.
Then, add the following command at the bottom of the script:
@reboot sleep 60 && /home/pi/smart_door_announcer/reboot.sh >> /home/pi/smart_door_announcer/cytronpi.log 2>&1
Press ctrl + X followed by Y and ENTER to save the changes. This command is used to tell the raspberry pi to wait for 60 seconds upon startup and then run the reboot.sh script. The 60 seconds sleep time is required to make sure that the sound card has been initialized before running the door announcer. The output of the script will be saved in the location of ~/smart_door_announcer/cytronpi.log.
In order to stop the door announcer running in the background, you have to enter the following command in the terminal to show all the cronjob running:
$ ps -o pid,sess,cmd afx | egrep -A20 "( |/)cron( -f)?$"
Search for the job running the reboot.sh script. Take note of the PID number shown on the left, in this case it is “306”.
Then use the following command to stop the script:
$ pkill -s <PID>
You can refer to the file “crontabkill.txt” which contains all the commands to stop the startup script running in the background.
Note: You can press and hold the button on top of the Pi HAT to turn off the Raspberry Pi when the script is running.
Setup for Remote Access to Raspberry Pi
Now, it’s time for the most interesting part of the project — making your door announcer SMART. Firstly, please follow this tutorial on Connecting to Raspberry Pi via Hostname. Be sure to remember the hostname that you’ve set, we are going to use it later on.
Then, make sure that you have enabled and tested out SSH in your raspberry pi. You might also want to consider disable desktop GUI by enabling CLI when deploying your Raspberry Pi as smart door announcer. That’s all you need to do on your Raspberry Pi.
Updating Sounds Remotely
As promised in this tutorial, we have a Graphical User Interface (GUI) for updating the sounds. The GUI is designed to be as interactive as possible so you that can share it among your family and friends for them to edit the sounds they want to play. You just need to tell them the hostname and password to access the Raspberry Pi.
Firstly, you have to download the GUI application from your Raspberry Pi. Open command prompt on your computer (Windows PC) and navigate to your desired directory to save the folder (eg: “cd Desktop”). Then, enter the following command:
$ scp -r pi@<HOSTNAME_OR_IPADDRESS>:~/smart_door_announcer/gui .
Enter the password and the folder will be downloaded into your computer. There are 2 files in the folder, one is the application file while another file is the Python source code.
Double click on the application file (the one with “.exe”) file extension, and BOOM. That’s the GUI. That’s what made the door announcer SMART. Play around with it before looking at the source code ?.
We will not dive deep into the source code and explaining each part of it in this tutorial. Instead, we will just briefly explain about what each Python code does and where you can edit the behavior of sounds to play. It requires you to have some knowledge in Python programming as well as image processing with OpenCV in order to fully understand the source code. They are thoroughly commented to help you in understanding, feel free to open them up and see what is actually happening in the backend of your smart door announcer!
This is the main source code for running the entire system. It does a few things:
- Import all the files and required libraries.
- Read the arguments for running the Python code. If “-nd” or “–nodisplay” command is being used, the output display for camera will be disabled.
- Setup and read GPIO for power off button.
- Blink the RGB LEDs on the Pi HAT.
This section of source code is where OpenCV library is being used to detect the motion through the camera. The key features in this code are:
- Motion detection according to the difference between the current frame and the reference frame.
- Automatically update the reference frame if prolonged change in background is detected.
This Python file is responsible for the behavior of sounds to be played when the motion is detected. The Pygame mixer being used to play the sound will be initialized upon importing this code. We will further explaining about this code in the next section so that you can customize the sound behavior of door announcer.
pixels.py & apa102.py
These 2 Python files are the libraries for blinking the RGB LEDs on the Pi HAT, which are taken from here. The functions that are being used in this tutorials are:
- pixels.wakeup() -> To initialize the RGB LEDs
- pixels.off() -> To turn off the RGB LEDs
- pixels.think(), pixels.speak() & pixels.listen() -> Used to make different sequence of blinking
This is the Python code for creating the GUI for remotely updating the sounds to play. All the UI components are being built by using Tkinter library. The uploading of the sound files are actually done by using SCP (Secure Copy) commands, which are just plainly transferring the .mp3 file from your computer to Raspberry Pi and rename it according to the name of sound to be changed. We will further explain on how to customize this code in the next section.
You might also be wondering about how we make the Python code into an executable (.exe) file. Just in case you don’t know what is an .exe file, it is a kind of file that can be executed or run as a program in the computer, specifically in Microsoft Windows. That’s the reason why you can run the music uploader even without Python installed in your computer. Back to exporting Python code into executable file, we are using autopytoexe, which is the graphical interface for PyInstaller, to make that happen.
Customizing Sound Behavior
This is actually the most important part of the tutorial. We will discuss in detail on how to customize when and what sounds to play when the motion is detected by the camera.
Referring to the Python file sounds.py, you will find a function called “decideSound”. This is the place where the algorithm for which sound to play is being called when the motion is detected.
In this case, we are using time based rule to determine which sound to be played. You can use different type of rules or even combine them so that the door announcer behave according to your needs. Here are some suggestions of what you can do:
- Extract data from weather forecast provider and announce the weather condition for the next few hours so that you can decide whether to bring an umbrella or not when leaving your house.
- Retrieve the traffic condition and inform which road you should take to reduce congestion before leaving your house.
As you can see, you can do something really SMART with this door announcer. That depends on your own imagination!
Other than that, you can use the predefined function “playSound” to play any .mp3 or .wav files:
This function accepts 2 parameters:
- file -> File name or file path pointing to the sound file to be played.
- wait -> If this value is being set to True, it will wait for sound file to finish playing before continuing to the next task. Otherwise, the sound will be played in the background and be overwritten if this function is being called again.
Note: The characters for file path, name and extension are case sensitive. Make sure to follow the exact characters so that it refers to the correct source.
On the GUI side for uploading the sound source, you can easily customize the items of the dropdown menu by editing the “SOUNDS” dictionary on smart_sound_uploader.py file.
The key of the dictionary (before “:” symbol) will be displayed as the name for the items in the dropdown menu whereas the value of the dictionary (after”:” symbol) will be used as the filename of the sound file being uploaded to the Raspberry Pi. By default, all of the files will be uploaded into the “~/smart_door_announcer/sounds” folder in the Raspberry Pi.
Have fun building your own smart door announcer!
Congratulations if you are able to follow the tutorial all the way until here. We have actually done a lot! Have ideas to further improve the smart door announcer? Leave a comment below.