Hack and / - Working on My Temper

by Kyle Rankin

In my August 2010 column, I wrote about how I used an old laptop, some spare X10 devices I had and a TEMPer USB thermometer to control my fridge so I could ferment beer in a nice, controlled environment. Since then, I've received a number of e-mail messages from Linux Journal readers talking about their own projects to create the perfect beer fridge. After reading about Arduino-powered fridges and other hardware-hacking solutions, I became jealous and began to think that my laptop solution, although simple, was overkill. My hardware-hacking skills aren't quite to Arduino levels yet, so I ultimately decided to look at other, lower-powered Linux devices I had around (it turns out I had quite a few) to see if I couldn't coax one of them into controlling my fridge.

The kind of device I needed had to meet a few criteria. One, it should be able to run Linux. Second, it ideally would have at least one USB port that I could use (with a hub if necessary) to connect my USB thermometer, and if it didn't have a serial port, also connect a USB serial adapter so I could use my X10 serial dongle. Sorry, Spykee robot, but my final choice was a pink Pogoplug (Figure 1). The Pogoplug is a nifty little Linux-powered appliance that allows you to connect your own USB hard drives and then provide that data to you wherever you are, as long as you have an Internet connection. Think of it somewhat like a personal Dropbox, except you have full control of the storage and data. There's also a good-size community around the Pogoplug that provides third-party applications and even a custom Linux distribution based on Arch Linux.

Hack and / - Working on My Temper

Figure 1. Pogoplug—Little, Pink, Different

Plugbox Linux Installation

The best things about the Pogoplug for my purposes were that it was small, low-powered, had a custom Linux distribution with package management, and most important, it had four USB ports. Once I decided to use it instead of my laptop, the next step was to get the Arch Linux-based Plugbox Linux installed on it. This distribution is hosted on www.plugapps.com, and the site provides all sorts of third-party applications and packages for Pogoplug, DockStar and SheevaPlug devices. I simply followed the install document for Plugbox Linux verbatim, so instead of reposting it here, just follow the steps at www.plugapps.com/index.php5?title=PlugApps:Pogoplug_Setboot. Be sure to follow the advice about setting up openntpd; otherwise, Plugbox's time will be way off, and you'll get strange warnings and errors as you try to install and compile software.

Essentially, Plugbox Linux installs itself on an external USB drive that you provide and then sets up the bootloader so that if the drive is inserted, it will attempt to boot from it; otherwise, it will boot from the native Pogoplug firmware. This provides a simple, relatively low-risk way to modify the device to do what you want while still being able to go back to defaults. Although I might have been able to get the native Linux install to do what I wanted, Plugbox has simple package management using Arch Linux's pacman, so I also could go back to the standard Pogoplug firmware at any time. In my case, I used a spare 1GB thumbdrive for the OS, and that seemed to be plenty.

Bottlerocket Is a Breeze

After the Plugbox installation completed, I was able to boot in to the environment and configure everything over SSH. Now, all I needed to do was repeat my steps to get the CPAN modules my TEMPer device needed, install bottlerocket, and I should be done. I guess I'm spoiled by all the packages available in Debian, but then again, I didn't expect bottlerocket to be packaged for this custom distribution. I figured I'd be able to get the CPAN modules I needed, but the real question was whether I could get bottlerocket to compile. Without it, this project would be over. It turns out it wasn't too difficult. I just used pacman to install my build environment and then downloaded and compiled bottlerocket like you would in the old days:

# pacman -S gcc make
# wget http://www.linuxha.com/bottlerocket/bottlerocket-current.tar.gz
# tar xvfz bottlerocket-current.tar.gz
# cd bottlerocket-0.04c/
# ./configure
# make
# make install

When I tested bottlerocket, I noticed something interesting. The USB-to-serial adapter I plugged in was detected and appeared to work; however, after I ran bottlerocket the first time, any subsequent execution would result in an error for /dev/ttyUSB0. I had to unload and reload that particular USB-to-serial module between each bottlerocket execution for it to work properly. I know from experience that not all USB-to-serial adapters are created equal and that some do a better job, for instance, with sending break signals to your console, so I figured this was just a case of a cheap serial adapter without full serial port support. Instead of just living with the kludge of reloading the module each time, I replaced this adapter with another one I had that I knew had excellent support for break signals and overall had better compatibility. With this new adapter, I was able to power my fridge on and off without issues.

Note: a Quick Rant about Pacman

I have to admit that Plugbox was my first foray into the Arch Linux distribution. I know a number of people who really like Arch, and I don't really have an opinion one way or the other about the distribution itself, but I wanted to say a few words about pacman. First, I love the name. Second, who came up with the command arguments? In my experience, capital letters in arguments always are reserved for when you run out of lowercase arguments (or alternatively, to do the opposite of what the lowercase argument does), and although sometimes arguments have no relation to the action you are performing, at least for most programs, the most commonly used actions are the ones with the intuitive arguments. For instance, I fully expected pacman to use -i to install a package or possibly to use a longhand --install. The -S argument it does use simply makes no sense (I know it stands for “Sync” but that's a long way from “install”), and the -Ss argument to search for packages that match a keyword makes even less sense. I know it takes only a second to get used to it, but I'd still argue there are a number of more intuitive letters to choose.

