Tag : Software

This project, a Raspberry Pi Outdoor Music Player had been germinating in my mind for a while. We’d had a few BBQs and found that running an extension outside to plug the iPod and speaker system into was a bit of a pain.

Requirements

I had seen a whole bunch of MP3 players housed in old / up-cycled ammo boxes, and I wanted something similar. The thing I wasn’t too keen on was most of them only had play/pause and fwd/back buttons. I wanted something that I could play music on, have playlists and stream internet radio on. So the requirements list was shaping up a bit like this:

6 to 8 hours battery life (and batteries that could be easily swapped / recharged).

Internet access (for streaming radio stations).

Web front end (for control, generating playlists etc.)

Reasonable volume (enough to be heard at a family BBQ, and but not enough to annoy the neighbours).

Rugged enclosure, that can handle itself outside.

The Parts

Enclosure

Whilst an ammo box is rugged and cool, I wasn’t too sure how a Wi-Fi adapter would fare trying to get a decent signal from inside a metal box. So, something else was needed, something rugged and non metal. Looking around the garage I spied some old plastic boxes that housed power tools. One of those would be ideal. I chose a Bosch Cordless Drill/Driver box that was empty anyway. If I hacked it about, it wouldn’t be a big loss if anything went wrong and I had to bin it.

Brain

This was a ‘no brainer’ (pun intended) – I would use a Raspberry Pi. I had a spare Model B+ (with 2 USB ports), so I could attach a Wi-Fi USB dongle and a USB memory stick to hold a local music library. That also allows me to change / improve the software as needed, and it can play from the local library, stream from the garage server, or Google Play Music, and the GPIO pins would allow me to drive a LCD display to show track information etc. if I chose to do that in the future. The Raspberry Pi also has an audio out jack, so driving the amp / speakers wouldn’t be an issue.

For software I decided to go with MPD (Music Player Daemon). This is fairly ubiquitous in Raspberry Pi Music Systems, and rightly so. It gets the job done well, has a ton of clients across all platforms and is easy to get installed working. For the front end I found ympd which is a great application, single file, self contained that you run and it acts as a web server (so no apache, nginx, lighttp needed) listening on a port of your choice. The UI has a nice clean functional look too.The only issue is that it doesn’t (yet) do playlist management/creation (I’ll be looking into adding that later).

Amplifier

The audio output from the Raspberry Pi itself is fine for headphones, but is not really good enough to drive a set of speakers directly. So, I wanted to feed a stereo audio amplifier from the Pi and have the amplifier drive the speakers. Initially I went with a 12 volt 15watt amplifier module, but after a few problems with this (see below), I swapped to one of these 5 volt stereo 3W=3W watt amplifier modules.

Speakers

For the speakers I needed something that fitted into the space available in the plastic case, and that would fit into a flat area on the outer part of the case. I found some 4 inch 4 Ohm speakers from Maplin, for the princely sum of £2.99 each. They were a perfect fit for the space I had available in the box. If you can’t get them from Maplin they you can try these from Amazon.

Battery

I originally planned to use some spare LiPo batteries that I had laying around. I had 2200mAh 3S batteries available that would provide around 11-13 volts. After having audio noise problems (see build below) trying to use a DC-DC (buck) converter to get this down to 5 volts for the Raspberry Pi, I abandoned this and reverted to an Anker 20,600mAh portable battery pack that I had anyway. I normally carry this portable charger around with me, and when needed in the Raspberry Pi Outdoor Music Player I just plug it in and we’re ready to go. I may have to buy one to keep in the box rather than swapping it around every time.

The Raspberry Pi Outdoor Music Player Build

I put this project together in 3 stages, each of around 1 hour.

Stage 1 – Case Mods

The first part of this was to cut out cardboard templates for the speakers – this allowed me to place them on the case and see where the best fit was. When I was happy with the position I then marked it and cut the holes. For the cutting I started each with a holes drilled all the way around, then did the rest with a Stanley knife / box cutter.

Next was the internal case mods. This will vary depending on the case you use. For me the plastic risers and dividers inside were thin enough to cut with the Stanley knife, so it was a quick job. Just find suitable spaces for the Raspberry Pi, the amp, the battery and cut those to the size required. You’ll also want to cut out some routing for the power and audio cables. I routed these around the outside of the box, but whatever suits you best.Note: Initially I mounted the 15W amp on it’s side, screwed into one of the dividers (see image below), but the 3W+3W (smaller) amp had an adjustable pot on it and was designed to be mount through the chassis.

