Instructables
Picture of Arduino / Raspberry Pi Internet Radio


This is a project for Arduino and Raspberry Pi to make an Internet Radio, aimed at intermediate skill level. Some familiarity with Linux usage will be beneficial (or access to someone who can help out if required).

Raspberry Pi runs mpd music player daemon to receive and decode the internet radio stream.
ALSA running on the Raspberry Pi provides the sound through either the Jack Socket or the HDMI output.

Arduino runs a nanpy interface code to interface with Python, providing Text output of the Radio Station playing and Button inputs to control Playback.

Objectives:
* Learn how to use the mpd/mpc on the Raspberry Pi
* Learn how to use the nanpy library for Python to interface the Pi to the Arduino
* Make a Cool Internet Radio

Step 1: You Will Need

Picture of You Will Need
IMG_7203.JPG
You will need:
* Arduino UNO
* LCD / Keyboard Shield e.g. (http://goo.gl/XAhvx)
* Raspberry Pi
* USB and Ethernet cables

Step 2: Install the Required Packages for mpd/mpc

The first thing we have to do is update Raspbian and install the required packages. Make sure you're logged in (obviously), and you have an Internet connection.

1. Update Raspbian to the latest packages
$ sudo apt-get update

2. Install the mpd / mpc packages
$ sudo apt-get install mpc mpd

This installs the Music Player Daemon (mpd) and it's client mpc.
You use the mpc client to configure mpd to add radio streams to the playlist, start and stop the player etc.




 
Remove these adsRemove these ads by Signing Up
Jazzi_p1 year ago
Here's Updated Indented code, Enjoy:

from datetime              import datetime 
from subprocess            import * 
from time                  import sleep, strftime 
from Queue                 import Queue 
from threading             import Thread
import os
from nanpy import Arduino, Lcd

Arduino.pinMode(14, input)

lcd = Lcd([8,9,4,5,6,7],[16,2])            # Setup the LCD pins for the Sainsmart Shield
lcd.printString("Jess's Web Radio",0,0)
lcd.printString("Loading" + "."*3,0,1)
sleep(5)
max_trax = 74
x = 1
loop_menu = 1
loop_radio = 1

def display_ipaddr(): 
   show_wlan0 = "ip addr show wlan0 | cut -d/ -f1 | awk '/inet/ {printf \"w%15.15s\", $2}'" 
   show_eth0  = "ip addr show eth0  | cut -d/ -f1 | awk '/inet/ {printf \"e%15.15s\", $2}'" 
   ipaddr = run_cmd(show_eth0) 
   if ipaddr == "": 
      ipaddr = run_cmd(show_wlan0) 
   lcd.printString('IP Address:',0,0)
   lcd.printString(ipaddr,0,1)
   sleep(2)

def displaymenu():    
    if x==1:
        lcd.printString("1. Display      ",0,0)
        lcd.printString("   IP Address   ",0,1)
    elif x==2:
        lcd.printString("2. Audio Output ",0,0)
        lcd.printString("   to hdmi Port ",0,1)
    elif x==3:
        lcd.printString("3. Audio Output ",0,0)
        lcd.printString("  to Analog port",0,1)
    elif x==4:
        lcd.printString("4. Audio Output ",0,0)
        lcd.printString("  Auto Sel. Port",0,1)
    elif x==5:
        lcd.printString("5. Reload the   ",0,0)
        lcd.printString("   Playlist     ",0,1)
    elif x==6:
        lcd.printString("6. ShutDown     ",0,0)
        lcd.printString("   the System   ",0,1)
    else:
        lcd.printString("7. Exit to      ",0,0)
        lcd.printString("   Main Menu    ",0,1)

def load_playlist(): 
   output = run_cmd("mpc clear") 
   output = run_cmd("/home/pi/radio_playlist.sh") 
                             
def run_cmd(cmd): 
   p = Popen(cmd, shell=True, stdout=PIPE, stderr=STDOUT) 
   output = p.communicate()[0] 
   return output

def getKey():                             
   val = Arduino.analogRead(14)
   if val == 1023:
      return "NONE"
   elif val < 100:
      return "RIGHT"
   elif val < 150:
      return "UP"
   elif val < 330:
      return "DOWN"
   elif val < 510:
      return "LEFT"
   elif val < 750:
      return "SEL"
   else:
      return "KBD_FAULT"

load_playlist()
def getTrack():
   #L= [S.strip('\n') for S in os.popen('mpc').readlines()]        # Get the Track info from the stdout of the mpc command
   output = run_cmd("mpc current")
   station = output [0:16]                                           # Pick out the Station and Track info
   track =  output [-17:-1]
   lcd.printString(station + " "*(16 - len(station)), 0, 0)
   lcd.printString(track + " "*(16 - len(track)), 0, 1)

track_num = 1                                                     # Start off on Track number 1
os.system("mpc play "+str(track_num))                             # Tell the OS to Play it

while loop_radio == 1:
   getTrack()
   loop_menu = 1
   x = 1
   key = getKey()
   if key == "UP":
      track_num += 1
      if track_num > max_trax:
         track_num = max_trax
      os.system("mpc play " + str(track_num))
      getTrack()
   elif key == "DOWN":
      track_num -= 1
      if track_num < 1:
         track_num = 1
      os.system("mpc play " + str(track_num))
      getTrack()
   elif key == "SEL":
      while loop_menu == 1:
           displaymenu()
           key = getKey()
           if key == "RIGHT":
                  os.system("mpc volume +2")
                  lcd.printString(16*" ", 0, 0)
                  lcd.printString(16*" ", 0, 1)
                  output = run_cmd("mpc volume")
                  lcd.printString("VOLUME UP:", 0, 0)
                  lcd.printString(output, 5, 1)
                        sleep(.25)
                        lcd.printString(16*" ",0,0)
                        lcd.printString(16*" ",0,1)
           elif key == "LEFT":
                  os.system("mpc volume -2")
                  lcd.printString(16*" ",0,0)
                  lcd.printString(16*" ", 0,1)
                  output = run_cmd("mpc volume")
                  lcd.printString("VOLUME DOWN:", 0, 0)
                  lcd.printString(output, 5, 1)
                        sleep(.25)
                        lcd.printString(16*" ",0,0)
                        lcd.printString(16*" ",0,1)
           elif key == "UP":
                 if x <= 1:
                     x = 7
                 else:
                    x = x - 1
           elif key == "DOWN":
                 if x >= 7:
                      x = 1
                 else:
                     x = x + 1       
           elif key == "SEL":
                 if x == 1:
                    display_ipaddr()
                    sleep(1)
               elif x == 2:
                     output = run_cmd("amixer -q cset numid=3 2")
                     lcd.printString("Audio OUT-->HDMI", 0, 0)
                     lcd.printString("output ", 0, 1)
                                sleep(.5)
                 elif x == 3:
                     output = run_cmd("amixer -q cset numid=3 1")
                     lcd.printString("Audio OUT->Analog", 0, 0)
                    lcd.printString("output ", 0, 1)
                                sleep(.5)
                 elif x == 4:
                     output = run_cmd("amixer -q cset numid=3 0")
                     lcd.printString("Audio OUT->  Auto", 0, 0)
                     lcd.printString("output ", 0, 1)
                                sleep(.5)
                 elif x == 5:
                     load_playlist()       
                        os.system('mpc play 1')
                 elif x == 6:
                     lcd.printString("Good Bye         ", 0, 0)
                     lcd.printString("Have a Nice Day  ", 0, 1)
                                output = run_cmd("mpc clear")
                                output = run_cmd("sudo shutdown now")
                elif x == 7:
                     loop_menu = 0
                                getTrack()
                                break
   elif key == "RIGHT":
                  os.system("mpc volume +2")
                  lcd.printString(16*" ",0,0)
                  lcd.printString(16*" ", 0,1)
                  output = run_cmd("mpc volume")
                  lcd.printString("VOLUME UP:", 0, 0)
                  lcd.printString(output, 5, 1)
                        sleep(.25)
                        lcd.printString(16*" ",0,0)
                        lcd.printString(16*" ",0,1)
   elif key == "LEFT":
                 os.system("mpc volume -2")
                  lcd.printString(16*" ", 0, 0)
                  lcd.printString(16*" ", 0, 1)
                  output = run_cmd("mpc volume")
                  lcd.printString("VOLUME DOWN:", 0, 0)
                  lcd.printString(output, 5, 1)
                        sleep(.25)
                        lcd.printString(16*" ",0,0)
                        lcd.printString(16*" ",0,1)

Guxtdu Jazzi_p9 months ago
Nice projekt have you the code in a file that i can use
with copy & past it brings some errors in a am not a programmer

i also use a Arduino Mega 2560 and the key's dont work :-(
Guxtdu Guxtdu9 months ago
Problems are solved
on the Mega Board the analog 14 Pin is Pin 54

the Python programm is now workung but after starting the program
the Display shows the ip an the loads the first Sender then the Raspberry
crashes :-( an i can only reset it
Jazzi_p1 year ago
I have merged this radio project with usualpanic.com web radio. The menu is added to display ip addess, set the audio port to hdmi/analog or auto. Also added the shutdown option. The volume keys are changed to left and right buttons and also it display's the volume status on the LCD, to change the stations we use up and bottom key. The Select key is used to activate the sub menu. I will upload the modified python program soon.
Another option added is to Reload of playlist. Please check the tutorial on usualpanic.com for creating radio_playlist.sh file.
AvrDon2 months ago

Hi could I use this to stream this radio station ?

http://pirateradio.rit.edu/listen-now

Thank you

NaeemA2 months ago

File "test2.py", line 6

SyntaxError: Non-ASCII character '\xc2' in file test2.py on line 6, but no encoding declared; see http://www.python.org/peps/pep-0263.html for details

WHY DO I GET THIS ERROR AND COULD SOMEONE PLEASE GIVE ME CODE TO FIX IT AS I HAVE NO KNOWLEDGE WITH PYTHON, THANK YOU
navi972 months ago
Arduino / Raspberry Pi Internet Radio

Please help

I can not translate in English

who sent me an Arduino / Raspberry Pi Internet Radio on sd card

Raspberry Pi

Arduino UNO

16X2 LCD display

reply to navi2@seznam.cz

What would be the price through pay pal

thank you

tonygo24 months ago

Email arduinolink@gmail for how to install the new version 0.9

tonygo24 months ago

Nanpy version 0.9 is out now but the old versions (0.7 & 0.8) are no longer available on the net. Get instructions for installing the new version from me. Email arduinolink@gmail.com

Tony Goodhew

lucariomike5 months ago

hi !!! can i make this radio work only whit raspberry pi and an lcd attached to it ?

akellyirl (author)  lucariomike5 months ago

Yes. I think that should be possible. A simple search revealed this article.

Arizno5 months ago

This instructable is no longer good. The firmware does not exist on the nanpy folder any more. I guess its a whole separate folder. Correct me if im wrong.

akellyirl (author)  Arizno5 months ago

I've checked the nanpy install, and it is still there!

Try the specified location:

http://pypi.python.org/packages/source/n/nanpy/nanpy-v0.7.tar.gz

issca7 months ago

hello,
I need your help once, I get no display on my display when I run the command make upload I get either an error message or it takes too long and the ssh connection breaks down so I have to start the raspberry pi new. the radio the other hand, works without problems.

I thank you for your great project

srogan18 months ago

Sorted all the indents out seems to run but Im getting

song number greater than playlist length.

Any ideas ?

Im using these mms links

BBC Radio 1 -
mms://wmlive-acl.bbc.co.uk/wms/bbc_ami/radio1/radio1_bb_live_eq1_sl0
BBC Radio 2 -
mms://wmlive-acl.bbc.co.uk/wms/bbc_ami/radio2/radio2_bb_live_eq1_sl0
BBC Radio 3 -
mms://wmlive-acl.bbc.co.uk/wms/bbc_ami/radio3/radio3_bb_live_eq1_sl0
BBC Radio 4 -
mms://wmlive-acl.bbc.co.uk/wms/bbc_ami/radio4/radio4_bb_live_eq1_sl0
BBC Radio 5 Live -
mms://wmlive-acl.bbc.co.uk/wms/bbc_ami/radio5/radio5_bb_live_eq1_sl0
BBC Radio 5 Live Extra -
mms://wmlive-acl.bbc.co.uk/wms/bbc_ami/radio5/5spxtra_bb_live_eq1_sl0
BBC 6 Music -
mms://wmlive-acl.bbc.co.uk/wms/bbc_ami/6music/6music_bb_live_eq1_sl0
BBC Radio 7 -
mms://wmlive-acl.bbc.co.uk/wms/bbc_ami/bbc7/bbc7_bb_live_eq1_sl0

bsiegel11 year ago
I get syntax errors at elif key == "LEFT":
akellyirl (author)  bsiegel11 year ago
Indentation is important in Python. Make sure the elif statement has the same number of spaces at the start of the line at the if previously.
EL84 akellyirl8 months ago
Hi, tried this tutorial, but after upload I got boxes in the upper line and the lower line stays empty. What went wrong? Can somebody help me?
YangAoG9 months ago

Hi is it possible if i do not include the arduino LCD/keypad shield? i am trying to find a guide where i can just used the raspberry pi + arduino + ethernet cable but i can't seem to find a proper guide out there. Something like this : http://www.youtube.com/watch?v=qBbhug3Jwq8 but there isn't any guide on it.

Jazzi_p1 year ago
akellyirl, thanks for indenting the code.
I have uploaded the updated code below with indents...
Thanks for a great project...
Jazzi_p1 year ago
(removed by author or community request)
Wpsinc Jazzi_p1 year ago
I like this project but I am really having some issues getting this Jazzi's python to run - think because there are no indents? (Obviously I'm a newby) Have been trying to add indents but not getting far!
damianest1 year ago
Thanks a lot for the great tutorial and project I'm having an issue though when I attempt to change stations with the keypad. I get the following errors:

i@raspberrypi ~ $ Radio Paradise - DJ-mixed modern & classic rock, world, electronica, & more: Regina Spektor - Better
[playing] #1/5   0:00/0:00 (0%)
volume: n/a   repeat: off   random: off   single: off   consume: off
['Radio Paradise - DJ-mixed modern & classic rock, world, electronica, & more: Regina Spektor - Better', '[playing] #1/5   0:00/0:00 (0%)', 'volume: n/a   repeat: off   random: off   single: off   consume: off']
Radio Paradise
Spektor - Bette
Chilltrax - The World's Chillout Channel: Maya Jane Coles - Something In The Air [Don't Put Me In Your Box]
[playing] #2/5   0:00/0:00 (0%)
volume: n/a   repeat: off   random: off   single: off   consume: off
Traceback (most recent call last):
  File "radio.py", line 51, in <module>
    getTrack()
  File "radio.py", line 33, in getTrack
    lcd.printString(station, 0, 0)
  File "build/bdist.linux-armv6l/egg/nanpy/arduinoboard.py", line 56, in wrapper
  File "build/bdist.linux-armv6l/egg/nanpy/arduinoboard.py", line 37, in _call
  File "build/bdist.linux-armv6l/egg/nanpy/arduinoboard.py", line 10, in return_value
  File "build/bdist.linux-armv6l/egg/nanpy/serialmanager.py", line 53, in readline
UnicodeDecodeError: 'ascii' codec can't decode byte 0xf8 in position 0: ordinal not in range(128)



Any suggestions?
akellyirl (author)  damianest1 year ago
Glad you liked it.

It looks to me like the station value returned from mpc is causing the error.

To debug, move the print statements before the lcd.printStrings() command. At least then you'll be able to see what's causing the problem.

As a solution I suggest making use of Python's try except commands.

See : http://wiki.python.org/moin/HandlingExceptions

For example:
try:
lcd.printString(track, 0, 1)
except:
print "track value error"

What this does is to 'try' to execute the lcd.printString command but if it throws an exception then print the message and continue with the program. That will prevent the program from reporting the error and stopping.

For an example of how it can be used see:
http://www.instructables.com/id/Flight-Simulator-with-Arduino-and-Python/

Good Luck,
Anthony