Anthony Kelly
Published © CC BY-NC-SA

Arduino / Raspberry Pi Internet Radio

Make an Internet Radio with the mdp music player.

Full instructions provided14,457
Arduino / Raspberry Pi Internet Radio

Things used in this project

Story

Read more

Code

file_6775.python

Python
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

max_trax = 6                                    # It would be much better to determine this value from an mpc command

def getKey():                                    # Function to Translate the analogRead values from the Keys to a Command
   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"


def getTrack():
   L= [S.strip('\n') for S in os.popen('mpc').readlines()]    # Get the Track info from the stdout of the mpc command
   station = L[0][0:15]                                                         # Pick out the Station and Track info
   track = L[0][-16:-1]
   lcd.printString(16*" ", 0, 0)                                            # Send it out to the LCD Display
   lcd.printString(station, 0, 0)
   lcd.printString(16*" ", 0, 1)
   lcd.printString(track, 0, 1)
   print L
   print station
   print track

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

while True:
   key = getKey()                                                    # Do something if a key was pressed
   if key == "RIGHT":
      track_num += 1
      if track_num > max_trax:
         track_num = max_trax
      os.system("mpc play " + str(track_num))
      getTrack()
   elif key == "LEFT":
      track_num -= 1
      if track_num < 1:
         track_num = 1
      os.system("mpc play " + str(track_num))
      getTrack()
   elif key == "SEL":
      os.system("mpc toggle")
      getTrack() 

file_6776.python

Python
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)

Credits

Anthony Kelly

Anthony Kelly

5 projects • 24 followers
Hobby Arduino user. Software Hacker. Ph.D. Electronic Engineering. Specialist in IC design, digital signal processing.

Comments