Introduction: Smart Mailbox for Packages and Letters

About: Hey there! I'm Juul Van de Velde, a 19-year-old student currently studying MCT at Howest in Belgium. I'm passionate about coding, (board) games, and spending time with friends. Looking forward to what the futu…

Welcome to this exciting step-by-step guide on building your very own smart mailbox! In an era dominated by online shopping and digital communication, traditional mailboxes often fall short in meeting our evolving needs. But fear not! With this DIY project, you'll learn how to create a smart mailbox that not only ensures the safety of your packages but also keeps you informed in real-time whenever mail arrives. Furthermore, this smart mailbox offers a user-friendly web interface, enabling you to conveniently monitor and manage your mailbox from anywhere. It also incorporates a lighted house number to improve visibility during nighttime, ensuring that your address is clearly visible even in the dark.

Supplies

Controller:

Electronics:

Components:

Housing:

  • 12mm MDF 244x122 cm (feel free to change this to your liking)
  • drill with some screws
  • wood glue
  • some hinges
  • sandpaper / wood file
  • Philips Screwdriver
  • Soldering Iron (with some solder of course)
  • CNC machine (to cut shapes out of the wood)
  • 3D printer

For anyone who doesn't have the right tools, you can always go to a local makerspace. There they will be happy to help you with building your enclosure.

Pricing

If you're curious about the total cost of everything, you can find the complete bill of materials in the attachments. (With the wood included, everything together cost me roughly €400.)

Step 1: Raspberry Pi Setup

1. Download the image

  • Download the zipped image to your local computer; it already has some pre-configurations.


2. Write the image

  • Unzip the file
  • Place the file on a SD card of at least 8GB (16GB) using Win32 Imager or Balena Etcher
  • After the image is written, you can safely remove the SD card and insert it into your Pi


3. Connect your Pi with your PC

  • Boot your Pi
  • Connect the Pi to your computer with a network cable and put your PC on a static IP (e.g. 192.168.168.100). By changing your IP address it is now possible to connect to your Pi over SSH.
  • To do this, open Putty or VNC Viewer and start a SSH connection (port 22) with the IP address of your Pi (192.168.168.169)
  • When done correctly you can now log in with the username 'pi' and password 'W8w00rd'
PLEASE NOTE: The image is created in AZERTY! If the login does not work, try Z!zààrd in qwerty.


4. Connect your Pi with the internet

  • After logging in, type 'sudo raspi-config'
  • In the menu, choose (1) System Options > (1) Wireless LAN
  • Fill in the SSID (name) of your WIFI network
  • Fill in the password of your WIFI network


5. Enable some interfaces

  • Still in the raspi-config menu, choose (3) Interface Options and enable (4) SPI, (5) I2C and (7) 1-Wire
  • When done, press twice on the tab key and select 'Finish' by pressing enter
  • Reboot your Raspberry Pi


6. Now you're Pi is ready

When everything is done correctly, your Raspberry Pi is now ready for action!

Step 2: Connect the Hardware

Now that your Raspberry Pi is set up, it's time to wire the electronics. To simplify things, I have created a breadboard and electronic diagram for your reference. You can find them below.

While wiring just keep the following things in mind:

  • Your Raspberry Pi operates on 3.3V. If you connect 5V or 12V directly to the Pi, it will result in a dead Pi.
  • It is good practice to complete your circuit before connecting it to your Pi.
  • Check the values of the resistors; they are not all the same.
  • If something gets really hot as soon as you connect it to the power supply, unplug it right away and double-check your wiring.

Step 3: The Code

I've already written the code, so you don't have to worry about it. However, feel free to make any changes you'd like. Here's a quick rundown of the core components used in the code: Python, HTML, SCSS, JavaScript, MariaDB, Apache, and MySQL Workbench.

To get the code ready for use, follow these instructions:

1. Set up Visual Studio Code

  • Download the code from this link.
  • Install Visual Studio Code on your Raspberry Pi.
  • Install the Remote - SSH extension for Visual Studio Code.
  • Open Visual Studio Code and click on the Remote Explorer icon in the sidebar.
  • Click on the "+" icon and select "Connect to Host..."
  • Enter the SSH connection details for your Raspberry Pi and connect to it.
ip: 192.168.168.169
user: pi
password: W8w00rd


2. Copy and configure the code

  • Once connected, create a new folder in Visual Studio Code for the code.
  • Copy and paste the downloaded code into the newly created folder.
  • Open a terminal in Visual Studio Code.
  • Navigate to the folder containing the code.
  • Create a virtual environment by executing the following command:
python3 -m venv venv
  • Activate the virtual environment by deleting the terminal and opening a new one.
  • Install the required packages by executing the following command:
pip install -r requirements.txt
  • Locate the file named config-example.py and make a copy of it.
  • Rename the copied file to config.py.
  • Open config.py and fill in the appropriate values for the user, password, and name of the database.
  • Save the changes made to config.py.
  • Run the backend (app.py) by pressing the play icon in the top right corner of Visual Studio Code.
  • If you get an error run the following command:
sudo !!
  • Wait for the backend to start successfully.