CPAN Is a Harsh Mistress

Because it was so easy to build bottlerocket, I assumed it would be a relative cakewalk to install all the Perl modules I needed using CPAN. For those of you who aren't Perl hackers, Perl provides a vast repository of extra modules on-line at www.cpan.org. If there is a particular Perl module you need, you can use the cpan binary on your local system to pull down and build those modules for your system much like a modern package manager. I figured that even if Plugbox didn't include a lot of Perl modules, I would identify the modules I needed and install them one by one, like in my previous TEMPer column. The reality was that although Plugbox did include Perl and even included the cpan binary, it didn't include a lot of the necessary modules you need for CPAN actually to work.

Honestly, everything else about this project was simple once I got CPAN working, but this part of the process took the most time and effort, and it really reminded me of what Linux was like back in the day when you would find a cool project on Freshmeat, download the source and then spend the next two days tracking down all the dependencies. I finally found the magic list of packages and libraries I needed, and in this case, I had to install a few packages from Plugbox along with a manual module compilation:

# pacman -S perl-yaml perl-xml-libxml 
# pacman -S perl-text-query perl-text-diff perl-text-reform
# wget http://search.cpan.org/CPAN/authors/id/R/RC/RCLAMP/
 ↪Text-Glob-0.08.tar.gz
# tar xfvz Text-Glob-0.08.tar.gz
# cd Text-Glob-0.08
# perl Build.PL
# perl Build test
# perl Build install

From this point, I was able to get the cpan binary to function, and I could follow the installation steps I laid out in my previous column to finish the process (I was glad I had that nicely documented for myself). Afterward, all I had to do was edit my temper.pl script so that it referenced /dev/ttyUSB0 instead of a local serial port and created a file called /etc/cron.d/temper that contained the following data:

* *  * * *   /usr/local/sbin/temper.pl

After I restarted cron (/etc/rc.d/crond restart), I was able to check my logs and see that the script was, in fact, reading the temperature and controlling the fridge just like my laptop. In case you'd like to do something similar with your Pogoplug, Listing 1 is the current iteration of the script.

Listing 1. Refrigerator Temperature Controlling Script


#!/usr/bin/perl

my $temp_min  = 67;
my $temp_max  = 69;

use 5.010;
use strict;
use warnings;
use Carp;
use Device::USB;
use Device::USB::PCSensor::HidTEMPer::Device;
use Device::USB::PCSensor::HidTEMPer::NTC;
use Device::USB::PCSensor::HidTEMPer::TEMPer;
use lib;
use Device::USB::PCSensor::HidTEMPer;

my $pcsensor  = Device::USB::PCSensor::HidTEMPer->new();
my @devices   = $pcsensor->list_devices();
my $logfile   = '/var/log/temper.log';
my $time      = localtime();
my $temperature;

foreach my $device ( @devices ){
   $temperature = $device->internal()->fahrenheit();
}

die unless $temperature;

open LOG, ">> $logfile" or die "Can't open $logfile: $!\n";

# B4 = Fridge power, B5 = Heater power

# turn on heater if I'm 1F below the low temp
if($temperature < ($temp_min - 1)){
   system('/usr/local/bin/br --port /dev/ttyUSB0 B5 ON');
   system('/usr/local/bin/br --port /dev/ttyUSB0 B4 OFF');
   print LOG "$time\t$temperature\tHON\n";
}
elsif($temperature < $temp_min){
   system('/usr/local/bin/br --port /dev/ttyUSB0 B4 OFF');
   system('/usr/local/bin/br --port /dev/ttyUSB0 B5 OFF');
   print LOG "$time\t$temperature\tOFF\n";
}
elsif($temperature > $temp_max){
   system('/usr/local/bin/br --port /dev/ttyUSB0 B4 ON');
   system('/usr/local/bin/br --port /dev/ttyUSB0 B5 OFF');
   print LOG "$time\t$temperature\tCON\n";
}
else{
   print LOG "$time\t$temperature\t\n";
}

close LOG;

Now that I've had this running without issues for a few weeks, I do like how quiet and low-power the device is. Plus, it takes up less space on the top of my fridge. The only real drawback I've seen is that there is no screen on the device. On my laptop if I wanted to check the temperature, I just had to open the lid; now, I have to ssh in to the Pogoplug. Although I used a Pogoplug for this, I imagine you could translate these steps for a number of other small Linux devices that have USB ports, like the NSLU2—provided it included Perl and a gcc build environment. Now I can use my old laptop for nobler pursuits—like maybe some day powering my smoker.

Resources

Pogoplug: www.pogoplug.com

Applications for Pogoplug, Including Plugbox: www.plugapps.com

Kyle Rankin is a Systems Architect in the San Francisco Bay Area and the author of a number of books, including The Official Ubuntu Server Book, Knoppix Hacks and Ubuntu Hacks. He is currently the president of the North Bay Linux Users' Group.

Load Disqus comments