Saturday, April 5, 2014

Raspberry Pi Information Radiator: Booting into a Browser

The third post in a series for building an information radiator using the raspberry pi, today we'll look at an approach for making a raspberry pi (running raspbian) boot up directly into a web browser. This was necessary for the information radiator since it was based on Dashing, a Ruby-based web application. While there were a number of articles describing how to boot into a browser, none of them worked in a way that was both simple and worked reliably.

The Application Script


So that I didn't have to debug the application startup and the boot process together, I developed a simple, but effective startup script for the application. It assumes there is a graphical environment running before it starts. The simple script starts dashing, waits for it to be available via HTTP and then starts the midori web browser in application mode, using our dashboard URL. I created the script in the pi user's home directory as $HOME/start-dashboard.bash.
First, let's setup the script to exit immediately on failure of a command:
 
 set -o errexit  

Now, let's start Dashing:

 URL=http://localhost:3030/sampletv  
  
 ##  
 # Start the dashboard, if URL is not already available  
 ##  
 if [ "$(curl --output /dev/null --silent -w '%{http_code}' --fail $URL)" != "200" ]; then  
  cd $HOME/dashing_sample  
  dashing start >& /dev/null &  
 fi    

Starting the application took much longer than starting the web browser. This meant that the browser loaded and showed "page not found" response. At least one other browser boot approach I found used midori's -i flag to periodically reload the browser. This would have worked except that the Dashing page loads take long enough on the pi that reloading periodically would have looked awful. It also would have shown the 404 page until Dashing started up. Rather than immediately starting the browser, I decided to simply wait until the dashboard URL was available to start the browser:


 ##  
 # Wait until the dashboard is up and running  
 ##  
 until [ "$(curl --output /dev/null --silent -w '%{http_code}' --fail $URL)" == "200" ]; do  
  echo -n "Waiting for ${URL}..."  
  sleep 2  
 done  
   
 echo "Starting web browser..."  
 midori -e Fullscreen --app "$URL"  
   

With that, our application startup script is complete. It starts Dashing, waits for our dashboard to be available over HTTP and then starts the web browser in app mode using our dashboard URL. All we need to do now is to use it during the raspberry pi boot process. The complete script:

 #!/bin/bash  
   
 set -o errexit  
   
 URL=http://localhost:3030/sampletv  
   
 ##  
 # Start the dashboard, if URL is not already available  
 ##  
 if [ "$(curl --output /dev/null --silent -w '%{http_code}' --fail $URL)" != "200" ]; then  
  cd $HOME/dashing_sample  
  dashing start >& /dev/null &  
 fi  
   
 ##  
 # Wait until the dashboard is up and running  
 ##  
 until [ "$(curl --output /dev/null --silent -w '%{http_code}' --fail $URL)" == "200" ]; do  
  echo -n "Waiting for ${URL}..."  
  sleep 2  
 done  
   
 echo "Starting web browser..."  
 midori -e Fullscreen --app "$URL"  
   

Hide the Cursor with Unclutter


By installing the unclutter package, the cursor will hide itself if left inactive for a short period. To clean up the interface by automatically hiding the cursor, install unclutter:

 sudo apt-get install unclutter  

Starting the App using LXDE's Autostart


At the time of this post, raspbian ships with the lightweight X11 desktop environment, or LXDE. We can configure the pi user's LXDE autostart setup to run our application startup script automatically. This leaves the default LXDE autostart configuration intact. First we have to create a user autostart file:

 # create an LXDE config directory for the pi user  
 mkdir -p $HOME/.config/lxsession/LXDE  
   
 # Create a new autostart file using your text editor
 vi $HOME/.config/lxsession/LXDE/autostart  

Then using our text editor, we add the following to our autostart configuration:

 # Turn off the screensaver  
 xset s off  
   
 # Disable blanking the screen  
 xset s noblank  
   
 # Disable display power management features  
 xset -dpms  
   
 # Run dashboard application  
 @/home/pi/start-dashboard.bash  

The xset command is a program that allows us to change the graphical environment's screen saver and power management features. We use them here to make the screen stay on 100% of the time. If we did not do this, after a period of inactivity a screensaver would start, the screen would blank or would be powered down. The xset command is not installed by default. It is part of the x11-server-utils package. To install it:


 # Install x11-server-utils to for xset command  
 sudo apt-get install x11-xserver-utils  

With this, starting the graphical environment as the pi user will automatically start our application. At this point, you can choose to configure booting into the graphical environment any way you like. I opted for manually configure it using /etc/rc.local and disabled the normal desktop tools.

Disabling LXDE Desktop Tools


While I didn't have to, I chose to disable the usual desktop tools that start with LXDE. I didn't want to spend the extra resources since the tools weren't going to be used in kiosk mode. To do this I edited the system-wide LXDE autostart file, /etc/xdg/lxsession/LXDE/autostartand commented out the usual tools:

 ##  
 # The original autostart setup  
 ##  
 #@lxpanel --profile LXDE  
 #@pcmanfm --desktop --profile LXDE  
 #@xscreensaver -no-splash# Install x11-server-utils to for xset command  

If you find yourself needing the full desktop environment later, you need only uncomment these lines and restart the desktop environment.

Starting X11 Using Rc.local


Rather than use raspi-config or /etc/inittab to automatically boot into the graphical environment, I opted for simply starting X11 as the pi user in /etc/rc.local:

 #!/bin/sh -e  
 #  
 # rc.local  
 #  
 # This script is executed at the end of each multiuser runlevel.  
 # Make sure that the script will "exit 0" on success or any other  
 # value on error.  
 #  
 # In order to enable or disable this script just change the execution  
 # bits.  
 #  
 # By default this script does nothing.  
   
 # Start X11 as the pi user  
 su -l pi -c startx  
   
 exit 0  

At this point, you should be able to reboot your raspberry pi and the application should start, the browser should start in application mode, and the screen should not go blank or disable automatically.

No comments:

Post a Comment