├── .gitignore ├── LICENSE ├── README.md └── radio.py /.gitignore: -------------------------------------------------------------------------------- 1 | *.py[cod] 2 | 3 | # C extensions 4 | *.so 5 | 6 | # Packages 7 | *.egg 8 | *.egg-info 9 | dist 10 | build 11 | eggs 12 | parts 13 | bin 14 | var 15 | sdist 16 | develop-eggs 17 | .installed.cfg 18 | lib 19 | lib64 20 | __pycache__ 21 | 22 | # Installer logs 23 | pip-log.txt 24 | 25 | # Unit test / coverage reports 26 | .coverage 27 | .tox 28 | nosetests.xml 29 | 30 | # Translations 31 | *.mo 32 | 33 | # Mr Developer 34 | .mr.developer.cfg 35 | .project 36 | .pydevproject 37 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2014 Giles Booth 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of 6 | this software and associated documentation files (the "Software"), to deal in 7 | the Software without restriction, including without limitation the rights to 8 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 9 | the Software, and to permit persons to whom the Software is furnished to do so, 10 | subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 17 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 18 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 19 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 20 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | pi-radio 2 | ======== 3 | 4 | ##A barebones Raspberry Pi internet radio 5 | 6 | This is a Python script for what I think is the simplest functional internet radio you can make with a Raspberry Pi. 7 | 8 | You will need: 9 | 10 | - A RaspberryPi with a fresh headless install of Raspbian – this means you set it not to boot into a graphical environment when it starts up. 11 | - To install the Music Player Daemon and Client mpd & mpc and configure some online radio stations. 12 | - A push button. 13 | - A 10k and a 1k ohm resistor, and some way of wiring them together (such as a breadboard) and some way of connecting 3 wires to pins on the RaspberryPi. 14 | - Headphones or some powered speakers. 15 | - Optional: USB wifi dongle to make your radio, er, wireless. 16 | 17 | First, log into your Pi at the command line. Ensure it’s connected to the internet and update it by typing 18 | ``` 19 | sudo apt-get update 20 | ``` 21 | 22 | Then install mpd (music player daemon) and mpc (client) by typing the following: 23 | ``` 24 | sudo apt-get install mpc mpd 25 | ``` 26 | 27 | Add some internet radio stations by typing this at the command line to add BBC Radio 1: 28 | ``` 29 | mpc add http://bbcmedia.ic.llnwd.net/stream/bbcmedia_intl_lc_radio1_p?s=1365376033&e=1365390433&h=a0fef58c2149248d6bff1f7b7b438931 30 | ``` 31 | 32 | There are more stations BBC listed here: http://thenated0g.wordpress.com/2013/06/06/raspberry-pi-add-bbc1-6-radio-streams-and-mpc-play-command/ 33 | 34 | I added BBC radios 1-6, and also added US public radio NWPR and the French station Fip, leaving me with 8 stations in total. Here’s how I added Fip (it’s a super-cool French music station): 35 | ``` 36 | mpc add http://mp3.live.tv-radio.com/fip/all/fip-32k.mp3 37 | ``` 38 | 39 | And for NPR try: 40 | ``` 41 | mpc add http://69.166.45.47:8000 42 | ``` 43 | 44 | Do try and add the stations in the order you want them to cycle through – you can re-order them using mpc at the command line, but it’s much easier to get them right first time. (I didn’t). 45 | 46 | Test mpc is working by typing 47 | ``` 48 | mpc play 1 49 | ``` 50 | at the command line, and you should hear Radio 1 (or whichever station you added first) coming out of the Pi’s headphone jack. You can adjust the volume of your sound device by typing 51 | ``` 52 | alsamixer 53 | ``` 54 | at the command line. You get a graphical mixer in the command line which is pretty intuitive. 55 | 56 | You can also adjust volume in mpc by typing: 57 | ``` 58 | mpc volume +5 59 | ``` 60 | or + or – any number you fancy. 61 | 62 | Then type 63 | ``` 64 | mpc stop 65 | ``` 66 | to make the horrible noise go away. 67 | 68 | Pi-Radio schematic 69 | 70 | Using a little breadboard, connect one side of your push button 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. You can find a good diagram of the pins here: http://elinux.org/RPi_Low-level_peripherals 71 | 72 | Now save **radio.py** in the home directory /home/pi. The script assumes you have 8 stations set up – if you have a different number, change the 8 to your number of radio stations. 73 | 74 | Then test it by typing 75 | ``` 76 | sudo python radio.py 77 | ``` 78 | at the command line. The radio should play, and when you press the button it should change up through the channels, cycling back to 1 when it passes 8. 79 | 80 | Next, to make it run automatically at start up, type 81 | ``` 82 | sudo nano /etc/rc.local 83 | ``` 84 | and add the following line before the exit command: 85 | ``` 86 | (sleep 65; python /home/pi/radio.py)& 87 | ``` 88 | The 'sleep 65' is needed because my Pi has a USB wifi dongle which takes an eternity (well, a minute) to get on the network. If your Pi is connected to the internet by ethernet, you could probably make the sleep time an awful lot shorter. 89 | Save it by typing ctrl-x. Reboot your Pi, and **enjoy**! 90 | 91 | 92 | 93 | -------------------------------------------------------------------------------- /radio.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # Bare bones simple internet radio 3 | # www.suppertime.co.uk/blogmywiki 4 | 5 | import RPi.GPIO as GPIO 6 | import time 7 | import os 8 | 9 | GPIO.setmode(GPIO.BCM) 10 | GPIO.setup(23, GPIO.IN) 11 | 12 | # sets initial station number to channel 8 13 | station = 8 14 | 15 | os.system("mpc play " + str(station)) 16 | 17 | #initialise previous input variable to 0 18 | prev_input = 0 19 | while True: 20 | #take a reading from pin 23 21 | input = GPIO.input(23) 22 | #if the last reading was low and this one high, increase channel by 1 23 | if ((not prev_input) and input): 24 | # assumes you have 8 radio stations configured 25 | station += 1 26 | if station > 8: 27 | station = 1 28 | os.system("mpc play " + str(station)) 29 | 30 | #update previous input 31 | prev_input = input 32 | 33 | #slight pause to debounce 34 | time.sleep(0.05) 35 | --------------------------------------------------------------------------------