Posts Tagged ‘ mplayer

Playing with the Raspberry Pi’s camera

I recently picked up a camera module for the Raspberry Pi from Adafruit.  This post will serve to be notes to myself on how to use the dang thing.  There’s a lot of info on the web, so I’m going to collect what’s applicable to myself here.  This post will continue to evolve over time.

Note, I’m using a Macbook Air, so all software and commands are centric to OSX (10.8.5).

First off, you need some sort of stand for it.  I made one that should survive the Zombie Apocalypse out of some MicroRAX:

SAMSUNG

First Time Setup:

Based on the latest installation of Rasberian via NOOBS, the hardware installed easily, and was auto-detected by the Pi.  First time setup can be found here on raspberrypi.org.

Documentation:

Official documentation can be downloaded off of Github.  The first time setup above covers many basics.

Camera forum can be found here.

Capturing and Viewing:

Important note:  You can’t view anything over VNC, and obviously you can’t do it via a ssh terminal.  This post explains the reasons behind it.  This is however, a bummer:  To run any of the “demo” code, you need to be viewing the Pi directly over hdmi.

Super Simple Commands:

You’ll find these on all the sites:

$ raspistill -o image.jpg
$ raspivid -o video.h264
$ raspivid -o video.h264 -t 10000

1 : Capture an image
2 : Capture a (5 second, default) video, at 1920×1080 (1080p)
3 : Capture a 10 second video (in milliseconds)

Viewing a video stream from the Pi on your Mac:

Need to instal mplayer on the Mac so you can access it from the command-line.  The best luck I had was to install it via macports.

$ sudo port selfupdate
$ sudo port install mplayer

Now, thanks to a post by spudnix from this thread, here’s how you can stream video from the Pi to your Mac:

Mac shell code to start netcat listening to port 5001, and piping that to mplayer:

$ nc -l 5001 | mplayer -fps 31 -cache 1024 -

Pi shell code to start streaming vid and pipe it to netcat on port 5001, shooting it to the mac’s local ip:

$ raspivid -t 999999 -o - | nc 192.168.2.15 5001

There’s a few second lag, but it worked right away.  My guess is the lag is because of the full 1080p signal being broadcast.  Dropping the resolution down had some lag at first, then caught up after a few minutes, odd:

$ raspivid -t 999999 -o -w 640 -h 480 - | nc 192.168.2.15 5001

Record raw video, convert to mp4, play:

The h264 video the camera records is “raw”.  To make it easily viewable by the Pi or Mac (or other PC’s) it needs to be converted.  Thanks to this post, here’s how you can do it:

First, you need to install gpac on the Pi, then run MP4Box (part of that install) to do the convert:

$ sudo apt-get update
$ sudo apt-get install -y gpac
$ MP4Box -fps 30 -add myvid.h264 myvid.mp4

To play video on the Pi, you need omxviewer.  I think it may come installed iwth NOOBS now(?), but if not:

$ sudo apt-get install omxplayer

Then play in a window (again, this doesn’t work over VNC, need to be on a monitor connected to the Pi) or to the HDMI port:

$ omxplayer myvid.mp4
$ omxplayer -p -o hdmi myvid.mp4

Copy data from the Pi to the Mac:

Once you record a nice video, how do you get it to your PC?  Presuming you have a ssh connection, execute this from a shell on your Mac, to copy the data from the Pi, to the Mac:

$ scp user_name@the_pi_ip:/path/to/source/file/on/pi /path/to/destination/file/on/pc

For example:

$ scp pi@192.168.2.27:~/piVid.mp4 ~/macVid.mp4

Broadcast video to the Internet:

Using VLC

Thanks to this post, I was able to get the Pi cam streaming to the web, and viewable on my Mac (via the “middle” option they described).  It wasn’t entirely straight forward though, so these are the steps I went though:

Install VLC:

On the Pi, it’s easy:

$ sudo apt-get install vlc

On the Mac, at first, I tried to install it via Macports like so:

$ port install vlc