15W 12v Amp mounted

6W 5v Amp mounted

The final product looked like this:

Stage 2 – Connecting Everything

This stage required a little soldering. Basically I took a 3.5mm audio cable I had laying around and cut off one end. I then found the right connections for Ground, Left Audio and Right Audio and soldered those to a 3 pin header socket I had. This was then connected to the 3 pin header on the amp board. The output of the amp board is a + and – audio signal for each channel. I used 2 pin cables to plug into the headers on the board, cut off the other ends and soldered the cables directly to the speaker + and – tabs.

For Raspberry Pi power I ran a USB to micro USB cable from the battery to the Raspberry Pi (just as you normally would). For the power to the amp board, I first considered hacking about another USB to micro USB cable. Then I remembered I had some little micro USB socket boards that I’d bought in bulk some time ago. I plugged in the second USB to micro USB cable to that board and soldered the +5V and Ground connections from that board to the amp board.

Everything was now connected – time to start on the software side.

Stage 3 – The Software

This is still a (kind of) work in progress stage. To get the first version working I installed mpd and the command line client (mpc) on the Raspberry Pi:

sudo apt-get install mpd mpc

Next I used the command line client to add a stream (Heart Radio) and told it to play:

Not much happened at first, but I turned up the volume on the amp board (using the on board potentiometer) and then it worked well, so I knew I had things connected and working correctly.

So far so good, but I didn’t want to be SSHing into the Raspberry Pi to add or change songs. I also wanted to get a local cache of all my music on there. A 64GB USB stick would easily hold my music collection with room to grow. So, I copied all music from my home server to the USB stick and inserted it into a USB socket. You could also simply copy all your music onto the Raspberry Pi SD card itself (assuming you have enough spare storage). The default location that MPD looks for music files is /var/lib/mpd/music so best to copy your files there. If you go the USB route then you have to tell mpd where the base folder is for your music. This is done in the config file, so a few more commands at the terminal:

sudo nano /etc/mpd.conf

Now find the line with the setting music_directory and edit the path to your USB drive. On my machine that was /mnt/usb/music. You may have to restart the mpd daemon after this, to do so:

sudo service mpd restart

That sorts out the playing of music, now we just need a simple way for a user to interact with it. For that I wanted a web interface, so that anyone (of my family or guests) could connect and chose songs to add to the queue. Initially I thought that would mean a full blown web server, database, PHP stack, however a google around found me ympd. This a self contained app that uses web sockets to serve up web pages dynamically. The author has done a really neat job of using bootstrap and JavaScript to provide a clean and functional user interface.

There are simple instructions on the website to get this installed and running. Essentially, download it, extract it and run it. I wanted it to be run automatically so I moved the app :

mv ympd /usr/bin

… and the made it automatically start with :

sudo crontab –e

… then added

@reboot /usr/bin/ympd –webport 90

to a new line at the end of the file and saved/exited.

With all that set up you should be able to use a browser to navigate to your Raspberry Pi. I gave mine a hostname of musicpi, so I simply browse to http://musicpi.local. From the web interface you can control everything you need to. Of course, there are a ton of other clients available for mpd if you would rather control it directly from your mobile etc.

Aside – Audio Problems

It is probably worth noting that initially (with the 15W audio amp) I had a lot of noise and distortion. The amp I had chosen needed a 12v source, so I had used a 12v power supply and fed the amp as well as a DC-DC (buck) convertor to step the voltage down to 5V for the Raspberry Pi. I believe the buck converter is very noisy (switching) and this was causing the problem. As soon as I switched to the 5v amp the noise vanished. Bonus for only having a single supply voltage too.

Conclusion

Overall, I’m really happy with the Raspberry Pi Outdoor Music Player Project result. A few spare (laying around) parts, combined with a cheap amp and speakers and a few hours of integrating it all together and I have a pretty cool music box. There are a few enhancements in the works for this too:

Change the case for an old 80s style ‘ghetto blaster’ radio cassette player.

[Possibly] Better working with playlists (creation, editing)

