PiRadio with clock

I’ve made my RaspberryPi internet radio much more reliable – by swapping it for my other Pi. Not sure if it’s the Pi or the wifi dongle that was dodgy – I may have a poorly Pi. (New readers – I made an internet radio from a RaspberryPi, talking to an Arduino LCD display and buttons using nanpy).

Anyway, I have tweaked the code a bit now so that the radio displays the time and date all the time (as well as the station name) and the SEL button on the LCD shield acts as a pause / mute button. The function to display the time runs as a separate thread every 30 seconds. You could make it run more often to increase the accuracy of the clock.

I thought long and hard about other functions to add, but I’m struggling a bit. An alarm? Not sure volume level display is worth it (you can hear how loud it is, after all). Maybe weather? Tweets related to the station concerned, or ‘now playing’ info if it can be extracted from the interwebs? Actually, some more radio stations would be good. Any suggestions for cool internet radio stations?

#!/usr/bin/env python
import time
import os
import thread, itertools
from nanpy import Arduino
from nanpy import Lcd

# configure pins for Hobbytronics LCD shield
Arduino.pinMode(0, input)
lcd = Lcd([8, 9, 4, 5, 6, 7], [16, 2])

# threaded function to show time twice a minute
def showTime():
   while True:
       now = time.localtime(time.time())
       lcd.printString(time.strftime("%H:%M   %d/%m/%y", now), 0, 1)
       time.sleep(30)

lcd.printString("MyLittleRadio1.2", 0, 0)
lcd.printString("by @blogmywiki  ", 0, 1)
time.sleep(2)

def getKey():
   val = Arduino.analogRead(0)
   if val == 1023:
      return "NONE"
   elif val < 100:
      return "RIGHT"
   elif val < 200:
      return "UP"
   elif val < 400:
      return "DOWN"
   elif val < 600:
      return "LEFT"
   elif val < 800:
      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
  sttn = L[0][0:15]                                                         # Pick out the Station and Track info
  lcd.printString(16*" ", 0, 0)                                            # Send it out to the LCD Display
  lcd.printString(sttn, 0, 0)
  print L
  print sttn

station = 4
os.system("mpc play 4")
getTrack()
pause = 0

# start a new thread for displaying time
thread.start_new_thread(showTime, ())

while True:
  #take a reading
  key=getKey()
  if key == "UP":
    station += 1
    if station > 7:
       station = 1
    print(str(station))
    os.system("mpc play " + str(station))
    getTrack()
  elif key == "DOWN":
    station -=1
    if station < 1:
       station = 7
    print(str(station))
    os.system("mpc play " + str(station))
    getTrack()
  elif key =="LEFT":
    os.system("mpc volume -3")
  elif key =="RIGHT":
    os.system("mpc volume +3")
  elif key == "SEL":
    if pause == 0:
      pause = 1
    elif pause == 1:
      pause = 0
    os.system("mpc toggle")
    if pause == 1:
      lcd.printString("      PAUSE     ", 0, 1)
    elif pause == 0:
      noo = time.localtime(time.time())
      lcd.printString(time.strftime("%H:%M   %d/%m/%y", noo), 0, 1)

# added for key press debounce
  time.sleep(0.05)
This entry was posted in Arduino, Raspberry Pi and tagged , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>