Making my PiRadio run at startup

Oddly, the hardest thing so far has been making my Really Very Simple RaspberryPi radio start playing by itself when you turn it on. Which is really the sort of thing you expect a radio to do, really. Not much to ask, you’d think.

First I made an internet radio with some bits of rubbish lying round the house. Then my daughter and I added a button to change the station.

I’ve been going crazy trying to figure out ways of getting the radio to start of its own accord as the Pi boots up, trying to edit rc.local, using @reboot statements in crontab – none of it worked. I thought it might be permission problems, possibly I needed to reinstall mpd and mpc as a different user… Eventually I thought of looking at the mpd (music player daemon) logs – they live in /var/log/mpd/mpd.log

Here I found that the problem was a lack of network connectivity – there were lots of ‘Couldn’t resolve host’ and ‘Network is unreachable’ errors each time I rebooted. It seems that the problem is my Edimax wifi dongle, which is taking a LONG time to get on the network.

I found, by trial and error improvement, that if I added this line to /etc/rc.local:

(sleep 65; python /home/pi/radio.py)&

the radio starts playing – eventually – by itself when you turn the Raspberry Pi on. It takes an age to start up though, so long that by the time it starts playing a radio station you’ve almost forgotten you turned it on. So I think if we do put this in a box, we’ll need to add another button as an ‘on/off’ switch that in fact just sends an ‘mpc stop’ command and leaves the Raspberry Pi running in the background. Perhaps I can find something else of use the Pi can do in the meantime…

Also, as I already have an Arduino with an LCD shield, I quite like the look of this project using an Arduino-RaspPi combo, a bit like I did with my Fridge Gizmo.

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

PiRadio part 2 – adding a button

Tonight my daughter (8) helped me add a button to my RaspberryPi internet radio, which I made out of bits of electronic rubbish lying round the house.

Tilly testing radio button

We started by going back to basics and just getting a button working. We used this tutorial and used the pull-down resistor diagram – it’s not clear on the web site but it’s the lower of the 2 circuit diagrams. We used GPIO pin 23 instead of 17, but other than that it’s just the same. Using a little breadboard, we connected 1 side of the switch to the 3.3v pin on the RaspberryPi. The other side of the switch is connected via a 1K resistor to RaspberryPi GPIO pin 23, and via a 10K resistor to a GND pin on the Pi.

Adding a push-button to change channel on my PiRadio

We then used a simple bit of code to test the button, incrementing and printing a variable called ‘station’ by 1 when the button is pressed. As in the tutorial we found that pressing the button even briefly caused the station counter to go up to 188. As far as I know, there’s no BBC Radio 188, so we had to add the debounce code. This made all the difference – now the counter just went up by 1 each time we pressed the button, even if we held it down for a long time.

Then we wrote some code to get MPC to play the next station in its playlist every time we press a button. I’ve already added 7 stations, BBC Radio 1 – 6 and NPR. (I used to work for NPR, and have a soft spot for them). If the station number gets higher than 7 (NPR), it gets set back to 1 (BBC Radio 1).

PiRadio - adding a button

We called this code radio.py and ran it by typing sudo python radio.py at the command line.

We noticed initially that the radio didn’t come on when we ran the Python script, only when we pressed the button, and it jumped straight to Radio 2. So we added a line os.system(“mpc play 4″) near the top to start it playing from the get-go. I also set it to start playing Radio 4 by default, as that’s the station I probably listen to most of all.

We had a discussion about how real radios behave – we found that most radios remember which station you were listening to last time, and tune to that station when you turn them on. Next time we’ll see how we can make our radio do this.

Here’s our simple radio code:

#!/usr/bin/env python

import RPi.GPIO as GPIO
import time
import os

GPIO.setmode(GPIO.BCM)
GPIO.setup(23, GPIO.IN)

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

#initialise previous input variable to 0
prev_input = 0
while True:
  #take a reading
  input = GPIO.input(23)
  #if the last reading was low and this one high, do stuff
  if ((not prev_input) and input):
    print("Button pressed")
    station += 1
    if station > 7:
       station = 1
    print(str(station))
    os.system("mpc play " + str(station))

  #update previous input
  prev_input = input
  #slight pause to debounce
  time.sleep(0.05)
Posted in Raspberry Pi, Raspbian, thrift | Tagged , , | 1 Comment

Making an internet radio from a Griffin iMic and RaspberryPi

IMPORTANT UPDATE – July 2015: BBC Radio addresses have changed – see here for more info. And Fip has moved to http://audio.scdn.arkena.com/11016/fip-midfi128.mp3

Continuing the theme of making something useful out of unloved gadgets lying round the house, today I started to build some sort of audio device from another unused Raspberry Pi and an ancient Griffin iMic – this was a silver hockey-puck shaped USB audio IO device that I bought donkeys years ago for my Bondi Blue iMac. I also found some old powered PC speakers that had been in the loft, and a random old power supply.