Some controls on the case for skipping forward/back or loading a specific playlist.

LCD display showing the current playlist/song/time remaining etc.

If you build one, or found this useful please comment below and leave a link for me to check out your project.

I had a little SSD1306 128 x 64 bit OLED display laying around from a previous Arduino project, so I thought it was time to do a bit of OLED display interfacing with a Raspberry Pi.

Display

The device I had was an I2C version of the 0.96” 128x 64 bit SSD1306 OLED module. Monochrome only. These devices are available on Amazon for £3-£4 and make for a really interesting project. It’s only 4 wires to connect; 2 for I2C communications, and 2 for power (it can be powered from the +3.3v line on the Pi GPIO).

So, little jumper wires between 3.3v, Gnd, SDA and SCL on the GPIO header, and the device (all clearly labelled) and we are ready to start.

There are some libraries and tools available for this device from Adafruit. It is sometimes useful to begin with those, if nothing else than to make sure the device works, and you have it wired up right. However, we want to do this ourselves, so the first thing we’ll need is a copy of the data sheet to understand how the device works. Googling it, I found copies for download here, and here.

The bits we are interested in are section 8.7 Graphic Display Data RAM, and sections 9 and 10 (Command Table and Command Descriptions). It’s tough reading, but you can get there eventually. Essentially, we want to put it into Horizontal display mode so that we are writing one byte per column (128 columns per line), then moving down to the next line and writing the next set of bytes for this line of 128 columns. I’ve tried to explain this on a post-it…

OLED display interfacing with a Raspberry Pi – Software

Now that we have the device wired up and the datasheet downloaded it is time to write some code for OLED display interfacing with a Raspberry Pi.

I’ve tried to pull out all the ‘initialization’ commands and just lump them into an array – you can look them up in the data sheet if you want to understand this in a little more detail. Here’s the sequence for init:

We also have to tell the display where we want to start the data being displayed from, in our case that is the top left. The command sequence for that is as follows:

unsigned char setFullRange[7] = {0x00,0x21,0x00,0x7F,0x22,0x00,0x07};

To get these commands sequences written to the device we need to send it over the I2C bus. Make sure your I2C is enabled (run sudo raspi-config). Here is the code we will use to send data over the I2C bus:

This simply opens the /dev/i2c-1 device, then gets a handle to our display device and writes data to it (and closes it afterwards).

When we have initialized the device we can then begin sending it data we want it to display. As you’ve seen above we just need to choose data bytes to send it that illuminate the pixels we want. For example sending 0xFF to every column (and page) will illuminate every pixel on the device. Sending 0x11 will produce 2 thin lines on each of the 8 pages (lines of characters).

That is great, but we want to send text characters to the device, how do we do that. Well the first thing we’ll need to do is to map out the characters and determine what bytes we will need to display them. Let’s start with ‘A’. You can see below that I have mapped this out over 4 bytes:

I’ve shaded in the pixels I want to illuminate to make the A character. The ‘Most Significant Bit’ (MSB) is on the bottom and the ‘Least Significant Bit’ (LSB) is at the top. You can see that I have also left the top line of pixels and the bottom line of pixels blank – this is to create a separation between each line of characters. The bytes we will need are 0x7E, 0x12, 0x12, 0x7E. We will also need to add a 0x40 byte before these to inform the display that we are sending some data to be displayed.

OLED display interfacing with a Raspberry Pi – Mini Sample App

So, if we initialize the display then send a sequence of these 5 bytes to it we’ll have the letter A displayed at the top left of the screen. Here’s the full code for an app that will display an A in the top left of the screen:

OLED display interfacing with a Raspberry Pi – Full Character Set

Instead of adding the bytes for each letter multiple times we are going to create ourselves a ‘look up table’ – we want a letter H?, the code looks up H in a table and there it finds the bytes we need. This is a bit of a labourious process of working out the byte sequence for every letter (and number and punctuation character) and putting all of them into an ‘array of arrays’. It is slightly complicated further because we might have a different number of bytes for different letters. For example the ‘@’ sign needs 5 bytes whereas the letter I only needs 3. Not a problem, for each letter the first byte of the sequence will tell us how many bytes for the letter. For the case of our letter, it would look like this:

{4, 0x7E, 0x12, 0x12, 0x7e}, //A