It took forever.  And while vlc was then available at the command line, it kept giving me missing plugin errors.  I found a App for it in my downloads folder (which I moved to Applications) and tried via the gui to “File -> Open Network…” : But it wouldn’t recognize the stream from my Pi (info below).  Soooo, I went to the official download page here, installed the App that way, and it started working!

Port Forward the Router:

I accessed my routers web page 192.168.x.x and via the “virtual servers” option, opened up port 8554 for outside listening.  I’m sure this process can vary widely per router.

Stream from the Pi:

After ssh’ing into the Pi, I executed this to start the video stream (note I knocked down the resolution from the default 1080p):

$ raspivid -w 640 -h 480 -o - -t 9999999 |cvlc -vvv stream:///dev/stdin --sout '#standard{access=http,mux=ts,dst=:8554}' :demux=h264

View via VLC:

I accessed “What’s My Ip” to find the external IP of my router.

Launching VLC, I accessed “File -> Open Network…”, and entered:

http://<ip of my router>:8554

And hit “open” :  Next popped up a (delayed by about 5 seconds) stream from my Pi’s cam.  Awesome.

Using MJPG-Streamer

It’s slightly more involved, but this tutorial shows how to broadcast video straight to a web page via MJPG-Streamer. All things considered, it’s really easy to setup.  I followed the tutorial my Miguel Mota, and it worked the first time I tried.  Nice!  :  “Raspberry Pi camera board video streaming

Miguel made two shell scripts, start_stream.sh & stop_stream.sh that handle all the heavy lifting of starting and stopping all the services:  Make a copy of them to your home dir for easy execution.  Note, I changed them to up the resolution, jpg quality, and add a password to the site.  I only made one change:  Since I previously port-forwarded port 8554, I also changed their code to use that port, rather than 9000.

To add your own password, edit start_stream.sh and change the line including the block of code below to include the “-c” stuff shown here, changing myUserName:myPassword appropriately.  Note, the -c argument must be inside the quotes, after the www, or things won’t work so well.

-o "output_http.so -p 8554 -w /opt/mjpg-streamer/www -c myUserName:myPassword"

Then browse to:

http://<ip of your router>:8554/stream_simple.html

To login and start watching from the auto-generated web page!  Looks like I’m getting around 1fps.

raspistill Image Formats

--encoding <format>

The default is jpg, but you can change it, where <format> is jpg, bmp, gif, & png.  From the docs: “Note that unaccelerated image types (gif, png, bmp) will take much longer to save than JPG which is hardware accelerated.”

If using jpg, you can set the quality via:

--quality #

Where # is a value from 1 -> 100.  They say that 75 is a good number.

Python Bindings

 picamera

Here on PyPi.  Official documentation here.  Source on Github here.  Forum discussion here.

Easy to install with pip:

$ sudo pip install picamera

I’ve successfully ran the quickstarts via Adafruit WebIDE successfully (while having the Pi hooked up over HDMI to preview the results).

picam

Homepage here.  Source on over on Github.  It returns PIL Image objects.

$ sudo pip install https://github.com/ashtons/picam/zipball/master#egg=picam

Project Links:

Refresh your Pi

It seems like I use my Raspberry Pi just often enough to forget everything about how I set it up the previous time.  Unlike an Arduino, it’s possible all sorts of new configuration \ updating will need to take place if it’s sat for a few months.  This post will serve mainly a notepad to myself on what I need to do to get it up and running again, and info on how to do fresh installs.

I’m sure this all can vary widely from user to user and Pi to Pi.

Great Linux/Unix command-line cheat-sheet: Web Site, PDF.

First Time Setup Guides

Just in case you do need to do everything from scratch:

The overall gist is:

  • Format SD
  • Download Linux distro and install on card
    • Most recently, I used Pi Filler (looks like Mac only)  : It does both the format and install.
    • Look here for other solutions.
  • Once the pi is up and running, via the settings menu:  (run > raspi-config to get there from the commandline)
    • Expand the root partition:  This is only needed for older distro installs.  If you’re using NOOBS, it should be auto-expanded.
    • Set screen overscan (if wanted)
    • Configure keyboard
    • Change password (if wanted)
    • Set locale, timezone
    • Enable ssh & boot behavior
  • Optionally you can then try to make a new user, but I’ve not found a need yet.  And when I do it, I tend to screw it all up permission-wise.
  • Install VNC (so you access the Pi’s gui remotely)

Linux Distros

I’m the last person you want to ask about which distro to use, but the two main one’s I have used are Raseberian, and Occidentalis.  Rasberian is now installed as part of NOOBS, and it’s a much easier install than in the past.

  • NOOBS : From the page:  “We recommend that first-time Pi users start by downloading and installing our New Out of Box Software (NOOBS) onto a 4GB (or larger) SD card. On first boot, this presents you with a choice of operating systems to install, including Raspbian, Pidora and two flavours of XBMC. Once you have installed an operating system, you can return to the NOOBS interface by holding down shift during boot; this allows you to switch to a different operating system, or overwrite a corrupted card with a fresh install of the current one.”
  • Occidentalis : From the page : “Our distro is based on “Wheezy” but comes with hardware SPI, I2C, one wire, and WiFi support for our wifi adapters. It also has some things to make overall hacking easier such sshd on startup (with key generation on first boot) and  Bonjour (so you can simply ssh raspberrypi.local from any computer on the local network)”

Get into the Pi

It’s been a while, how do I do that?  I forgot where my  surplus usb keyboard is & the wifey is using the spare hdmi cable…

(To do a first-time setup of SSH & VNC, see the post, “Connecting the Mac to the Pi“)

First, SSH:

This will let external machines talk to the Pi.  The primary way I interact with the Pi.
Connect the pi to the network (I use cat5).
There’s a good chance the dynamic ip has changed.  From another computer, access the router and click on the ‘DHCP Client List’ to find the new ip of the Pi.
Or, if you have the Pi plugged into a monitor \ keyboard, you can enter the below command, and look for the “inet addr”

$ sudo ifconfig

Open a shell.  ssh into the Pi:

$ ssh <pi ip address> -l <userName>

Enter password.  Hopefully it lets you in…

This can fail though on a new install.  I get this on my mac:

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@ WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED! @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!
Someone could be eavesdropping on you right now (man-in-the-middle attack)!
etc...

If this happens, lower down in the error it will have this line:

The fingerprint for the RSA key sent by the remote host is

And right below that, is an address.  Copy that address, browse (on the mac) here:

/Users/<userName>/.ssh/known_hosts

Edit that file (known_hosts), and find the section starting with the local ip of your pi.  At the end of that section, add a ‘+’, then paste in the address copied above.

When you try to re-ssh back in, it will ask you if you want to add this new host, and say “yes”.  Enter the password, and you should be good!

Copying files over SSH:

Once you have a ssh connection, you can copy data from one machine to another.

From a terminal on your PC (in my case, a Mac):

Copy data from the Pi to the PC:

$ scp pi@you_pis_ip:/path/to/source/file/on/pi /path/to/destination/file/on/pc

For example:

$ scp pi@192.168.2.27:~/piVid.mp4 ~/macVid.mp4

Copy data from your PC to the Pi:

$ scp /path/to/source/file/on/pc pi@you_pis_ip:/path/to/destination/file/on/pi

For example:

$ scp ~/macVid.mp4 pi@192.168.2.27:~/piVid.mp4

I should note that I’ve not got this to work over SSH from a terminal on the Pi.  But once a ssh connection is open, I can execute these from a terminal on my Mac just fine.

Copying files via SFTP

If you want a more robust solution to copying files to\from the Pi, SSH File Transfer Protocol (SFTP) is a good solution. The post HERE by Trevor Appleton covers how to set it up using FileZilla.  Provides a super easy way via the FileZilla gui to drag & drop files.

Second, VNC:

Once you’ve ssh’d in to the Pi, optionally you can access the desktop via VNC.