I’m not sure if I’m eventually going to make an internet radio or something to stream audio from some networked storage yet – the first step was to get some sound out.

Music player made out of rubbish

It’s a good idea to use a USB audio device for music on the Pi, as the onboard audio jack is not good quality.

I started by following the advice here: http://computers.tutsplus.com/articles/using-a-usb-audio-device-with-a-raspberry-pi–mac-55876

In short, I copied a WAV file onto my Pi’s desktop, and rebooted the Pi up with the Griffin iMic plugged into the top USB socket (I tried it in a powered USB hub and it didn’t work). I then edited an audio config file by typing

sudo nano /etc/modprobe.d/alsa-base.conf

at the command line, and commenting out the line

options snd-usb-audio index=-2

by putting a hash sign at the front:

# options snd-usb-audio index=-2

and saving it by pressing ctrl-x and y to over-write the file.

I plugged my PC speakers into the mains, plugged the jack into the iMic’s audio out socket, then rebooted the Pi, navigated to the desktop at the command line and typed

aplay TheDaysWeMade.wav

and sweet music came forth! Didn’t even need to hunt for any drivers. (I do love the Paper Aeroplanes, but I need to get some more music on here soon!)

You can even turn the volume up at the command line by typing alsamixer

AlsaMixer showing Griffin iMic

Next steps – playing MP3 files, internet radio (I’ve asked Santa for a PiFace display control gizmo – could be the start of a beautiful but of gadget making!), who knows?

I just made a quick and dirty command line internet radio that plays BBC Radio 6music and Radio 4 by installing mpd and mpc:

sudo apt-get install mpc mpd

and then adding the BBC 6music like this:

mpc add http://bbcmedia.ic.llnwd.net/stream/bbcmedia_intl_lc_6music_p?s=1365376386&e=1365390786&h=de40a9915206c4402c73e3766dc3fec0

adding BBC Radio 4 by typing

mpc add http://bbcmedia.ic.llnwd.net/stream/bbcmedia_intl_lc_radio4_p?s=1365376126&e=1365390526&h=ed9a0642b30c422b07fbcd8683c52335

and then typing

mpc play 1

for 6music (the first one I added), mpc play 2 plays Radio 4. There are more BBC radio stations listed here.

Plenty more to do – get it to run at boot, controls, displays, a pretty box – but amazing how quickly you can turn a pile of rubbish into something useful like an internet radio.

UPDATE

I’ve since written up a very simple Raspberry Pi internet radio here, and started working on a fancier one with an LCD display and buttons for volume and station changing.

Posted in music, Raspberry Pi, thrift | Tagged | 3 Comments

Making a wireless printer from a Raspberry Pi

Untitled

I had two bits of unused hardware knocking around – a Raspberry Pi that used to run my Go Free Range weather printer (the thermal printer broke – another story), and an old Samsung ML-1510 black and white laser printer that was pretty useless to me as you can’t get up-to-date Mac OS X drivers for it. The laser printer only cost me about £40 when new, but I hate seeing things go to waste.

I wondered if it was possible to glue them together – turn the Raspberry Pi into a print server that I could leave on all the time plugged into the laser printer. And, dear reader, it is possible. And incredibly easy. Using this guide: http://www.bartbania.com/index.php/cups-raspberry-printer/ I had it working in about 30 minutes.

The only thing I’d add is that you should update your Pi by typing
sudo apt-get update
at the command line before installing CUPS. But other than that, it worked like a dream. I couldn’t see a link called ‘Do Adminstration Tasks’, but when I went to the web page for managing the print server on my laptop and tried to change the settings to share my new / old laser printer to the network, I was challenged for a password, and provided the user name and password for my Raspberry Pi login. I added the printer by turning it on, and plugging its USB socket into the Pi’s remaining free socket and clicking on the ‘add printer’ button on the web page. I searched for Samsung and found the ML-1510 in the list – a relief after lots of OS X driver hell for this printer. (The Pi’s other USB socket has a wifi dongle in it. This is a ‘headless’ Pi with no screen, keyboard or mouse, and I control it at the command line by using SSH from another computer).

Screen shot 2013-12-09 at 22.51.49

So now, on an Apple Laptop, if I go to print, I see my Samsung printer listed ‘@raspberrypi’. I fired up Word and printed a ‘Hello World!’

Turning a RaspberryPi into a wireless print server

A useless laser printer and unloved Pi are now doing useful work once more, and I’m hoping we can print our essays out a bit more crisply and more cheaply than on our ink jet printer. We can print from our iMac, our iPhones, laptops and even from our old iPad (although not, at the moment, from an Android tablet). Not a bad result for half an hour’s tinkering.

Untitled

As for my thermal printer… well, that’s another story…

Perhaps I can write a script to print the BBC Weather page every morning at 6am…

Post Script

