The Code4Lib Journal – Using a Raspberry Pi as a Versatile and Inexpensive Display Device
Mission
Editorial Committee
Process and Structure
Code4Lib
Issue 21, 2013-07-15
Using a Raspberry Pi as a Versatile and Inexpensive Display Device
This article covers the process by which a library took some unused equipment and added a cheap computing device to produce very inexpensive but effective electronic signage. Hardware and software issues as well as a step-by-step guide through the process are included.
by Edward Iglesias and Arianna Schlegel
Libraries are always short of money but rich in talent. As more recent graduates are not afraid to code–and perhaps even modify existing hardware–possibilities open. Such was the case when the Burritt Library needed a display device and did not want to pay a large amount of money for a commercial solution. We looked at a few of the options available but decided to roll our own solution using a cheap computing device called the Raspberry Pi.
The Raspberry Pi is a $35 computer running a stripped down version of Linux. It is very bare bones and does not come with a keyboard, power supply or display device. Fortunately we had all of these things lying around in the form of an old monitor, keyboard and mouse. A 5 volt power supply was scavenged from an old cell phone and in the end all we had to purchase was the Pi itself, an HDMI cable for the monitor and an SD card for the operating system.
Figure 0. Pi vs. Pen
Setting up the operating system (OS) is straightforward for anyone who has ever burned a Linux distribution onto a CD. You will need a card reader. Using the built in dd tool it was a simple matter to burn the OS onto the card.
Using the Raspberry Pi and some custom PHP on a website, we came up with a solution that cost approximately $50.00 plus time. As a comparison, a typical commercial solution starts at around $900.00 for something like this which is set up at our local coffee house.
Figure 1. Coffeehouse Display
The Problem
In the spring of 2012 the Burritt Library started checking out Kindle readers for ebooks. This was only a pilot project with 5 readers but proved wildly successful. As a result students were often asking whether there were any Kindles available. For some reason they did not think to check the online catalog first. The Circulation Department needed a way to display how many Kindles were available for checkout.
Figure 2. Initial Raspberry Pi Setup
The Solution
The newly released Raspberry Pi. This is a small $35 – $40 computer that can be loaded with a special distribution of the Linux OS (in this case Raspian) and can be used as a normal computer or hooked up to a breadboard (a physical board for prototyping electronic projects) for use with physical sensors. Since the distribution already came with a browser the idea emerged that by placing the computer hooked up to a monitor in a location at Circulation the browser could be set to a web page that auto-updates. Additionally, since the computer could be set to automatically log in and launch the browser in kiosk mode at startup there would be no need for a mouse or keyboard.
Figure 3. Kindle Counter Display
The Process
The project was divided between the two authors with Iglesias taking care of the physical hardware and formatting of the Pi while Schlegel took care of the PHP scripting that would scrape the catalog to display the item status.
Raspberry Pi Customization
In order for the display to be as low maintenance as possible, several settings had to be adjusted.
The login sequence had to be changed so that no login was required.
The boot sequence had to be changed so that upon startup the X environment (the graphical environment in Linux that lets you run browsers and other software) was started.
The default browser for Raspian, Midori, had to be invoked on startup and set to go to a particular site and stay there in full screen or kiosk mode.
The energy saving sequence that blanked out the screen after a set period of time had to be disabled.
At the time we worked on this project it was a long process to set up the Pi so that it would start up and log into the graphical interface without a password. Recent distributions of Raspian have simplified this process. The Raspberry Pi configurator launches the first time you boot up and can be summoned at any time by opening a terminal window and typing in
sudo raspi-config
This allows you to simply select an option that boots the Pi into the graphical interface under the section “boot_behaviour”. After this a very small script must be written that will invoke the browser and point it at the desired website. Just open an editor at the command line and create a file called start.sh in your home directory such as
nano /home/pi/start.sh
In this file you need only two lines. The first tells the script where to call the command from and the second invokes midori in “Fullscreen” mode and goes to the URL you define.
#!/usr/bin/sh
midori -e Fullscreen -a www.library.ccsu.edu/kindlecounter &
After this make your script executable
sudo chmod +x /home/pi/start.sh
and add your script to your LXDE autostart file
sudo nano /etc/xdg/lxsession/LXDE/autostart
Be sure to use sudo before trying to edit this file. At the bottom of the file add the line
@sh /home/pi/start.sh
Finally there was the problem of the screen not blanking out. The solution was found on a discussion on StackExchange which involved the installation of the x11 server utilities. These are a group of tools which let you control among other things when a screen goes blank. To install these tools go to the command line and type in
apt-get install x11-xserver-utils
Then it is simply a matter of editing the autostart file with
sudo nano /etc/xdg/lxsession/LXDE/autostart
Next, uncomment the @xscreensaver line in the following code block:
@lxpanel --profile LXDE
@pcmanfm --desktop --profile LXDE
#@xscreensaver -no-splash
@xset s off
@xset -dpms
@xset s noblank
@sh /home/pi/start.sh
More details can be found at http://raspberrypi.stackexchange.com/questions/752/how-do-i-prevent-the-screen-from-going-blank.
At this point the Pi should be able to be unplugged and replugged and automatically launch the browser with the specified target webpage.
Software
The software part of this project is pretty painless and quick. Additionally, it can be extrapolated to so many other uses! We are already considering setting up boards to display library announcements, notices, etc.
Figure 4. The original Kindle Counter Screen
For now, however, we have just what we call the “Kindle Counter.” To see a working example of the page, please visit: http://library.ccsu.edu/kindlecounter. (This soon will be changing to accommodate a split-screen view which incorporates a “laptop counter,” as well, but the basic work is all the same. The original page will be saved and perpetually available at: http://library.ccsu.edu/kindlecounter/justkindle.php.)
To begin, we had to figure out how we wanted to read the information from the page. The library’s Kindle holdings are listed at http://www.consuls.org/record=b3315628~S16, so we needed some way of scraping that page to read the current status of each item. The page updates immediately upon item processing, so we knew we could get current information from there, without having to figure out some way of getting access to our ILS and trying to read the status from the database. (A reminder: Do use your catalog’s persistent link functionality for this project!)
We opted to use PHP for screen scraping, following the instructions at http://www.dmxzone.com/go/4402/page-scraping/. We relied heavily on this page as a jumping off point, ensuring we could successfully execute a scrape with the sample code before making the transition to our own site.
We needed the code to indicate how many Pis were available for checkout, so we decided that the best option would be to sum up the number of times that the two phrases which indicated availability – “Available” and “Recently Returned” – occurred on the page:
$numavail = substr_count($file, "AVAILABLE");
$numrr = substr_count($file, "Recently Returned");
Originally, of course, we were simply displaying formatted text. But we wanted something that looked better – so we decided to create images of each number and store them on the web server. This was easy for us, because we currently only have 4 Kindles – it might become quite time-intensive if you are looking at dozens or hundreds of items. We originally tried using Google Fonts, but found that automatic page refreshes caused the page to flicker too much. Switching to images which resided on our web server resolved that issue. It was a simple matter of an if/else statement to check the current number of Kindles available, then choose which image to display:
$maxkindles = 4;
if ($totnumavail <= $maxkindles) {
print '';
} else {
print '
';
}
And lastly, because we wanted the Kindle Counter to be entirely autonomous, we had to make the page refresh itself – and do so regularly, so that we had a real-time display for anyone interested in whether there were any Kindles available. We opted to go with this refresh: