Making the Raspberry Pi Boot And Run My Jukebox Program Only

I put this post together over a three day period. It attempts to document how to set up software (in this case the Raspberry Pi Convergence Jukebox software that I’ve written) so it “self boots” when a Raspberry Pi is powered up. For something that is seemingly simple I managed to spend three days before finally getting it going. Hopefully if you’re reading this I can save you some time. What complicated this “seemingly simple” task was that the Jukebox program consists of two pieces of software that need to be run together;

  1. The first program is a Python based Play Engine that operates in a terminal window. It’s function is to first catalogue all of mp3’s that make up the Jukebox’s content and then to play that content either randomly or from a playlist that is generated when a user selects a song.
  2. The second program is a Python based Tkinter GUI application that the user interacts with to select songs, monitor upcoming selections and display credits.

Both these Python applications are pretty mature having been tested for hundreds of hours. I have recently ported the software to the Raspberry Pi and have had no problems starting them from the command line so I expected the process to autostart them to be relatively simple. What I didn’t know –and what I couldn’t find any documentation on– was that autostarting a Python based Tkinter application and a Python based terminal application happen in two different autostart directories as part of the Raspian boot process. So here is what I learned…

The first thing I had to do was to set-up the proper output which in this case was using an HDMI monitor.

Setting HDMI To 720P

I found the information to set the HDMI output to 720P at http://elinux.org/RPiconfig#Video

Getting The Sound To Operate via HDMI Connection

In my previous post Convergence Jukebox Now Ported To Raspberry Pi I noted where to find the information that describes how to get a USB soundcard working on a Raspberry Pi.  However, prior to setting up my USB soundcard, I was unable to get sound via my HDMI connection. I found this link —Sound does not work with an HDMI monitor— that explained the process.

NOTE: I found I had to reverse the what I had done to enable my USB sound card in order to get the HDMI audio working.

Backing Up My Convergence Jukebox SD Card

Having done all this work to configure my SD card I thought it prudent to back up my custom Raspian SD card in case I break it trying to get the auto boot to work. To back up my Convergence Jukebox I followed the the following blog post How to Clone Your Raspberry Pi SD Card for Super Easy Reinstallations as a start. It worked…

HOWEVER: the method above described creates an image file as large as the card itself. Hence the 64 gig card I’m using –so it can hold a large number of mp3 files– took a long time to create the image file. I decided to go with it and eventually rebuild a small working version on a 2 or 4gig SD card at a future date. After all an SD disk img file only needs the os and files to get the the program running. The mp3 library can always be added to a rebuilt SD card later.

Getting My Program To Run At Bootup

I found this post “Choosing which programs run when the Pi boots” to be useful. It’s short and concise and its last section describes simply describes how to run a Python program at Bootup.

In implementing this process I discovered as the Jukebox Player Engine started that it lost the path to it’s ancillary files and the program would not run. I located the fix using the information at this link “Python os.chdir() Method“. By adding two lines of code to Engine the the problem was solved.

Once my Player Engine was happily playing songs I quickly discovered that the screen would go to sleep and go blank. Not good for a continuous Jukebox Application. I solved this problem by following the instructions at this post on how to disable screen sleep on the Raspberry Pi.

The method described in  “Choosing which programs run when the Pi boots” is great to run python terminal programs, However it turns out that is  not good for running Python Tkinter GUI programs. As mentioned previously the Convergence Jukebox GUI program uses Tkinter to generate the Jukebox GUI. In order to run a Python script that requires Tkinter one has to have the Lightweight X11 Desktop Environment (or LXDE) running prior to starting a Tkinter application. This link “Autorun browser on startup gives one a clue on how to run a GUI application once LXDE runs. This second link “Running a Python Script AFTER X GUI Starts up” shows specifically how to run a python script after the LXDE has started.

There are a few things I learned that are worth noting here.

  1. A Python terminal application is best auto started in the /etc/rc.local file as described in Choosing which programs run when the Pi boots“.
  2. A Python Tkinter GUI application is best auto started from the /etc/xdg/lxsession/LXDE/autostart file as described in “Running a Python Script AFTER X GUI Starts up“.
  3. Do not start the two Python applications at the same time. You can delay the start of a Python based Terminal program got 20 seconds by coding the rc.local file with (sleep 20;sudo python /path/mypythonprogram.pyc) &
  4. I could not get both programs to run when I placed them in the LXDE autostart file no matter how hard I tried.
  5. And finally…. At some point during my exploration my GUI stopped displaying. Eventually I believe it was because of the Python file permissions.  While my Player Engine was compiled Python code (.pyc file) my GUI program was not. Apparently if one does not log into the Raspberry Pi –as would be the case when a program is auto started at boot up– one has to make a python script executable by changing permissions. In my case I used the Raspberry Pi’s file manager as discussed in this blog post “Making A Start.

Hiding My Mouse Cursor

Once I had the Jukebox booting correctly on my Raspberry Pi I wanted to hide my mouse cursor. I discovered how to do this by reading this post called “Hide mouse cursor“.

To close you’ll find one great Raspberry Pi link at: http://www.raspberry-projects.com/pi/

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: