Quick and dirty Raspberry Pi radio scheduling

Many thanks to James West for putting me onto this: http://www.bbc.co.uk/rd/blog/2013/09/prototyping-radio-experiences-with-radiodan and http://radiodan.github.io/. A lot of this seems quite complex – I like really, really simple things – but it’s got me thinking about building my own simple radio schedule, changing the channel for programmes I know I don’t want to miss.

It occured to me that I could use cron to do some really simple radio scheduling of my own on one of my Raspberry Pi radios – either the simple one or the sexy one. I tried it, and it works! I am my own scheduler: I never need miss treasured programmes again, nor indeed have to listen to ones I detest (I’ll refrain from naming any, but the words ‘hilarious opening montage’ spring to mind).

All you have to do is type

sudo crontab -e

at the command line and then add a line like this at the bottom of the file, remember to press return to make a new line at the end, and save it:

0 14 * * 5 mpc play 5

This will play Wittertainment every Friday at 2pm! Or, try this:

0 12 * * 1-5 mpc play 5

which will make sure you never miss Shelagh Fogarty‘s programme.

You can use a cron job generator like this to generate the crontab codes for you.

Next I need to tweak my LCD radio code to display the station that’s been played by the sheduler, but even so the buttons still work, so if you don’t like the scheduled programme, you can still over-ride it.

UPDATE

One way of getting the scheduled radio event to update the LCD display might be to run another script at the same time, so the cron entry would look something like this:

0 14 * * 5 mpc play 5 && /home/pi/schedLCD.py

The schedLCD.py script would look something like this:

#!/usr/bin/env python

import os
from nanpy import Arduino
from nanpy import (OneWire, Lcd)

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

L= [S.strip('\n') for S in os.popen('mpc').readlines()]
sttn = L[0][0:15]
lcd.printString(16*" ", 0, 0)
lcd.printString(sttn, 0, 0)

Not sure if this would conflict with the other Python script running the radio – I’ll have to try it!

Posted in Arduino, hardware, Linux, radio, Raspberry Pi, Raspbian | Tagged , , , , | Leave a comment

Scrolling LCD text on Arduino/Raspberry Pi

As part of my Raspberry Pi/Arduino internet radio project, I’m tinkering with getting the display to show something other than the station name and the time and date – the weather, for example.

Now we’ve been having quite a lot of weather lately, and sometimes it won’t fit in the 16 characters my LCD display will show at a time (at least on one line).

My display is an Arduino shield, and I’m using nanpy to get the Raspberry Pi to send text to the display and read button presses. I couldn’t work out if nanpy supports LCD scrolling, so I’ve written a function of my own that takes a string; if it’s 16 characters or less, it just displays it on the bottom line of the LCD. If it’s longer than 16 characters, it scrolls it across the display and then leaves the first 16 characters on the screen. This means the function finishes running and you can get on with something else.

Here’s the Python code for scrolling text on an Arduino LCD controlled via nanpy (weather code will follow, but I have it working!). I added the extra space to the end of the text being scrolled, as without it, it seemed to leave a trail of the last character.

import string
import time
from nanpy import Arduino
from nanpy import (OneWire, Lcd)

# set up Hobbytronics Arduino LCD shield
lcd = Lcd([8, 9, 4, 5, 6, 7], [16, 2])

def scrollText( scrollBlurb ):
   if len(scrollBlurb) > 16:
     padding = " " * 16
     oldText = scrollBlurb
     scrollBlurb = padding + scrollBlurb + " "
     for i in range (0, len(scrollBlurb)):
      lcd.printString(scrollBlurb[i:(i+16)], 0, 1)
      time.sleep(0.35)
     lcd.printString(oldText[:16], 0, 1)
   else:
     lcd.printString(scrollBlurb, 0, 1)

scrollText("some very long text that won't fit on the screen")
Posted in Arduino, Debian, Raspberry Pi, Raspbian | Tagged , , , , , | Leave a comment

Fip Radio