If not yet installed, install it:

$ sudo apt-get install tightvncserver

Then start the server:

$ vncserver :1

To start the server.
On your other PC, access the VNC Viewer app.
Set the VNC server address to <pi ip address>:1
Connect:  You should see your Pi’s desktop on your PC.

Update Pi Configuration

Not always needed, but if you need to access the global configuration on the pi, from the shell enter:

$ raspi-config

When done, if a reboot is needed:

$ sudo reboot

Optionally, to safely shutdown the Pi (rather than just unplugging it, which I’m told can be hard on the sd card) :

$ sudo shutdown -h now

Auto Login

Check out the post here:  Raspberry Salad Part 3: Auto-run program on startup

General linux commands

  • ifconfig : network info
  • pgrep <process name> : Return the process id
  • kill <process id> : delete the process
  • lsusb : Get attached usb device info

Setup WiFi

Adafruit has a good overview here.  Note that their section on “Setting up WiFi with the graphic interface” works, but the Pi will only start using WiFi when the gui is up.  If you want wifi to be there at the commandline as soon as the Pi boots, you need to follow their  “Setting up WiFi with the Command Line” tutorial.

Note, I’ve needed to have the pi connected to a monitor\keyboard\mouse to get this setup properly.   Having the Pi hooked up over lan and ssh’d in seems to override the WiFi with the lan, and I can’t get the IP.  Maybe I’m not smart enough.

Check to see if it’s working:

$ ping google.com

BUUUUT, I’d had problems with loosing connection over wifi.  After getting a tip from this thread, I added this line:

wireless-power off

to

/etc/network/interfaces

But that didn’t help anything.  So then I tried the recommendations from this thread (since it’s based on the chipset of the wifi dongle I got from Adafruit) to:

$ sudo nano /etc/modprobe.d/8192cu.conf

and add this code to it:

# Disable power management
options 8192cu rtw_power_mgnt=0

But after a reboot, it still didn’t work well.  In fact it got even harder to ssh in, it would constantly “Operation timed out”.  So I decided to connect the wifi dongle to my powered usb hub.  When I removed it from the Pi, it was really hot:  Power issues?

After plugging the wifi dongle into the  powered hub, and the hub into the Pi, nothing seemed to be working.  I had to power it on\off twice (over lan).  Bu by the 3rd time the wifi dongle started blinking, apparently recognized.  From there I set the Pi to pinging google. com, and my mac to ping the Pi.  So far, so good, internet radio is streaming.

So it looks like the Pi can’t provide enough, or stable enough power to the wifi dongle over it’s own usb port?   That seems… really annoying.

FINALLY, I bought a new wifi dongle:  It worked immediately.  So lesson to you all.  If you have these problems, spend $10 and get a new dongle…

Setup Camera

This post from raspberrypi.org covers it all.

A really good tutorial covering how you can capture raw  h264 videos, convert them to mp4, the play them on the Pi can be found here: Capturing HD Video With The Pi Camera Module.

And, my own setup guide here: “Playing with the Raspberry Pi’s Camera

Software Update

I use the apt-get package manager.

Update your cache:

$ sudo apt-get update

Search the cache for software:

$ sudo apt-cache search <someString>

Upgrade all the software on the Pi (can take a while… hours…)

$ sudo apt-get upgrade

Install new software:

sudo apt-get install <package>

Remove old software.  remove can leave some dependencies behind, purge deletes it all.

$ sudo apt-get remove <package>
$ sudo apt-get purge <package>

Adafruit WebIDE

The Adafruit WebIDE is a great way to program on the Pi from some other computer over ssh.  All the code is managed in the cloud via Bitbucket.   Setup guide HERE.

The general process is:

Note if you’re ‘refreshing’ an old Pi, the WebIDE may no longer recognize it.  In that case, log into your Bitbucket account, and in the “Manage Account -> Integrated Applications” menu, remove any “Consumers” and “OAut Consumers”, and then follow the setup guide to create a new one.

