Slimbox - a Smart Bluetooth Speaker!

About: I am an 18 y/o Belgian student studying MCT at Howest Kortrijk to become a future web & app developer.

Hey there!

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.

Datasheet: https://datasheets.maximintegrated.com/en/ds/DS18...

Potentiometer sensor

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.

Overview: https://learn.adafruit.com/lsm303-accelerometer-s...

Datasheet: https://cdn-shop.adafruit.com/datasheets/LSM303DL...

MCP3008

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.

Datasheet: https://components101.com/ics/mcp3008-adc-pinout-...

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.

Overview: https://www.adafruit.com/product/1313

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.

Overview: https://learn.adafruit.com/adafruit-max98357-i2s-...

Datasheet: https://cdn-shop.adafruit.com/product-files/3006/...

Arduino Uno

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

Overview: https://store.arduino.cc/arduino-uno-rev3

Levelshifter

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.

Overview: https://www.adafruit.com/product/1643

LCD Display 16x2

I used a basic LCD Display to print my temperature, volume and IP address.

Datasheet: https://www.engineersgarage.com/electronic-compon...

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.

Software: https://sourceforge.net/projects/win32diskimager/

Start image: https://www.raspberrypi.org/downloads/raspbian/

So once these are downloaded:

  1. Put your SD card in your computer.
  2. Open up Win32 which you just downloaded.
  3. Select the Raspbian image file which you also just downloaded.
  4. 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.

  1. Go to your SD card's directory, search for the file named 'cmdline.txt' and open it.
  2. Now add 'ip=169.254.10.1' on the same one line.
  3. Save the file.
  4. 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.

Raspi-config

sudo raspi-config

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:

  • One-wire
  • SPI
  • I2C
  • Serial

Now that we're done with raspi-config, let's try and make a connection with internet.

Wi-Fi connection

First, you have to be root for the following commands

sudo -i

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:

nano /etc/wpa_supplicant/wpa_supplicant.conf

So after we entered our network, let's enter the WPA client interface

wpa_cli

Select your interface

interface wlan0

Reload the file

reconfigure

And finally you can see if you are connected well:

ip a

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

MariaDB Database

Install the MariaDB database server:

sudo apt-get install mariadb-server

Apache2 Webserver

Install the Apache2 webserver:

sudo apt install apache2

Python

Install Python:

update-alternatives --install /usr/bin/python python /usr/bin/python2.7 1
update-alternatives --install /usr/bin/python python /usr/bin/python3 2

Python package

You will have to install all these packages to make the backend work perfectly:

  • Flask
  • Flask-Cors
  • Flask-MySql
  • Flask-SocketIO
  • PyMySQL
  • Requests
  • Python-socketio
  • RPi.GPIO
  • Gevent
  • Gevent-websocket
  • Ujson
  • Wsaccel

Speaker library

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!

sudo 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:

sudo mysql

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';

Now reboot.

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
discoverable on
  • 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.
discoverable off
  • Then we can exit our bluetooth interface
exit
  • And finally our audio routing: our source device forwarding to our RPi
bluealsa-aplay 00:00:00:00:00:00

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.

Step 8: Writing the Frontend (HTML, CSS & JavaScript)

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!

And Javascript was not that hard, worked with a few GET's from my backend routes, tons of event listeners and some socketio structures.

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.

rfkill list

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.

Source: https://www.raspberrypi.org/forums/viewtopic.php?t...

Share

    Recommendations

    • Sew Tough Challenge

      Sew Tough Challenge
    • Backyard Contest

      Backyard Contest
    • Barbecue Challenge

      Barbecue Challenge

    2 Discussions