Maltese Online Radio – Raspberry Pi

So here’s the story:

  1. I woke up to Mum complaining that she can’t listen any longer to the radio from our kitchen due to noise interferance
  2. My brother suggested of buying an online radio
  3. I said “No..I can build one using the Raspberry Pi 2 and Music Player Daemon”
  4. I expected some excitement from their end but they just replied “ok”. Sad.
  5. Using the terminal I installed the player:
sudo apt-get install mpd mpc
  1. Changed the audio output to the headphone jack: https://www.raspberrypi.org/documentation/configuration/audio-config.md
  2. In the terminal I added some local radio stations using the command mpc add. I used Chrome’s Developer Tools(f12) to get these links:
Radio RTK mpc add http://stardust.wavestreamer.com:3328/Live?type=.mp3
Radio Malta 1 mpc add http://5.135.154.91:6982/
One Radio mpc add http://162.252.85.85:8202/stream/1/
Radio Malta 2 mpc add http://37.59.37.139:4954/
Magic Malta mpc add http://46.105.126.68:4542
Radio 101 mpc add http://stream.vanilla.net.mt:8000/Title1=Radio%20101Length1=-1Version=2
Radju Marija mpc add http://dreamsiteradiocp2.com:8096
bay radio mpc add http://s1.voscast.com:8132
bay easy mpc add http://s1.voscast.com:8154
bay retro mpc add http://s1.voscast.com:8156
Campus FM mpc add mms://stream.um.edu.mt/CampusFM
Classic FM mpc add http://media-ice.musicradio.com/ClassicFMMP3
Vibe FM mpc add http://s9.voscast.com:7824/
  1. From Ebay, I bought: 16 x 2 LCD Keypad Kit + DIY Transparent Acrylic Case For Raspberry Pi. I selected this product because it comes with 5 buttons and a case. http://www.ebay.co.uk/itm/New-16-x-2-LCD-Keypad-Kit-DIY-Transparent-Acrylic-Case-For-Raspberry-Pi-/112058244362?_trksid=p2047675.l2557&ssPageName=STRK%3AMEBIDX%3AIT&nma=true&si=D01jYFjgSMSs6rfWdX2eA1IH%252F%252Bo%253D&orig_cvip=true&rt=nc
  1. When the product arrived, I attached the LCD on the Pi. No soldering is required. It’s very easy. Just place the LCD board in the respective GPIOs.
  2. Followed this tutorial to enable I2C (with Raspi Config) – https://learn.adafruit.com/adafruits-raspberry-pi-lesson-4-gpio-setup/configuring-i2c
  3. Continued the setup : https://learn.adafruit.com/adafruit-16×2-character-lcd-plus-keypad-for-raspberry-pi/usage
  4. Installed the following library (can be done in step 10): https://github.com/adafruit/Adafruit_Python_CharLCD
  5. Programmed the following code (Buttons Up&Down = Volume Control; Buttons Left/Right = Skip radio; Button Select – Turn off Pi) :
#!/usr/bin/python
# Example using a character LCD plate.
import time
import Adafruit_CharLCD as LCD
import traceback
import subprocess
import os.path



def saveChannelInFile(index):
    channel_txt = open("/home/pi/Desktop/RVellaFM/channel.txt","w")
    channel_txt.write(str(index))
    channel_txt.close()
    
 

def isConnected():
    try:
        remote_server = "8.8.8.8"
        status = subprocess.call(['ping','-c','1',remote_server])#test connection with google
        if(status == 0):
            return True
        else:
            return False
    except:
        pass
    return False