Packages

Scratch-pad area of different packages used based on projects I’ve done, or learned about, and probably forgotten about.

To get a list of all the packages installed:

$ dpkg -l

This post has a great overview of many packages to install: Linux & Python Packages for my Raspberry Pi

Presuming you have pip installed, you can use this to get a list of all Python packages installed:

$ pip freeze --local

Audio

Thanks to this post, I learned about these first three apps:

Alsamixer

$ alsamixer

Alsamixer is part of the distro, and has a simple command-line interface & gui.  Gives you a simple way to adjust volume, etc.

MPD & MPC

MPD is the “Music Player Daemon”.  From the page it is “an audio player that has a server-client architecture. It plays audio files, organizes playlists and maintains a music database all while using very few resources. In order to interface with it, a separate client is needed.”

One of those clients is MPC.  It is “a minimalist command line interface to MPD, not to be confused with musepack.”

To install the two at once:

$ sudo apt-get install mpc mpd

To add internet stream(s) to play (Let’s play some Groove Salad):

$ mpc add http://ice.somafm.com/groovesalad

To play a specific stream:

$ mpc play 1

MPlayer

I used MPlayer as part of my Raspberry FM internet music streamer.

Install:

$ sudo apt-get install mplayer

Play a stream:

$ mplayer http://streamer-dtc-aa01.somafm.com:80/stream/1018

Lot more MPlayer configuration from this post : “Raspberry Salad Part 2: Raspberry FM

Games

Minecraft Pi

Why wouldn’t you want to play Minecraft on you Pi?

Note, Minecraft Pi doesn’t work for me over VNC:  The application launches, but all I see is a black window.  You need to have the Pi directly connected to a monitor/keyboard/mouse.

FYI, it needs to be ran via the XWindows gui mode, so be sure to launch it first via

$ startx

Open a LXTerminal window:  Download (to your home dir), install (then remove the tar), & run:

$ cd ~
$ wget https://s3.amazonaws.com/assets.minecraft.net/pi/minecraft-pi-0.1.1.tar.gz
$ tar -zxvf minecraft-pi-0.1.1.tar.gz
$ rm minecraft-pi-0.1.1.tar.gz
$ cd mcpi
$ ./minecraft-pi

Python

First off, be sure to get pip installed:

$ sudo apt-get install python-pip

Once that’s installed, you can use pip to pull down all other Python packages needed.  This is preferred to using apt-get, this  post does a pretty good job of explaining why.  That being said, I’ve been unable to use pip to install certain packages (like matplotlib), and apt-get works fine.  However, once installed by apt-get, pip recognizes them.

See what Python packages you have installed:

$ pip freeze

RPi.GPIO

You’ll need this to access the Pi’s GPIO pins in Python:

$ sudo pip install RPi.GPIO

pyserial

$ sudo pip install pySerial

PIL

I’ve been unable to get PIL installed via pip.  But apt-get seems to work:

$ sudo apt-get install python-imaging

scipy

Again, pip fails for some reason.

$ sudo apt-get install python-scipy

OpenCV

This wil install OpenCV, and the Python bindings:

$ sudo apt-get install libopencv-dev python-opencv

Some links:

Other Hardware

Arduino

This Instructable has a really good overview of how to instal all the necessary packages to get your Pi to talk to an Arduino: “Arduino / Raspberry Pi Internet Radio“.

$ apt-get install arduino

And other Python dependencies:

$ sudo pip install nanpy
$ sudo pip install pyserial

Raspberry FM : Project finale

This weekend I finally knocked out the final hardware config for me to consider the “Raspberry FM” project complete.  What is this project exactly?

  • It’s an experiment I gave myself to combine a Raspberry Pi, Makey Makey, MicroRAX, and MPlayer to create an auto-on internet radio player.
  • I called it ‘Raspberry FM’ after a combination of the Raspberry Pi, and Soma FM, my favorite internet radio station.

Pic & vid of the final result (more pics at the bottom) :

The YouTube video shows off changing the volume & station.

