The 2nd Raspberry Pi in the House Now Controls a Closet Fan

When I remodeled my home office last year, I put a (bathroom) fan into the ceiling of the server closet. This would allow the heat from the servers leave the house via the attic, without heating my office up too much in the summer. It gets quite a bit a sun and heat even without the servers.

I wired that ceiling fan to a Raspberry Pi (mounted in the attic), which is also connected to half a dozen 1-wire thermometers that mostly poke through the ceiling wall board in my office, the closet, the hallway and so forth.

To my great embarrassment, I didn’t have any software for it. The way to turn the fan on and off was via ssh. Which is just a tad less user-friendly than it should have been, even for a geek like me. Until yesterday, when I hacked together this gem (in Github). Here’s the money quote:

my $fanOn = 0;
if( $temperatures->{'Server closet'} > 30 ) {
    if( $temperatures->{'Server closet'} - $temperatures->{'Office'} > 3 ) {
        $fanOn = 1;
    }
}

UBOS::Utils::myexec( "gpio mode $gpioDev out" );
UBOS::Utils::myexec( "gpio write $gpioDev $fanOn" );

printf "Fan is $fanOn (%.2f - %.2f = %.2f)\n", $temperatures->{'Server closet'}, $temperatures->{'Office'}, $temperatures->{'Server closet'} - $temperatures->{'Office'} ;

Isn’t that a beautiful control algorithm in all its complexity? :-P My former co-workers in control design better not see this. But then, today I find this in my systemd journal:

Jul 07 20:00:13 attic perl[3008]: Fan is 0 (29.56 - 25.56 = 4.00)
Jul 07 20:00:13 attic systemd[1]: Started Run fan.pl.
Jul 07 20:15:06 attic systemd[1]: Starting Run fan.pl...
Jul 07 20:15:12 attic perl[3059]: Fan is 0 (29.88 - 25.75 = 4.12)
Jul 07 20:15:12 attic systemd[1]: Started Run fan.pl.
Jul 07 20:30:06 attic systemd[1]: Starting Run fan.pl...
Jul 07 20:30:12 attic perl[3102]: Fan is 1 (30.06 - 25.88 = 4.19)
Jul 07 20:30:12 attic systemd[1]: Started Run fan.pl.
Jul 07 20:45:06 attic systemd[1]: Starting Run fan.pl...
Jul 07 20:45:12 attic perl[3143]: Fan is 0 (29.94 - 26.12 = 3.81)
Jul 07 20:45:12 attic systemd[1]: Started Run fan.pl.
Jul 07 21:00:06 attic systemd[1]: Starting Run fan.pl...
Jul 07 21:00:13 attic perl[3185]: Fan is 1 (30.50 - 26.25 = 4.25)

It’s working as it is supposed to! Better than nothing.