Introduction: SmartBox - Smart Home System for Your Room
In this instructable, I will explain to you how to make a smart room system. This system contains two devices.
A general device with a humidity sensor and temperature sensor that measures the current quality of life in your room. You will be able to set the desired minimum and maximum temperature/humidity. When these values deviate, you'll receive notifications on the home page.
In addition to the sensors, there is also a smart radio clock integrated with a speaker that you can control via the website. You can create, delete and enable/disable alarms. Disabling alarms happen with a simple hand movement over an ultrasonic sensor.
As a second and separate device, you will be able to control an LED strip with pre-programmed patterns and colors.
- Raspberry Pi 3 model B+
- 16GB SD Card
- Arduino Uno
- Assembled Pi T-Cobbler Plus
- Small breadboard
- Breadboard power supply(For example the YwRobot Power Supply)
- One Wire Temperature sensor (DS18B20+)
- Digital Humidity & Humidity sensor (DHT22)
- Ultrasonic Distance sensor (HC-SR05)
- 16x2 LCD Screen
- Amplifier(Adafruit MAX98357A I2S 3W Class D Amplifier)
- Speaker 3" Diameter - 4 Ohm 3 Watt
- Bluetooth module (HC-05)
- 5V RGB Ledstrip (WS1812B)
- 1x 4.7K Ohm Resistor
- 1x 10K Ohm Resistor
- 3x 1K Ohm Resistor
- 1x 330 Ohm Resistor
- Jump wire cables
Step 1: Wiring
Let's start, shall we.
First, connect everything as in the scheme above. Make sure you connect everything correctly, otherwise some things will not work.
NOTE: If you want to make adjustments, then you will have to edit the code. So only do this if you know what you are doing! You can always contact me for questions or problems.
If you need the fritzing schemes, you can download them here.
Step 2: Database
The database will save all sensor measurements in a history table. This is used for history graphs on the website.
If you want to operate on the website you will need an account. Every account stores alarms, lightdevices, notifications and settings.
NOTE: The login mechanism is not included but I may add this later.
Step 3: Raspberry Pi Setup
So if everything is connected, we can begin with the setup of the Raspberry Pi.
First, let's start with the image.
1: Download the Raspberry Pi OS image: https://www.raspberrypi.org/downloads/
2: Download and install Win32DiskImager: https://sourceforge.net/projects/win32diskimager/
3: Insert the SD-card and run Win32DiskImager.
4: Select the downloaded image on your drive. Then select your SD-card and press the write button. (Make sure the SD-card is empty before you start this process, all data will be removed!)
5: If the process is done, we can do the last adjustments to the Raspberry Pi. Go to the SD-card directory and add a "ssh" file without extension to make sure the pi will enable SSH on start.
6: Then open cmdline.txt in the same directory and add "ip=169.254.10.1" on the end of the line and click on save.
NOTE:Keep everything on one line or some settings won't work.
7: Now safely eject the SD-card from your computer and put the SD-card in the Raspberry Pi.
8: When that's done, you can connect the pi with an Ethernet cable to the LAN port of your laptop or computer.
9: Power the Raspberry Pi.
The Wi-Fi and Pi configuration
To control the Raspberry Pi over SSH we need a software called Putty. You can download and install Putty here: https://www.putty.org/
1: Once Putty is installed you can connect to the pi with IP: 169.254.10.1 and port: 22. When the command line interface appears you can log in with the user: pi and the password: raspberry.
2: Now we are logged in type "sudo raspi-config" and go to interfacing options. Make sure One-Wire, Serial(Only enable the serial hardware port, not the login shell over serial), I2C en SPI are enabled.
3: To make connection with Wifi we need to use the root user. Type "sudo -i" to login as the root user.
4: To add your Wifi network to the Raspberry Pi, type
"wpa_passphrase "SSID" "your password" >> /etc/wpa_supplicant/wpa_supplicant.conf"
in the terminal with the name and password of your network.
5: Once this is done, reboot your pi.
6: If everything is right, you should see have an IP on your wlan0 interface. Do "ip a" to check this.
7: As last type "sudo apt-get update" and "sudo apt-get upgrade" to update your pi.
8: Make sure to install MySQL, apache2 and php-mysql for this project. Type: sudo apt install apache2 mariadb-server php-mysql -y
9: For installing MySQL read this article: https://randomnerdtutorials.com/raspberry-pi-apach...
10: Edit your MySQL user and password in the config.py file in the backend.
1: Power the Arduino
2: Type hcitool scan to find the mac address of your Bluetooth module. Once found write it down or copy it in a different document.
3: Now we are going to add the Bluetooth module to the Raspberry Pi. Do the following commands:
pair mac-address (If it asks for a pin, the standard pin is 1234)
4: Add your mac address to the app.py file in the backend.
Now your pi is updated and we have internet connection. We can start configuring the speakers.
1: Run the following command: "curl -sS https://raw.githubusercontent.com/adafruit/Raspbe... | bash"
2: When it's done it will ask you for a reboot, press y and enter.
3. Now run the script again, this will make sure the audio works correctly. If everything is right you'll hear a woman speaking.
4. When that's done, reboot a second time.
The project code runs on python so make sure you have python 3.7 installed. Check this with "python3 -V". If you have python you can Install the following packages with the pip installer: pip install Flask Flask-Cors Flask-MySQL Flask-SocketIO PyMySQL gevent gevent-websocket python-socketio requests
Now all main settings are configured we can start with the official SmartBox setup.
1: Clone the SmartBox repository to your home directory(/home/pi) by typing: git clone https://github.com/DeDeckerThomas/SmartBox.git
2: Once this is done, you can run the sql file. Type "sudo mysql -u root -p << SmartBox.sql" to make the database.
3: Then copy the frontend to /var/www/html by doing "cp -R frontend/. /var/www/html"
4: For automatic startup add the service file to systemd. Type: "cp service/SmartBox.service /etc/systemd/system/SmartBox.service" and do "sudo systemctl enable myscript.service" to enable it.
5: Once that's done, go to the config.py in the backend and change it to your mysql password.
6: Reboot your pi
Step 4: Arduino Uno Setup
Now the main device is done, we are going to start with the ledstrip. The Arduino will control the 5V WS1812B LED strip for us.
1: Download and install the the Arduino IDE: https://www.arduino.cc/en/main/software
2: Download the Arduino Code in my github repository: https://github.com/DeDeckerThomas/SmartBox
3: If everything is downloaded and installed, you can plug in your Arduino Uno.
4: Open the LedStripCode.ino file and upload it to your Arduino by clicking on the arrow pointing to the right.
5: Connect your bluetooth module and ledstrip to the arduino and everything should work. (See Step 1 Wiring)
Step 5: Case Design
For my case design I used an existing plastic box and the official Raspberry Pi case.
On the front view I made different holes for the speaker, and one for the LCD screen.
On the back side I made an opening for the wiring of the temperature and humidity sensor. There is also an opening for the wiring of the breadboard power supply inside.
On the top view there are two holes for the ultrasonic sensor, so movement can be detected when an alarm goes off.
I mounted the Raspberry Pi in the case on the right side of the box, so I can upgrade or replace it easier.
Once that is done, you can integrate your T-cobbler and sensors. I used screws and silicone to make sure everything is solid.
NOTE: If you use metal screws to mount the Raspberry Pi make sure you use insulating tape.
Step 6: Finishing Touch
Now everything is done, can you delete the IP part in the cmdline.txt.
Use: sudo nano /boot/cmdline.txt
Congratulations you have successfully made a SmartBox. If there are any problems, just contact me on instructables.