Previous Steps

  1. Install MPlayer, stream some music
  2. Configuring MPlayer to play nicely with the Makey Makey, and create custom MPlayer playlists
  3. Auto-login and auto-run program on Raspberry Pi startup

Final Step: Building the enclosure, putting it all together…

There are a lot of Raspberry Pi enclosures over on Thingiverse ready for printing on my Makerbot Replicator.  But I needed something to also support the Makey Makey.  I knew I could modify one of those files, but I was interested in trying a different media, my MicroRAX kit.  I’d done some mockups with it earlier (bookholder, quad-copter frame), but nothing permanent, and this seemed like the perfect use case.  And it turned out great… other than the fact I actually had to have the Raspberry Pi in another enclosure, so that it wouldn’t short-out with all the exposed metal.  Luckily I had an acrylic laser-cut case that came with my Maker Shed Raspberry Pi Starter Kit.  Had I not had that case, I probably would have printed out a minimal one.

Other things I learned is that the Makey Makey can be driven directly off the RPi’s USB port:  No external powerd USB hub needed.

The final hardware setup was thus:

  • Makey Makey connected to Raspberry Pi over USB.
  • Raspberry Pi powered via wall-wart.
  • Raspberry Pi connected to internet via Ethernet.
  • Raspberry Pi connected to stereo system over headphone-jack.
  • Whole thing wrapped up via MicroRAX.
The current on\off functionality is just pluggin\unplugging it : After it boots up, the first station in the play list fires off, and you’re good to go.

Going further…

If I was to extend this project, these are some things I’d think about:

  • Switch out the tethered Ethernet for WiFi.
  • Hook up a battery-pack and speakers:  Make it a portable boom-box.
  • Splice in a toggle-switch for on\off.
  • Have some sort of better integration with MPlayer & Python, or use some other music player entirely:  Have LED’s that represent each station in the playlist that light up showing which one your on, or even a simple LCD screen broadcasting the station name and volume level.
  • Hook up awesome fruit to the MakeyMakey to make it a fruit activated radio turner.  That one should be pretty easy 😉
Overall it was a great learning experience for me to grasp the basics of linux, the Raspberry Pi hardware, and streaming internet radio.

 

Raspberry Salad Part 3: Auto-run program on startup

Continuing from my previous post of turning my Raspberry Pi and Makey Makey into a ‘internet radio tuner’, the next step is to get the RPi to auto-login and start playing music when booted:  I want this to be a standalone piece of hardware that won’t require a monitor and keyboard every time I want to use it.

Coming from the embedded microcontroller world of Arduino, I’m used to powering the hardware on, and it running “the code” I’ve programmed for it.  The Raspberry Pi is its own little Linux computer:  When it turns on, by default it expects a user to log-in, via password.  Then, it expects the user to “do stuff” on the computer.  But since the RPi is so small, it’s perfect for embedded systems like the Arduino.  But once embedded, how do you have it auto-login and execute code of your choosing?  Like many others on the web I encountered this issue, and like many others, I’m blogging about how I figured it out.  This is all completely new territory for me, everything was done via searching the web and trying it out.

Auto login to the Pi:

The first thing to solve is how to get the RPi to auto-login, thus removing the necessity of a connecetd keyboard & monitor (or ssh) to get things running.  This doc completely covers it: http://elinux.org/RPi_Debian_Auto_Login.  But to paraphrase, this is what you need to do:

Edit /etc/inittab , the  file that controls the startup \ initialization process of the Pi.  Do a “What is /etc/inittab” in Google for a far more robust answer on the specifics of what it does.

$ sudo nano /etc/inittab

In that file, comment out this line:

1:2345:respawn:/sbin/getty 115200 tty1

Like so:

#1:2345:respawn:/sbin/getty 115200 tty1

And place this line under it:

1:2345:respawn:/bin/login -f pi tty1 </dev/tty1 >/dev/tty1 2>&1

Replacing “pi” with the user you want auto-logged on on power-up.  Reboot the Pi, and watch it auto-login.  Magic.