3. Setup the web server

  • Open your web browser on your PC and go to http://192.168.168.169.
  • Normally, you should see the Apache2 Debian Default Page, which is the default page of Apache. It is currently located in the /var/www/html/ directory on the Pi. We won't be using this default directory, but we will use the front-end directory from the code you just downloaded.
  • If you're not already in sudo mode, run:
sudo -i
  • Open the file /etc/apache2/sites-available/000-default.conf using the nano editor:
nano /etc/apache2/sites-available/000-default.conf
  • Use the down arrow to navigate to the line that says DocumentRoot /var/www/html or DocumentRoot /home/pi/<name_of_your_folder>/front, and change it to DocumentRoot /home/pi/<name_of_your_folder>/front.
  • Save the changes by pressing Ctrl + x, then Y, and finally Enter.
  • Next, restart Apache by running the following command:
service apache2 restart
  • Now we need to set the correct permissions for the root folder.
  • Open the file /etc/apache2/apache2.conf using the nano editor:
nano /etc/apache2/apache2.conf
  • Use the down arrow to search for the following lines:
<Directory />
Options FollowSymLinks
AllowOverride All
Require all denied
</Directory>
  • Change them to:
<Directory />
Options Indexes FollowSymLinks Includes ExecCGI
AllowOverride All
Require all granted
</Directory>
  • Save the changes by pressing Ctrl + x, then Y, and finally Enter.
  • Restart Apache again by running the following command:
service apache2 restart
  • To check if Apache has started correctly, run the following command:
service apache2 status
  • You should see an output similar to the following:
Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: enabled)
Active: active (running) since ...


4. Automatically start the code (optional)

  • Create a file named "myproject.service" using a text editor of your choice.
  • Copy and paste the following code into the "myproject.service" file:
[Unit]
Description=ProjectOne Project
After=network.target

[Service]
ExecStart=/home/pi/<your_repo_name>/<venv>/bin/python -u /home/pi/<your_repo_name>/backend/app.py
WorkingDirectory=/home/pi/<your_repo_name>/backend
StandardOutput=inherit
StandardError=inherit
Restart=always
User=root

[Install]
WantedBy=multi-user.target
  • Save the file.
  • Open a terminal or command prompt.
  • Copy the "myproject.service" file to the "/etc/systemd/system" directory by running the following command with root privileges:
sudo cp myproject.service /etc/systemd/system/myproject.service
  • Test the service by starting it with the following command:
sudo systemctl start myproject.service
  • To stop the service, use the command:
sudo systemctl stop myproject.service
  • If the service works correctly, you can configure it to start automatically after booting by running the command:
sudo systemctl enable myproject.service
  • You can check the status of your service at any time by running:
sudo service myproject status
  • To view the logs related to your service, use the command:
sudo journalctl -u myproject


5. Test it ;-)

If you have followed all the instructions correctly, you should periodically see two IP addresses displayed on the LCD. Take note of the second IP address and type it into your phone's web browser. Once you complete the process, you will see your personal mailbox website right in front of you!

Now you can log in using the temporary admin account:

username: admin
password: admin

And tada! You should now see the dasboard page!

Step 4: Database Setup

For this project, I have created a simple and efficient database to store data from different sensors and handle user information. The database design is customized to meet the requirements of my project, ensuring efficiency and scalability. If you're interested in viewing the data from your mailbox, please follow these instructions:

1. Open MySQL Workbench and click on the "+" icon to add a new connection.

2. Choose a connection name that suits your preference.

3. Fill in the following details:

Connection method: 'Standard TCP/IP over SSH'
SSH Hostname: '192.168.168.169'
SSH Username: 'pi'
SSH Password: 'W8w00rd'
Username: student
Password: 'W8w00rd'

4. Test the connection and press 'ok'

Step 5: Building the Mailbox

To build the wooden mailbox, I started by sketching my design, considering both functionality and aesthetics. Once I was satisfied with the concept, I went to a local woodworking store to get the materials I needed. After acquiring the chosen wood, I carefully measured and cut it to the desired dimensions.

To create special features like the house number, mail slot, and the LCD cutout, I sought assistance from a local creator/maker space that provided access to a CNC machine. I also utilized a 3D printer to make a casing for the lighted house number, improving its visibility in the night. In the future, I plan to add a mechanism (like in this video) to enhance security, making it harder for parcels to be stolen.

I have included all the files you need as attachments to recreate the exact same mailbox. And to help you a little more, here is a list of the wood sizes I used:

  • 1x (77.6cm x 37.6cm)
  • 2x (78.8cm x 38.8cm)
  • 2x (38.8.6cm x 37.6cm)
  • 1x (37.6cm x 37.6cm)
  • 1x (37.6cm x 13.8cm)
  • 1x (15cm x 40cm)
  • 1x (25cm x 40cm)
  • 1x (40cm x 40cm)
If you'd like to 3D print the case, resize it to 77% because otherwise it won't fit the mailbox ;)


Step 6: Enjoy

I hope the explanations were clear and that you were able to achieve excellent results. If you have any questions, please feel free to ask—I'll be more than happy to provide the best possible answers.