Official website for Linux User & Developer
FOLLOW US ON:
Jun
20

Raspberry Pi portable internet radio

by Les Pounder

Turn your Raspberry Pi into a portable Wi-Fi streaming radio

There are thousands of free radio stations on the internet, and with this project you can listen to all of them from one tiny little box. So let’s build our streaming radio using a Raspberry Pi, a speaker and a few odds and ends…

An example setup
An example setup

What you’ll need

A wireless internet connection
2 x momentary switches
4 x female-to-male leads (to connect your Pi to a breadboard)
2 x 220-ohm resistors
4 x male-to-male leads
Speakers connected to 3.5mm headphone jack

Step-by-step

Step 01 Let’s get set up

Firstly, we need to prepare our Pi. Using Raspbian, and a Pi connected to the internet, open a terminal and switch to the root user:

sudo su

And update your list of packages, then upgrade your Pi to the latest software:

apt-get update && apt-get upgrade -y

Step 02 Install some extra packages

We need to install the Python packages to access the GPIO. In a terminal, logged in as root, enter the following.

apt-get install python-rpi.gpio

Now install MPlayer, which is what will be playing our audio.

apt-get install mplayer

Step 03 Set up your hardware

We will be using GPIO 23 and 24 to provide connections for two push buttons.

You will need 4 female to male leads to connect GPIO 23,24,3v3 and GND to the breadboard. You will also need 4 male to male leads, and 2, 220 ohm resistors. Please refer to the diagram for wiring guidelines.

Also get your wiring sorted with this diagram
Also get your wiring sorted with this diagram

Step 04 Setup the software

Copy the code listing into a file called radio.py and put that in your home directory – we’ll use the code as is for this guide, but feel free to edit the code to suit your needs later on.

Now open a terminal and switch to root, and edit your network interface config:

nano /etc/network/interfaces

Step 05 Wi-Fi configuration

We want the Pi to automatically connect to your router via Wi-Fi during boot.

Edit your /etc/network/interfaces file to resemble this:

auto lo
iface lo inet loopback
iface eth0 inet dhcp
allow-hotplug wlan0
auto wlan0
iface wlan0 inet dhcp
wpa-ssid “ssid”
wpa-psk “password”

Replace the “ssid” and “password” with your own details, but keep the quotation marks.

Step 06 Configure the radio to start at boot

In a terminal, as root, navigate to /etc/init.d/ and then create a file called radio
using nano.

nano radio

In that file, type in the following:

#! /bin/bash
modprobe snd_bcm2835
amixer cset numid=3 1
python /home/pi/radio.py

This loads the kernel module for the sound card Amixer sets the output to the 3.5mm headphone jack (that’s what 1 means, HDMI is 2). Lastly it calls the Python script.

Step 07 Make it executable

Save and exit radio in /etc/init.d by pressing Ctrl+X and then answering yes to the prompt.

Now make radio executable by typing (as root):

chmod 755 radio

Then, as root, register radio to start on boot by typing in a terminal:

update-rc.d radio defaults

Step 08 Raspi-config

In a terminal as root, use raspi-config to change the boot behaviour of your Pi. We don’t want it to load the desktop – a terminal is all we need, as the project will not require a screen for future use…

Once complete, reboot the Pi and watch as the output from boot whizzes across the screen.

Step 09

Once the Pi has finished loading, press one of the buttons on your breadboard. In a few seconds you should hear the audio come through the speakers that you attached to the 3.5mm headphone jack.

That’s it, you have a wireless internet radio. Why not add a mute function using amixer and another momentary switch. Or even add an LCD screen to show the station details.

Code listing

#!/usr/bin env python
import time import sleep
import os
import RPi.GPIO as GPIO
# I found loads of BBC Radio streams from http://bbcstreams.com/
GPIO.setmode(GPIO.BCM)
GPIO.setup(23 , GPIO.IN)
GPIO.setup(24 , GPIO.IN)
while True:
    if GPIO.input(23)==1:
        os.system(‘sudo killall mplayer’)
        os.system(‘mplayer -playlist http://bbc.co.uk/radio/listen/live/r1.asx &’)
    if GPIO.input(24)==1:
        os.system(‘sudo killall mplayer’)
        os.system(‘mplayer -playlist http://bbc.co.uk/radio/listen/live/r6.asx &’)
    sleep(0.1);
GPIO.cleanup()

This tutorial appears in the magazine and the Raspberry Pi The Complete Manual.

  • Tell a Friend
  • Follow our Twitter to find out about all the latest Linux news, reviews, previews, interviews, features and a whole more.
    • me1010

      Generally nothing should be connected directly to a power rail. For some reason the schematic diagram doesn’t appear on the page in my browser – however, it would appear that you have connected a 220 ohm resistor between 3.3V and ground using the switch contacts as the connection point. If this is the case then the circuit and code both have serious flaws:

      1. Current will flow continuously from 3.3V to GND through the low value resistor.

      2. Without a debouncer, either software or hardware, the button presses will not register cleanly – and may register multiple presses when only one was intended.

      The resistor flaw will cause a large continuous current draw:

      E/R = I -> 3.3V / 220 ohm = 15mA

      I*E = W -> 49.5 mW

      I would recommend using a 5K or 10K ohm resistor. Then reconfigure the circuit so the switch connects to ground when pressed which then drops the voltage on the input pins from the reconfigured pull-up resistors. Also if you add a capacitor and a low value resistor, you can construct a simple switch debouncer… These improvements will make your circuit function significantly better with lower power consumption…

      Here’s a rough circuit schematic…

      http://s26.postimg.org/7uo9hhd21/switch_with_debouncer.png

    • Raj Kumar

      We do photograph men and women gonna spend a fresh world-wide-web company are often splash invalidated in intrinsic feelings.

      listen to free music