Raspberry Pi : Camera + Ground Control

In the midst of researching Google Go (which is something I really want to make some time to learn!) I stumbled across an excellent little project called Ground Control. Designed for the Raspberry Pi, Ground Control is a framework for reporting statistics from and running commands  on your Raspberry Pi. Statistics can be any value that changes over time. The reference implementation is a statistic tracking the CPU Temperature of your Pi. Ground Control can emit statistics to a simple web interface accessible directly from off the Pi or to one of several commercial statistic capturing services such as Librato or TempoDB.

Ground Control also provides the ability to run commands on your Pi. By supplying the proper configuration, Ground Control will present buttons on its web interface allowing you to either execute a command, or even toggle two commands (to start and stop a service for example.)

The goal of this blog will be to show you how to set up Ground Control on your Pi and use it in conjunction with a Raspberry Pi camera to take photos remotely .

To get started with Ground Control you first need to download it. My preferred way of doing this is to download it directly onto the Pi. Create a directory on your Pi, and from within that directory execute this command:

wget http://jondot.github.io/groundcontrol/groundcontrol-0.0.1.tar.gz .  

This will download Ground Control as a compressed TAR file. To extract the TAR, execute this command:

tar -xvf groundcontrol-0.0.1.tar.gz  

Inside the directory you have just extracted, Ground Control has a sample configuration file (groundcontrol.json.sample) for your review. (The file is written in JSON. Here is a description of JSON if you are unfamiliar with it.)

If you open this file you will see entries for host and port (where the web interface will run), configuration for Librato and TempoDB, and a sample on/off command for XMBC. You will want make a copy of this sample (calling your copy groundcontrol.json)  and modify several parameters. For my project, I accepted most of the defaults, removed the configuration for Vibrato / TempoDB, and replcaed the sample XMBC command with one for my camera.

Here is my configuration file:

  "temperature" : "/sys/class/thermal/thermal_zone0/temp",
  "port": 4571,
  "host": "",
  "stdout" : false,
  "interval" : 30,
  "history_interval": 60,
  "history_backlog": 20,
  "controls" : {
    "camera": {
      "once" : "raspistill -t 10 -o /home/pi/`date +\"%y%m%d%H%M%S\"`.jpg"

Several things to note. First, my "camera" command has a "once" entry instead of "on" and "off" entries. This is provided by Ground Control for commands that do not have a "on" and "off" state and are simply executed once.

Also note the command:

raspistill -t 10 -o /home/pi/`date +\"%y%m%d%H%M%S\"`.jpg  

The call to raspistill invokes the camera. The -t parameter sets the delay in milliseconds before taking the picture (in my case I wanted the picture to take almost immediately.) The -o parameter specifies where to save the resulting JPG.

The /home/pi/`date +\"%y%m%d%H%M%S\"` looks complicated but is actually quite simple. I am constructing a path to save the result JPG. The /home/pi are an absolute reference to the pi user's home so this command can be executed from any context. The date ... dynamically invokes the date command everytime the command is executed, creating a unique name for the file with the date / time so that each file does not overwrite the prior one. The +\"%y%m%d%H%M%S\" is just a properly escaped format string that specifies the format of the timestamp for the date command.

Note: There are many other parameters that can be send to raspistill to control almost every aspect of the camera. I would recommend executed raspistill --help and reviewing the inline documentation.

By running ./groundcontrol -config groundcontrol.json you can validate that your configuration file is valid and that Ground Control starts correctly. Browse to http://<IP ADDRESS OF PI>:<PORT> and verify that the web interface loads correctly.

Now that is all well and good, but as soon as I close Ground Control I will lose access to my metrics and remote picture taking capability! What I really want is to have Ground Control persist across restarts of my Raspberry Pi.

Thankfully Ground Control provides an init script (found in /<GROUND CONTROL DIRECTORY>/support/init.d/groundcontrol) that can be installed in your init.d directory to have Linux start Ground Control each time it boots up. The provided init script expects the configuration and Ground Control executables to be in certain default locations, so you either need to copy them there or modify the locations in the provided init script.

I choose the former, which I recommend for simplicity. Copy your configuration file to /etc/ as follows:

sudo cp /<PATH TO GROUNDCONTROL.JSON>/ /etc/groundcontrol.json  

Copy the executables to /opt/groundcontrol/ as follows:

sudo mkdir /opt/groundcontrol  
sudo cp -r /<PATH TO GROUND CONTROL DIRECTORY>/ /opt/groundcontrol  

Finally, copy the init script to /etc/init.d/ as follows:

sudo cp /opt/groundcontrol/support/init.d/groundcontrol /etc/init.d/groundcontrol  

To verify this works, run:

/etc/init.d/groundcontrol start

Note: The first line of output may report a FAILURE (it was trying to shutdown Ground Control if it was running) but that is all right. It was just expected Ground Control to be already running.

Browse to the web interface and verify that Ground Control is up and running. If it is not, double check your configuration file to verify that it is properly formatted JSON. (You may choose to use an online validator to be certain.) If it is up and running, then install it into your Pi's startup cycle as follows:

update-rc.d groundcontrol defaults  

Reboot your Pi:

sudo reboot  

Once your Pi has finished rebooting, double check that Ground Control is up and running by browsing to the web interface.

Congratulations! You can now monitor your Pi's temperature (and any other metrics you choose to configure) as well as have remote access to your Pi's camera!

Questions? Comments? Email me at: [email protected]