The first byte is 4, telling us there are 4 bytes in the sequence to display the letter ‘A’. Here is the sequence for the ‘@’ sign, which requires 5 bytes.

{5, 0x7E, 0x42, 0x5A, 0x52, 0x5E}, // @

Fast forward through the section where I work out all the byte sequences and populated our ‘array of arrays’.

Now, we make sure our ‘array of arrays’ is laid out in the same sequence as the standard ASCII table. This allows us to implement a neat trick where if we subtract the ASCII value for ‘<SPACE>’ we get the correct offset into our array of arrays. So if the start of our data looks like this…

… and we want to display a ‘#’ character (ASCII value 35) then we subtract the ASCII value of ‘<SPACE>’ (which is 32) and get 3. Allowing us to look up the byte array at fontData[3] – which is {5, 0x24,0x7E,0x24,0x7E,0x24} The first number (5) is the number of bytes we need to display this character, and the following 5 bytes are the actual values we send to the device to display this pattern.

OLED display interfacing with a Raspberry Pi – Bringing it all together

Now that we know how to get the correct bytes sequences we just need to pull everything together. This is a case of combining the initialization, the interpreting the desired characters into bytes sequences and the writing of the bytes to the display.

I found the easiest way to do this was to create a 128 byte array for each line…

We also need to make sure that the remainder of the line is filled with zeros just in case we are overwriting a line that previously had text on it.

// now fill up any left over with 0x00s on the current line while(currByteCount < 128) { displayLines[currentLine][currByteCount++] = 0x00; }

Now that we have the displayLines arrays populated with the bytes we need it is a simple case of sending them to the device over I2C. We have to create a byte sequence that is a 0x40 (‘data stream coming’ command) followed by all the bytes for the line. We do this for each line…

… and now we have working code that we can use to call a function with some text as the parameter and have that text displayed on the OLED screen.

Source Code

The full source for this OLED display interfacing with a Raspberry Pi project can be found on github at : https://github.com/kjhughes097/pi-ssd1306-oled This separates out the functionality into a ‘SSD1306’ class that you can use in other apps. It also separates the character mapping header (charmap.h) out so that it is easy to add additional characters (you could try adding lowercase letters ?). The ‘sample’ app, display-a.cpp is self contained and shows the basic use of I2C to send data to the display. The other ‘sample’ app, oled.cpp shows how to use the class. The makefile will compile the oled app – at the command line simply type

make

Hope this has been useful. Please leave a comment below if you thought so.

Take all 26 letters of the alphabet, add the numbers 0 to 9 and a bunch of punctuation. Now work out the byte arrays for the character maps required to display each character on a 128x64bit OLED display…

This article is about how to configure Wi-Fi on your Raspberry Pi without using the desktop GUI

It only takes a minute or so and is basically just entering your Wi-Fi settings into the configuration file.

Let’s get started…

We’ll need our Pi up and running and you’ll want to make sure we have all the latest updates, so open a terminal (or SSH connection to it) and make sure you’ve run the following commands:

sudo apt-get update sudo apt-get upgrade

Now plug in the USB Wi-Fi plug. To check that it has been recognised you use the command:

lsusb

You should see your Wi-Fi adapter listed. An easy way to check is to run the lsusb command with the adapter unplugged and again with it plugged in (you should see an extra device listed). Next we want to configure the device so that it connects to our Wi-Fi network. To do that we’ll need to edit the /etc/wpa_supplicant/wpa_supplicant.conf file, so type:

sudo nano /etc/wpa_supplicant/wpa_supplicant.conf