I was tinkering with my Raspberry Pi / Arduino internet radio, and stumbled upon David Hepworth talking about Fip radio. This is France Inter Paris, which plays ‘French chanson, jazz, world music, film soundtracks, alt.rock and the occasional blast of classical music’ (according to The Guardian). I suspect that the French spoken links are at least as appealing as the music. So I added Fip to my roster of radio stations on my Raspberry Pi by typing
mpc add http://mp3.live.tv-radio.com/fip/all/fip-32k.mp3
at the command line. Up until now I’ve mostly been listening to BBC stations I can hear on DAB, so it’s nice to have a station I could only listen to on the internet – makes the project more worthwhile. I could still do with some more interesting internet stations – any suggestions?

I also tweaked the Python code for the radio player itself to account for the extra station – there are 8 on my radio not 7 now.

I also added some code to remember which station I was listening to last time, so that when I turn it on it’ll play that station, which is what most real radios do. It stores the station number in a file called station.txt every time you change channel.

Here’s the code below for lcd-soundsystem.py, which is in my /home/pi directory. It’s run when I turn the Pi on by adding the line
(sleep 65; python /home/pi/lcd-soundsystem.py)&
to /etc/rc.local. The ‘sleep 65′ is needed because I’m using wifi, and it takes an age (well, over a minute) for the wifi network to come up.

The radio itself was made by plugging an LCD shield into an Arduino Uno, plugging that into a Raspberry Pi’s USB socket. The Pi has music players mpc and mpd installed, plus nanpy, which allows the Pi to talk to the Arduino. The up and down buttons on the display change the channel up and down, left and right decrease and increase the volume and the SEL button acts as a pause / mute. At the moment the PAUSE display is over-written by the clock thread – I need to work out how to get the clock thread to know what state the pause button is in.

(NB – this code below was updated May 2014 – click here.)

#!/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])

# read station number from text file
f = open('/home/pi/station.txt', 'r')
station = int(f.read())
f.close

# 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.3", 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 " + str(station))
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 > 8:
       station = 1
    print(str(station))
    os.system("mpc play " + str(station))
    getTrack()
    f = open('/home/pi/station.txt', 'w')
    f.write('%d' % station)
    f.close
  elif key == "DOWN":
    station -=1
    if station < 1:
       station = 8
    print(str(station))
    os.system("mpc play " + str(station))
    getTrack()
    f = open('/home/pi/station.txt', 'w')
    f.write('%d' % station)
    f.close
  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.1)
Posted in Arduino, Raspberry Pi | Tagged , , | 5 Comments

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)
Posted in Arduino, Raspberry Pi | Tagged , , , | Leave a comment

RaspberryPi internet radio with display

I liked the simple elegance of my Really Simple Raspberry Pi internet radio that just had a push button to change the channel… but I saw this project that connected a RaspberryPi radio to an Arduino LCD shield for displaying station names and for controlling the radio with buttons.

I happen to have an unused Arduino Uno and LCD shield lying around, so I decided to have a go at this myself. My LCD is a different kind, so I had to change the code for my shield – it’s one of these: http://www.hobbytronics.co.uk/arduino-lcd-keypad-shield

The main steps were:

1) Install mpc & mpd, add 7 internet radio stations.

2) Install nanpy.

3) Add the code below.

So far my code only changes the channel up and down, next step is to do volume as well. I had to use a USB port to drive the display, so I’m back to using the Pi’s internal headphone jack rather than USB. It’s also not very stable – sometimes it is better to keep things simple. It’s usually fairly obvious which station you’re listening to, so does a radio need an LCD display?

Here’s my rough Python code. I called it lcd-soundsystem.py. Ahem.

#!/usr/bin/env python
import time
import os
from nanpy import Arduino
from nanpy import (Lcd)
Arduino.pinMode(0, input)

lcd = Lcd([8, 9, 4, 5, 6, 7], [16, 2])

lcd.printString("MyLittleRadio v1", 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)
  lcd.printString(16*" ", 0, 1)
  print L
  print sttn

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

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()
Posted in Arduino, Raspberry Pi, Raspbian | Tagged , , , | 4 Comments