I just remembered why I bought the laser printer in the first place – I wanted to get into Print Gocco print making. And then they stopped making Print Goccos. Why? Why why why why? The Chinese can put a probe on the moon, but the Japanese can’t make Goccos any more? Oh what days we live in…

Posted in Linux, Raspberry Pi, Raspbian, thrift | 4 Comments

Give the hudl a cudl

clock on lock screen

We got a Tesco hudl today – a very belated present for Son B. The hudl is a cheap (£119 – potentially much less if you have enough Clubcard points) 7 inch Android tablet that competes with the Amazon Kindle Fire HD (also £119) and the Google Nexus 7 (currently £199). I’m not going to compare it with those, however, as I’ve not used them. In fact my only frame of reference is a 1st generation iPad and the only other Android device I’ve used is my rooted Nook e-reader.

Cut to the chase: would I recommend it? On half a day’s use, I’d say yes. It feels well-made, it’s pretty easy to use – 11 yo Son B managed to set it up himself and install some free apps by himself – and it plays YouTube videos and runs the BBC iPlayer, which accounts for about 99% of what he used his mum’s iPad for. The cameras (front and back) are pretty awful, but we didn’t buy it for the cameras.

nice plug

It feels solid and well-made, and the supplied mains adapter and packaging shows some attention to detail. It doesn’t feel like a cheap device.

"What's an unboxing video, dad?"

As I say, setting it up was pretty easy, Son B doing most of it himself – once he’d figured out how to turn it on. He’d soon figured out he could install free apps using his YouTube login, and before long the home screen was filling up.

hudl homescreen after a few apps have been installed

It took me a while to work out how to do a screenshot. Holding down the power button brings up 3 options, but taking a screengrab isn’t one of them. Instead you have to simultaneously press the power and volume down buttons – a bit mad, but much easier than it was on older Android devices.

Installing the BBC iPlayer on our hudl

The BBC iPlayer seems to work just fine, but oddly you have to install something called the BBC Media Player separately in order to get it to work – I don’t recall ever having to do this on an iOS device.

you have to install this on the hudl to make the iPlayer work

If I can ever manage to prise it out of SonB’s hands, I might be able to report on how it copes playing other video files. It has a Micro SD card slot for additional storage, you can connect it to a computer by USB for file transfer, and it also has a micro HDMI socket so you can plug it into a TV. We don’t have the right lead to test this, but to be honest I can’t see us ever using it this way. We have leads for connecting iPods and the iPad to a TV and I think we’ve used them once.

hudl's rear-facing camera in low light

It has two cameras. They both seem pretty awful, at least in fairly dim electric light. The rear-facing camera, I assume, is supposed to be the better of the two, but it had a horrible yellow cast on it. The front-facing web cam, if anything, looked slightly better (if you can get past the horror of the subject in the photo – I was looking at myself, you understand). But we didn’t get it for the cameras, and my views on people filming school performances and gigs on iPads are well known. I’m sure the webcam is fine for Skype, though.

hudl's front-facing camera in low light

I was a bit worried that I’d find Android a bit clunky after using an iPhone and iPad, especially as the rooted Nook e-reader gives a less than seamless user experience. But the implementation of Android on the hudl is just fine – it hasn’t been over-customised by Tesco (there’s that T on the bottom left-hand corner and some fluff you can dismiss) and it’s fairly intuitive to an Android-newbie like me.

So it’s early days, but we’ll give the hudl a cudl. I can’t get SonB off it, so that’s a good sign…

The Hudl arrives! There goes the afternoon...

update…

playing an MP3 off SD card on hudl

I managed to have a play with the hudl myself – used the Google music player to play MP3s off the micro-SD card, and the video player to play an iPod-formatted video file. Video playback was just fine, but I’ve not tried any HD video files yet.

duck!

I also noticed that if you pull down the top of the screen you get a status widgety thing (that’s a technical term) that shows various things that are going on, and this includes some MP3 player controls. The audio quality from the built-in speakers is not great, but I plugged some good quality headphones in and the audio quality was very good indeed, if a little on the quiet side.

statusbar thingy, playing mp3 from SD card

For Halloween we watched The Woman in Black with the lights off – we could just as easily have watched Blink, though. I expected the wallpaper to reveal the word ‘duck!’

As they are currently exactly the same price, it’s interesting to have a look at this review comparing the hudl with the Amazon Kindle Fire HD. Personally I think the hudl is a better bet as it’s a more regular, open Android tablet. Yes it has Tesco stuff on it, but you can ignore / delete it (except the T button on the bottom left of the screen), but the Kindle, as far as I can tell, locks you in to Amazon’s services.

Screenshot_2013-11-01-09-57-50

(There’s also already a ‘hacking the hudl’ topic on a forum – apparently if you repeatedly tap the model number in the ‘about’ page, it calls up some diagnostics, and you can root it. Not sure why you’d want to, mind. Keep me away from this stuff!)

Screenshot_2013-11-01-09-57-15

Posted in Android | Tagged , , | 2 Comments