You’ll see there are only 2 lines in the file (assuming you haven’t already entered any Wi-Fi configuration. So now we add our Wi-Fi details – namely the SSID (network name) and the PSK (password). The format is to add those details as follows:

network={ SSID=“myssid” PSK=”mypassword” }

Obviously replace myssid and mypassword with your actual values. Below is one I create with sample values.

Now close and save the file (CNTL+X, Y, ENTER), and reboot and your Pi should connect to Wi-Fi.

In this project we are going to set up a FTP server using vsftpd. This is one of the fastest and most secure FTP server for Linux.

This will take us around 10 minutes to complete and is solely consists of typing commands to install and configure software. By the end of this you will have a FTP server running on your Pi that you (and authorized others) will be able to connect to and upload/download files.

Lets get started…

We’ll need our Pi up and running and you’ll want to make sure we have all the latest updates, so open a terminal (or SSH connection to it) and make sure you’ve run the following commands:

sudo apt-get update sudo apt-get upgrade

Now we’ll want to install the vsftpd package:

sudo apt-get install vsftpd

When this is complete (it takes a minute or two) we will have the software installed and need to move on to configuring it. To configure it we need to edit the vsftpd.conf file in the /etc/ folder. As this file is in the /etc/ folder we’ll need to be sudo to do that, so enter the following:

sudo nano /etc/vsftpd.conf

All the options are set to their defaults initially, but there are a few we want to change:

listen=YES

Setting this to ‘YES’ allows us to run the software as a daemon

#listen_ipv6=YES

This setting needs commenting out, or the service will fail to start. Simply add a # character at the beginning of the line

anonymous_enable=YES

Setting this to ‘YES’ allow other users to log in without have to enter a username / password first, but they will only have ‘read’ access (because of the following settings). Obviously, set this to ‘NO’ if you don’t want unauthenticated users being able to access your server / files.

write_enable=YES

Setting this to ‘YES’ (by removing the # character at the start of line) allows authenticated (not anonymous) users to upload files to our server.

anon_upload_enable=NO

Setting this to ‘NO’ (or omitting it from the configuration by adding a # character at the start of the line it is on) prevents anonymous users from uploading files – we don’t want any old user uploading files to our server (and maybe maliciously using up all the storage, or worse)

ftpd_banner=Welcome to the Pi Starter FTP server

You can set this to any text value and that text will be displayed to users when they successfully login. Make sure you remove the # character from the start of the line.

Excellent, we should now be fully configured and ready to start the ftp server, so we do that by entering the following command:

sudo service vsftp start

We should now have a fully working FTP server. You can test this out by running something like CuteFTP, or FileZilla on a remote machine and connecting to your Pi. Here’s an example of my FileZilla client connected to the Pi, and a Windows (PowerShell) command line connection to it

This project will show you how to use a standard webcam to create a time-lapse video recorder – great for watching the whole day go by in a few minutes.

For this we’ll need a Raspberry Pi, a USB webcam (and a portable power source if you want to position it somewhere outside) I’ll be using a Pi B+ running the latest version of Raspbian Jesse, a USB Wi-Fi dongle, a Microsoft Lifecam HD and an Anker portable battery pack.

First up you’ll want to get powered up and logged in, with the USB webcam connected. To check the webcam is recognised just type lsusb

You can see that my webcam is Device 005 on Bus 001 and is from Microsoft Corp. So great we’re in business, now we need some software to control the webcam. For that we’ll be using fswebcam – this is a great package with tons of features, but we’ll only be using the basics. To install it just type sudo apt-get install fswebcam

You can try it out by typing fswebcam –r 1280×720 –no-banner image.jpg (the –r 1280×720 tells it what resolution to use for the capture, the -–no-banner tells it not to display the time banner and image.jpg is the name of the file to save the image in.

You can view this image now on the Pi if you have a monitor attached or by grabbing a copy of the file (FTP, SFTP etc) onto another machine. Now we have this working we’ll also need some software to convert all the image captures we take into a movie, for this we’ll use avconv which is part of the libav-tools package, so let’s install that by typing sudo apt-get install libav-tools

That’s all the software we need, it is just a matter of orchestrating things. We’ll need to:

Capture a webcam snapshot to a file (with a sequential number as the filename)

Sleep for X seconds

If we don’t have enough snapshots yet go back to step 1

Merge all the snapshots together into a time-lapse movie file

Delete all the snapshot files

To write the script that does all this type nano timelapse.sh and enter the following:

Final bits required are to create the timelapse folder – type mkdir timelapse and make the script we wrote executable – type chmod +x timelapse.sh

We now have a script that will take a snapshot every 10 seconds (INTERVAL) until we have 30 of them (MAXCOUNT) and then combine them all into a movie for us. Be warned that this will take some time (30 time 10 seconds to get all the shots and then around 10 minutes of processing to combine them) but the result will be a cool time-lapse movie. Feel free to adjust the INTERVAL and MAXCOUNT to record for longer period and with different periods between shots.