Introduction: Slimbox - a Smart Bluetooth Speaker!
For my school project at MCT Howest Kortrijk, I made a smart Bluetooth speaker device with different sensors, an LCD and RGB NeoPixel ring included. Everything runs on the Raspberry Pi (Database, Webserver, Backend).
So in this instructable I will show you how I made this project in 3 weeks, step by step, so if any of you guys want to recreate my project, you can easily do!
This is also my first instructable, if you have any questions, I'll try to answer them as quickly as possible!
My GitHub: https://github.com/TiboSevenhant/slimbox
Step 1: Supplies
DS18B20 Temperature Sensor
The DS18B20 is a one-wire sensor that measures the temperature, manufactured by Maxim Integrated. There are 2 sorts of DS18B20 sensors, the component only (Which I used) and the waterproof version, which is much bigger, but that’s not what I needed for my project, so I used the component only. The sensor can measure the temperature in a range of -55°C to +125°C (-67°F to +257°F) and it has an accuracy of 0.5°C from -10°C to +85°C. It also has a programmable resolution from 9 bits to 12 bits.
A potentiometer is resistor with three terminals which is manually adjustable by just rotating the upper part of the sensor. The position of the upper part determines the output voltage of the potentiometer.
LSM303 Accelerometer + Compass Breakout
The LSM303 breakout board is a combination of a triple-axis accelerometer and a magnetometer / compass, manufactured by Adafruit. It is used with the I2C interface of the Raspberry Pi.
To read the data from my potentiometer I used an MCP3008, which is an 8 channel 10 bit analog to digital convertor with the SPI interface and is pretty easy to program.
Speaker – 3”Diameter – 8 Ohm 1 Watt
This is the speaker cone I chose after calculating the Voltage and Amperes it would need and this was a perfect fit for my Raspberry Pi project, manufactured by Adafruit.
MAX98357 I2S Class-D Mono Amplifier
This is the amplifier that comes with the speaker, not only is it an amplifier, it’s also a I2S digital to analog converter, so it’s also a perfect fit for my speaker & audio system.
The Arduino Uno is an open-source microcontroller board based on the Microchip ATmega328P microcontroller, manufactured by Arduino.cc. The Uno board has 14 Digital pins, 6 analog pins and is fully programmable with the Arduino IDE software
This is a small board which takes care of the communication between the Arduino Uno and the Raspberry Pi and the different voltages, Arduino: 5V & Raspberry Pi: 3.3V. This is needed because the NeoPixel ring is connected to the Arduino and runs on there, while all the other stuff runs on the Raspberry Pi.
RGB NeoPixel Ring
This is a small ring filled with 12 RGB leds (you can buy bigger rings with more RGB leds, if you want to). Which is in my case connected to the Arduino Uno, but can also be connected to many other devices and is really simple to use.
LCD Display 16x2
I used a basic LCD Display to print my temperature, volume and IP address.
Raspberry Pi 3B+ & 16GB SD Card
My whole project runs on my Raspberry Pi 3B+ with a configured image, which I will help you configure later in my instructable.
GPIO T-Part, 2 Breadboards and lots of jumperwires
To connect everything I needed breadboards and jumperwires, I used the GPIO T-part so I have more space and it is clear which pin is which.
Step 2: Schematic and Wiring
For my schematic I used Fritzing, it's a program you can install which allows you to create a schematic really easy in different kinds of views.
Download Fritzing: http://fritzing.org/
So make sure you connect everything in the right way! In my case the colors of the wires are not the same like on the schematic.
Step 3: Database Design
We are collecting a lot of data from the 3 sensors connected, so we need a database to store the data and sensors in. Later we'll see how to configure the database on the Raspberry Pi and how to add data to it. But first the database design or ERD (Entity Relationship Diagram) has to be made and mine was also normalised with 3NF. That's why we split up the sensors into another table and work with ID's.
Overall this is a really basic and easy database design to further work with.
Step 4: Preparing the Raspberry Pi!
So now that we have some basics of the project done. Let's get started with the Raspberry Pi!
SD Card configuration
First, you need a 16GB SD Card where you can put your image on and a program to upload a start image to the SD card.
Start image: https://www.raspberrypi.org/downloads/raspbian/
So once these are downloaded:
- Put your SD card in your computer.
- Open up Win32 which you just downloaded.
- Select the Raspbian image file which you also just downloaded.
- Click on 'write' to the location of your SD card.
This may take some time, depending on your hardware. Once this is done, we are ready to make some final adjustments before putting the image into our RPi.
- Go to your SD card's directory, search for the file named 'cmdline.txt' and open it.
- Now add 'ip=169.254.10.1' on the same one line.
- Save the file.
- Create a file named 'ssh' without extension or content.
Now you can SAFELY eject the SD card from your computer and put it into the Raspberry Pi WITHOUT power. Once the SD card is into the RPI, connect a LAN cable from your computer to the RPi LAN port, once this is connected you can connect the power to the RPi.
Now we want to control our Raspberry Pi, this is done through Putty.
Putty software: https://www.putty.org/
Once downloaded, open up Putty and insert the IP '169.254.10.1' and Port '22' and connection type: SSH. Now we can finally open our command line interface and login with the starter login information -> User: pi & Password: raspberry.
What is really important for this project is the interfacing section, we have to enable a lot of different interfaces, enable all the following interfaces:
Now that we're done with raspi-config, let's try and make a connection with internet.
First, you have to be root for the following commands
Once you're root, use the following command. SSID is your network name and password is obviously it's password.
wpa_passphrase "ssid" "password" >> /etc/wpa_supplicant/wpa_supplicant.conf
In case you made a mistake, you can check, update or delete this network by just entering that file:
So after we entered our network, let's enter the WPA client interface
Select your interface
Reload the file
And finally you can see if you are connected well:
Update & upgrade
Now that we are connected to the internet, updating the already installed packages would be a smart move, so let's do that first before installing other packages.
sudo apt-get update sudo apt-get upgrade
Install the MariaDB database server:
sudo apt-get install mariadb-server
Install the Apache2 webserver:
sudo apt install apache2
update-alternatives --install /usr/bin/python python /usr/bin/python2.7 1 update-alternatives --install /usr/bin/python python /usr/bin/python3 2
You will have to install all these packages to make the backend work perfectly:
Install the speaker library from Adafruit:
<p>curl -sS <a href="https://raw.githubusercontent.com/adafruit/Raspberry-Pi-Installer-Scripts/master/i2samp.sh" rel="nofollow"> https://raw.githubusercontent.com/adafruit/Raspbe...</a> | bash</p>
Time to reboot!
Step 5: Forward Engineering Our Database to the RPi!
Now that we have installed everything we needed, let's put our Database we designed on our Raspberry Pi!
So first we need to forward engineer our database in the MySql workbench, while doing that, copy your full database code and delete all the 'visible' words in it. So once that is copied, lets open putty again, log in and type:
and now you're in the mysql interface, copy your database code in it and press enter.
Now we just need to create a user
CREATE USER 'user' IDENTIFIED BY 'user'; GRANT ALL PRIVILEGES ON *.* TO 'user';
So everything should be setup now, you can also make a connection with your Pi and MySql Workbench, so it's easier to check all the data in your tables.
Step 6: Configuring Bluetooth on Our RPi
We are creating a Bluetooth speaker, so this means the media is being sent from our source to the Raspberry Pi and this can be done pretty easy, let's get right into it!
My source for the bluetooth connection: https://scribles.net/streaming-bluetooth-audio-fro...
- Removing the already running bluealsa.
sudo rm /var/run/bluealsa/*
- Add A2DP profile Sink role.
sudo bluealsa -p a2dp-sink &
- Open up the bluetooth interface and power your bluetooth on.
bluetoothctl power on
- Set up a pairing agent.
agent on default-agent
- Make your RPi discoverable
- Now from your bluetooth device, search for the RPi and connect with it.
- Confirm the pairing on both devices, type 'yes' in your putty.
- Authorize A2DP service, type 'yes' again.
- Once this is done, we can trust our device, so we do not have to go all through this every time we want to connect
trust XX:XX:XX:XX:XX:XX (Your bluetooth mac address from our source device)
- If you want your RPi to keep on being discoverable, that's your own choice, but I prefer to turn it off again, so that people can't try to connect with your box.
- Then we can exit our bluetooth interface
- And finally our audio routing: our source device forwarding to our RPi
Now our device is fully connected to our Raspberry and you should be able to play media from your source device on the Pi speaker.
Step 7: Writing the Complete Backend
So now the setup is done, we can finally begin writing our backend program!
I used PyCharm for my whole backend, you just need to make sure your PyCharm project is connected to your Raspberry Pi, this means your Deployment path is set up in your settings and you installed all the packages we need, should be already done in step 4.
I used my own classes and these are also all included in my GitHub. Link is in the intro in case you missed it ;)
In my backend file I used threading classes, so everything can run at the same time and it won't interrupt each other. And at the bottom you got all the routes so we can easily get data in our frontend.
Now that the backend is done, we can start writing the full front-end.
HTML & CSS was done pretty easy, tried working mobile first as much as possible, since we most of the times connect with Bluetooth from a mobile device, it would be easier to control from a mobile dashboard.
You can design your dashboard in any way you want to, i'll just leave my code and design here, you can do whatever you like!
Step 9: Building My Case and Putting It All Together
I first started with some sketches of how I wanted the case to look, something important was that it had to be big enough for everything to fit in, since we got a big circuit to put in the case.
I made the case out of wood, I think it is the easiest to work with when you don't have that much experience with building cases and you also have a lot of things you can do with it.
I started from a case for wine bottles and just started sawing the wood. Once I had my basic case, I just had to drill holes in it (a lot on the front of the case, as you can see on the pictures :P) and put some nails in it, it's a really basic case, but it looks pretty cool and fits perfect.
And once the case was done, it was time to put it all together, as you can see on the last picture! It is kinda a mess inside the box, but everything works and I did not have that much more space, so I advice you to maybe create a bigger case if you are recreating my project.
Step 10: Some Problems I Had on My Road of Creating the Slimbox Speaker...
Bluetooth & bluealsa errors
Everytime I wanted to play music or connect with bluetooth, I received errors from bluetooth and bluealsa. I did some research on it and this was the solution to my problem. So for some reason my bluetooth was softblocked, not sure if this is standard soft-blocked. You can see if it is by typing the following command in your Putty.
So if it is softblocked, just use this:
rfkill unblock bluetooth
And you might want to reboot after this, my source: https://github.com/linrunner/TLP/issues/180
Serial connection problems
So another big problem I had was that I could not make any connection with my Arduino through the levelshifter, after some searching I found out that my '/dev/ttyS0' was gone and this could be because of an update of your RPi. Also found a solution on this
You will have to re-enable the serial console with raspi-config, reboot, and then manually remove the "console=serial0, 115200" bit from '/boot/cmdline.txt'.Confirm that "enable_uart=1" is in '/boot/config.txt', and reboot again.That should get your ttyS0 port back, as well as the '/dev/serial0' soft link to it.