Bash scripting for noobs

Before you can create a starup script, you need to know a bit about how to author it.  And when I say noob, I mean me:  I’ve done plenty of scripting in Python, Java, Mel, etc… but never in Bash.  Here’s basic stuff you need to know to make your scripts work (based off of info pulled from the web.  Repeat:  I am no expert at this):

  • The name must end in .sh
  • The first line must be : #!/bin/bash
  • You’ll need to make the script executable: $ chmod ugo+x startup.sh
  • To execute the script, you need to run it via bash:  $ bash startup.sh
  • Use ‘echo’ to print strings in the script.

How to auto-start a script on (auto) login:

I found a pretty good overview from this post:
http://www.raspberrypi.org/phpBB3/viewtopic.php?f=29&t=7192
I’ve sort or reorganized the posts to describe the different ways to o it:

/etc/rc.local : Add something to this file if you want it to execute every time the RPi is booted or rebooted.

  • Putting something into /etc/rc.local adds it to the boot sequence. The word “sequence” is important, if your code gets stuck then the boot sequence cannot proceed.
  • It is good for starting extra services, doing it this way will give you an output to the display [and is what I need] and also the ability to stop the script drop to a terminal and restart it with ease .
  • It is also true that when you put it into the /etc/rc* sequence, if it fails, it gums up your booting process (and this can be fatal). You have to be careful what you put there. Human frailty and all that…
  • Put stuff in /etc/rc.local if you want it to run on boot – it will run with root privileges (I think you could use sudo to stop that), and will run before login.
  •  You can see it running right there in the virtual console – and this is critical if it is interactive.

~/.bashrc : Add something to this file if you want it to execute every time a specific user logs in.

  • The same with rc.local, if you put a faulty command in here you may not be able to login.

/boot/boot.rc

  • Note that the RPi may have a file on the FAT partition named /boot/boot.rc which performs the same function as /etc/rc.local. This is safer because you can edit this partition from any computer.
  • In my case, I didn’t have this.

Based on that info, I decided to use the ~/.bashrc method (shown below):  In case I do something terrible, I could always login as another user (pi) and try to rectify the situation.

Author the startup script

To make this terribly obvious, I created a startup.sh script in my home dir, running the code I wanted to execute on login.  This is a modified version of the mplayer plalylist launcher code from my previous post, passing in the absolute path to the playlist to launch:

$ nano ~/startup.sh
#!/bin/bash
echo startup.sh : Launching mplayer: somafm.pls
mplayer -playlist ~/mplayer/somafm.pls

Need to make it executable:

$ chmod ugo+x startup.sh

Now that the startup script is created, edit ~/.bashrc to execute it:

$ nano ~/.bashrc

Go to the last line and add:

# Run the startup bash script:
echo .bashrc : Running startup.sh
bash startup.sh

Note I don’t give it a specific path to startup.sh:  Based on the fact startup.sh is in the home dir, it seems to execute fine from that location.  The echo statements in the above scripts are a bit extraneous considering for the most part a monitor is no longer needed, but I figured they’d be worth adding for troubleshooting purposes.

Listen to the music

At this point, reboot the Pi, plugin some headphones, and wait for the music to start streaming :)  If you’ve followed the instructions in the previous posts, you can use the Makey Makey to ‘change stations’ based on the authored playlist.

What’s next?  Packaging:  Need to come up with a slick way to package this, and provide a good tactile interface via the Makey Makey.

Go to previous: Raspberry Salad Part 2: Raspberry FM

Go to next post: Raspberry FM : Project finale

Raspberry Salad Part 2: Raspberry FM

Raspberry FM Tuner: Makey Makey hooked up to Raspberry Pi ready to change stations