def Main():
    

    # Initialize the LCD using the pins
    lcd = LCD.Adafruit_CharLCDPlate()
    lcd.clear()

    #keep repeating until internet connection is found
    while(True):
        if(isConnected()):
            lcd.clear()
            break
        else:
            lcd.clear()
            lcd.message("connecting to\n internet")
            time.sleep(1)
            

    try:

        
        radio = ['RTK','Malta1','One Radio','Malta2','Magic','101','Radju Marija','Bay','Bay Easy','Bay Retro','Campus FM','Classic FM','Vibe']
        #every element in radionLinks list is respectively mapped to the radio name found in radio list. example: RTK radio is mapped to http://stardust.wavestreamer.com:3328/Live?type=.mp3 etc
        radioLinks=['http://stardust.wavestreamer.com:3328/Live?type=.mp3','http://5.135.154.91:6982/','http://162.252.85.85:8202/stream/1/'
                    ,'http://37.59.37.139:4954/'
                    ,'http://46.105.126.68:4542','http://stream.vanilla.net.mt:8000/Title1=Radio%20101Length1=-1Version=2'
                    ,'http://dreamsiteradiocp2.com:8096','http://s1.voscast.com:8132','http://s1.voscast.com:8154'
                    ,'http://s1.voscast.com:8156','mms://stream.um.edu.mt/CampusFM','http://media-ice.musicradio.com/ClassicFMMP3'
                    ,'http://s9.voscast.com:7824/']
        index = 1

        #replace index with saved station
        channel_txt = open("/home/pi/Desktop/RVellaFM/channel.txt","r")
        index = int(channel_txt.readline())
        channel_txt.close()
     
        # create some custom characters
        lcd.create_char(1, [2, 3, 2, 2, 14, 30, 12, 0])
        lcd.create_char(2, [0, 1, 3, 22, 28, 8, 0, 0])
        lcd.create_char(3, [0, 14, 21, 23, 17, 14, 0, 0])
        lcd.create_char(4, [31, 17, 10, 4, 10, 17, 31, 0])
        lcd.create_char(5, [8, 12, 10, 9, 10, 12, 8, 0])
        lcd.create_char(6, [2, 6, 10, 18, 10, 6, 2, 0])
        lcd.create_char(7, [31, 17, 21, 21, 21, 21, 17, 31])

        #turn on green light 
        lcd.set_color(0.0, 0.0, 1.0)
        

        # Make list of button value, text, and backlight color.
        buttons = ( (LCD.SELECT, 'Select', (1,1,1)),
                    (LCD.LEFT,   'Left'  , (1,0,0)),
                    (LCD.UP,     'Up'    , (0,0,1)),
                    (LCD.DOWN,   'Down'  , (0,1,0)),
                    (LCD.RIGHT,  'Right' , (1,0,1)) )

        lcd.clear()
        lcd.message("connecting: \n" + radio[index-1])    
        subprocess.call("mpc play " + str(index),shell=True) #start player
        lcd.clear()
        lcd.message("\x01 " + radio[index-1])        
        cache_oldmessage = "\x01 " + radio[index-1]
        
        while True:
            message = ""
            #bug fixed - mpc sometimes switch channel automatically due to channel error. Now index is updated constantly for such cases
            currentSongLink = subprocess.check_output('mpc current -f ["%file%"]',shell=True)
            
            tmp_index =1
            for channel in radioLinks:
                if(channel == currentSongLink):
                    index = tmp_index
                    break
                tmp_index+=1
                
            message = ("\x01 " + radio[index-1])
            
            # Loop through each button and check if it is pressed.
            for button in buttons:
                if lcd.is_pressed(button[0]):
                    
                    if(button[0] == LCD.LEFT):
						print("left button")
						index = index - 1
						if(index < 1):
							index = len(radio)
                    
						saveChannelInFile(index)
						lcd.clear()
						lcd.message("connecting:\n " + radio[index-1])
						subprocess.call("mpc play " + str(index),shell=True)
						message = ("\x01 " + radio[index-1])
                    elif(button[0] == LCD.RIGHT):
                        print("right button")
                        index = index + 1
                        if(index > len(radio)):
                            index = 1
                       
                        saveChannelInFile(index)
                        lcd.clear()
                        lcd.message("connecting: \n" + radio[index-1])
                        subprocess.call("mpc play " + str(index),shell=True)
                        message = ("\x01 " + radio[index-1])
                    elif(button[0] == LCD.UP):
                        subprocess.call("mpc volume +5" ,shell=True)
                        print("volume increased")
                    elif(button[0] == LCD.DOWN):
                        subprocess.call("mpc volume -5" ,shell=True)
                        print("volume decreased")
                    elif(button[0] == LCD.SELECT):
                        lcd.set_color(0.0, 1.0, 1.0)
                        lcd.clear()
                        lcd.message('Goodbye...')
                        time.sleep(2.0)
                        lcd.clear()
                        lcd.set_color(0.0, 0.0, 0.0)

                        subprocess.call(['poweroff'], shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
                    
                    time.sleep(0.75) #button click delay

            if(cache_oldmessage != message): 
                lcd.clear()
                lcd.message(message)
                cache_oldmessage = message
                
                    
    except Exception as ex:
        traceback.print_exc()
        lcd.set_color(1.0, 0.0, 0.0)
        lcd.clear()
        lcd.message("error")

Main()

  1. Followed the following tutorial to launch the python script automatically on start-up: https://www.dexterindustries.com/howto/auto-run-python-programs-on-the-raspberry-pi/
  2. If you are having mpc connection timeouts with newest raspbian versions, just follow the fix by user csrlima in the following forum: https://www.raspberrypi.org/forums/viewtopic.php?f=28&t=159485
  3. Tested the radio. Like a boss 🙂

unnamed-1

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s