Based off my previous post (which explains how to instal MPayer), I wanted to find a way to create a Raspberry Pi project combining my Makey Makey:  I figured the Makey Makey would work well as a ‘station changer’ for streaming internet radio.  I tried, for quite a while, very unsuccessfully, to get this working via a Python program wrapping PyGame’s keyboard detection:  It would lock up after the first station change.  This was all done via Adafruit’s ‘Raspberry Pi WebIDE‘, connecting in to the Pi over SSH.  I learned one very important thing from authoring code that way:

When logged into the Pi over SSH, either from the WebIDE or a PC, the Pi won’t detect the Makey Makey.

I thought I had a defective Makey Makey, until I realized it worked fine when plugged into my Mac.  Programming the Pi via the Mac over SSH had been working so well, I’d pretty much forgotten about connecting it to my TV over HDMI and working with it directly (via a USB keyboard).  Once I did that, the Makey Makey immediately started working, both in the shell, and the gui.

Once I knew the Makey Makey was functional, these were the steps I went through to turn the whole thing into an “internet radio tuner”.

Makey Makey Configuration:

  • Black alligator clip on ground, yellow on “Left Arrow”, red on “Right Arrow”.  White alligator clip on the “Up Arrow”, gray alligator clip on the “Down Arrow” (not shown in the above image).
  • Connected to the Raspberry Pi, over a powered USB hub.

Raspberry Pi Configuration:

  • Audio out connected to a stereo system (headphone jack, or in my case to tv over HDMI)
  • Connected to internet (CAT5 \ WiFi)
  • USB Keyboard hooked up (over powered USB hub) to issue commands below.  Remember, the Makey Makey won’t function when connected to over SSH.
  • In my case, video connected over HDMI so I could hook it up to my TV, to issue commands.

MPlayer Playlist Creation:

You can create playlist files for MPlayer, which can be made up of internet radio station streams.  This allows you to use the keyboard (or in our case, the Makey Makey) to advance and go back though them.  It’s a simple text file, with each line being a different item to play.  I decided to make a directory under home called /mplayer to store the playlist:

cd ~
mkdir mplayer
cd mplayer
nano somafm.pls

And inside that file (not sure why it’s showing up as double-spaced, it shouldn’t be), I added some of my favorite stations from SomaFM:

http://ice.somafm.com/groovesalad
http://ice.somafm.com/lush
http://ice.somafm.com/beatblender
http://ice.somafm.com/dronezone

You can of course enter any type of streaming data MPlayer supports.  But those are the stations I recommend 😉

Re-Configure MPlayer’s Key Bindings

By default MPlayer uses the ‘less than’ & ‘greater than’ keys < > to switch to the previous\next items in a playlist, and ‘9’ and ‘0’ to decrease/increase volume.  Those aren’t exposed on the Makey Makey by default, so we want to override that with the Left / Right arrow keys instead to control the playlist selection and Up/Down arrow keys to control the volume.  To do so, we need to create a ‘configuration file’ in the ~/.mplayer dir.  By default, I didn’t have one, so I created a new one:

cd ~/.mplayer
nano input.conf

And inside that file:

RIGHT pt_step 1
LEFT pt_step -1
UP volume 5
DOWN volume -5

Launch The Radio

Everything should be ready to go: Get back to the mplayer dir with the playlist, and launch MPlayer:

cd ~/mplayer
mplayer -playlist somafm.pls

At this point, music should start streaming in.  If you press the “Right Arrow” on the Makey Makey, it should advance to the next item in the playlist, until you hit the end.  The “Left Arrow” will go back through the previous playlists.  Pressing Up/Down Arrows should increase/decrease the volume in steps of 5.

MPlayer resource links:

I had a really hard time tracking down MPlayer command-line info:  Here’s docs/solutions on how to find pertinent info:

What’s next?

  • Figure out how to launch this automatically on Pi startup.  If I don’t have a keyboard and TV laying around, how can this just ‘turn on’ by itself?
  • Make a nice enclosure for the whole thing.
  • Make some sort of cool interface for the Makey Makey.  Fruit is the first choice…. but I’m trying to think of something more permanent.

Go to Part 1: Raspberry Salad…  |  Raspberry Salad Part 3: Auto-run program on startup