Temperature alerting with Temper232 and Mon

Last month I posted a Perl script for reading data from the Temper232. Once the problem of reading data is solved, the next step is to provide logging and alerting. I’m using rrdtool for logging and generating graphs. This might be the subject of a future blog post, but today I’m writing about alerting. For alert management, I turned to Jim Trocki’s Mon.

Mon has been around for a while. It’s not fancy, but as I’m planning to deploy on a Raspberry Pi, I need a light-weight solution. The fact that Mon is written in Perl helps quite a bit, as I’m already using Perl to extract data from the Temper232. This presentation gives a good overview of Mon and what it can do. debianhelp.co.uk has a nice set of instructions for configuring Mon with Debian.

I am using Debian GNU/Linux as a platform. There is a Mon package which can be installed with the following command line (as root):

apt-get install mon

Mon’s modular architecture makes it pretty easy to add new monitors. I went ahead and wrote a monitor for the Temper232, which is pasted below. On my Debian machine I saved this script as

/usr/lib/mon/mon.d/temper.monitor

Once the file is saved, we need modify its permission settings so that it is executable by the Mon daemon. To match the permission settings of other monitor scripts, I used the following command:

chmod a+x /usr/lib/mon/mon.d/temper.monitor

Here’s the source code of the monitor script:

#!/usr/bin/perl

use Device::SerialPort;
use Getopt::Std;

getopts("dt:l:h:");

# d debug mode
# t timeout period
# h high threshold
# l low threshold 

my $timeout = $opt_t || 10;
my $minTemp = $opt_l || -99;
my $maxTemp = $opt_h || 99;

my @serialports = @ARGV;
my @failed;
my $detail;

print "testing @serialports\n" if $opt_d;

foreach my $portname (@serialports){
  my $timer = $timeout;
  $port = Device::SerialPort->new($portname)||die "Couldn't open port $!\n";
  $port->user_msg(ON);
  $port->baudrate(4800);
  $port->databits(8);
  $port->parity("none");
  $port->stopbits(1);
  $port->handshake("none");
  $port->read_char_time(0);
  $port->read_const_time(1000);
  die "Couldn't open port $!\n" unless $port->write_settings;
  my $code = "\x24\x10\x05\x01\x01\x00\x32\x01\x55";
  my $count_out = $port->write($code);
  while ($timer > 0){
    # Read enough characters from port to guarantee a complete reading
    my ($count,$saw) = $port->read(10);
    if($count > 0){
      if ($saw=~m/\x24\xfe\x02(\C\C)\x55/){
        my $raw =  unpack('n*',$1);
		$raw -= 1<<16 if $raw & 1 << 15;
		my $temp = ($raw * 0.007812);
		if ($temp > $minTemp && $temp < $maxTemp){
		  print "temperature OK\n" if $opt_d;
		  $detail .= "$portname in range: $temp\n";
	    }else{
		  push @failed,$portname;
		  $detail .= "$portname out of range: $temp\n";
		  print "temperature bad\n" if $opt_d;
		}
	    print "$temp\n" if $opt_d;
	    last;
	  }
	}
	else{
	  $timer--;
	}
  }
  $code = "\x24\x11\x00\x55";
  $port->write($code);
}
print join (" ", @failed), "\n";
print $detail;

@failed == 0 ? exit 0 : exit 1;

To use this monitor, I created a “temp” hostgroup in Mon pointing to the serial device corresponding to the Temper232. In most cases this will be /dev/ttyUSB0. Mon’s a networking tool, but since the “hostname” specified in the hostgroup is the parameter passed to the monitor script, we can use this functionality to get data from a local serial port. In /etc/mon/mon.cf, my “hostgroup” section looks like this:

#
# Define groups of hosts to monitor
#
hostgroup localhost localhost 

hostgroup temp /dev/ttyUSB0

I set up the following monitoring specification for the hostgroup:

watch temp
  service temperature
    description Temp monitor
    interval 1m
    monitor temper.monitor -t 10 -l 10 -h 25
    period
      numalerts 10
      alert mail.alert bob@aol.com

The entry is fairly self-explanatory except for the switches passed to the monitor.

  • -t specifies a timeout value (in seconds) to wait for the serial port
  • -l specifies the low temperature minimum for our range (in Celsius)
  • - h specifies the high temperature maximum for our range

If the temperature reading falls outside of the acceptable range, Mon will dispatch an e-mail alert to the address specified.

The Game of Gale 1.0

The Game of Gale 1.0 is now available for download from the iTunes Store

The Game of Gale is an abstract strategy game invented by David Gale. Each player attempts to complete a connection of their own colour from one side of the board to the other.

My implementation of Gale has the following features:

  • Local multiplayer
  • AI opponent for solo play
  • The finest quality programmer art available in my neighbourhood
The Game of Gale

The Game of Gale in progress

The Game of Gale is an iPad-only release, mainly because of the way I chose to implement touch controls – players use their finger to “draw” the connection between nodes of their colour. I chose this approach because I think it feels good to use, and because it is very difficult to accidentally make a wrong move.

I learned a lot and had a lot of fun making this game. I hope you enjoy it too.

Purple Pawn was kind enough to take note of the game’s launch.
iPad Board Games gave the app a review.

TEMPer232 Perl script

One of my projects for 2012 is building a Raspberry Pi-based data collector device. Like most others, I’m still waiting for my Pi hardware. Fortunately I’ve been able to get started with a SheevaPlug that was collecting dust at Netmon. The Sheeva and the Pi share a processor architecture and both support the Debian-ARM operating system. I anticipate being able to transition to the actual Pi hardware more or less seamlessly.

I decided to start by collecting temperature data since it’s a very common data collection scenario and there are plenty of cheap sensors available.  After a bit of research I settled on the TEMPer232. The device was attractively priced and promised to be easy to work with as it contains a USB-Serial adapter compatible with the Linux ch341 driver.

The vendor’s documentation was pretty sketchy. Luckily, a guy named Silicon Owl on LiveJournal put together a nice post of his investigation into the TEMPer232. I was able to use this information to write a basic Perl script to test out my SheevaPlug temperature sensor, which I’ve pasted below. On a Debian machine you will need to install the package libdevice-serialport-perl.

SheevaPlug and Temper232

The Temper232 and it's SheevaPlug host installed in Netmon's server room.

You may want to check out my newer post which shows how to expand this script to perform alerting with Mon.

#!/usr/bin/perl

use Device::SerialPort;

my $timeout = 10;
my $port = Device::SerialPort->new("/dev/ttyUSB0")||die "Couldn't open port $!\n";

$port->user_msg(ON);
$port->baudrate(4800);
$port->databits(8);
$port->parity("none");
$port->stopbits(1);
$port->handshake("none");
$port->read_char_time(0);
$port->read_const_time(1000);
die "Couldn't write port settings: $!" unless $port->write_settings;

my $code = "\x24\x10\x05\x01\x01\x00\x32\x01\x55";
my $count_out = $port->write($code);

while ($timeout > 0){
  # Read enough characters from port to guarantee a complete reading
  my ($count,$saw) = $port->read(12);
  if($count > 0){
  # Uncomment to print hex values of data from port
  #  my $output="";
  #  @data = unpack('C*',$saw);
  #  foreach my $c (@data){
  #    $output .=  sprintf("%lx",$c) . " " ;
  #  }
  #  print "$output\t";
  if ($saw=~m/\x24\xfe\x02(\C\C)\x55/){
    $raw =  unpack('n*',$1);
    $raw -= 1<<16 if $raw & 1 << 15;
    $temp = $raw * 0.007812;
    print "$temp\n";
    last;
    }
  }
  else{
    $timeout--;
  }
}
$code = "\x24\x11\x00\x55";
$port->write($code);

SimpleLife 1.2

SimpleLife 1.2 is now available in the App Store.

The main reason for this update is to clean up the #1 user complaint with the application: it is not obvious or easy to recover from clearing the grid.  I have added a “reset” button which will allow the user to select from a number of starting patterns, including a blank grid.

In addition, I’ve cleaned up the cell graphics and added a dozen or so new starting patterns to choose from.

 

Neutron Capture 1.05

A small update to Neutron Capture (1.05) is out in the App Store. I found a bug that had the AI ignoring open goals in some corner cases, and took the opportunity to tweak the board evaluation heuristics.

I recommend it for all users as it makes the AI much “smarter.”

Neutron Capture

Neutron Capture is now available for download from the App Store.

Neutron Capture is a free fast-paced strategy game played on a 5×5 grid. Players take turns moving the neutral piece – the Neutron – and one of their own pieces.

The Neutron is captured by moving it to the player’s home row, or completely surrounding the Neutron so that it cannot be moved.

Neutron Capture supports single-player and two-player games. It is compatible with iPod Touch or iPhone running iOS 4.0+, and with iPad running iOS 3.2+.

Neutron Capture is an implementation of “Neutron,” invented by Robert A. Kraus. It includes art sourced from freeicons.eu, icons by Joseph Wain and music from incompetech.com. Many thanks to the folks who helped me test the game: Stuyvesant Parker, Robert and Jake Rock, Katherine Sartori, and Robert and Devin Waterman.

A few people have recognized Neutron Capture since it released. I’ve listed them here:

  • Apple has listed Neutron Capture as “New & Noteworthy” in 17 stores around the world.
  • freeapptracker.com promoted Neutron Capture as the free app of the day on 1/20/2011.
  • João Neto was kind enough to include a link to this page from his site.
  • iphoneica.com put together a review.
  • boardgamegeek.com took notice of the game’s release.
  • James Bruce was kind enough to mention Neutron Capture in a blog on MakeUseOf.com.

Chocolate Chomp

My new iOS project is an exploration of Combinatorial Game Theory for the iPad, iPhone and iPod Touch. I wrote the code and text, and Amanda Sinasac of Dreams & Drifters provided art and design. Chocolate Chomp is now available for download in the iTunes App Store.

Chocolate Chomp is an implementation of the game Chomp, as described by mathematician David Gale.

Chomp is a two-player strategy game which takes place on a rectangular grid of squares.

Chocolate Chomp can be played against a friend, solo against the computer, or you can watch the computer play both sides of the game. By selecting different board dimensions you can explore the different properties and winning conditions of the game.

Chocolate Chomp includes a descriptive essay about Chomp and combinatorial game theory.

A screenshot of Chocolate Chomp running on the iPhone.


iPad screenshot

Chocolate Chomp on the iPad.

Chocolate Chomp includes toolbar icons made by Joseph Wain of glyphish.com.

SimpleLife 1.1

SimpleLife 1.1 is now live on the App Store.  I’ve made a few UI improvements and added a much-needed load / save feature, along with a “revert” button that allows you to return to the last grid state you loaded or saved.  In addition I’ve added new starting patterns for the iPhone and iPad which should be a bit more interesting than a randomized grid.

I’m planning at least one more revision of SimpleLife.  I’d like to improve the grid UI and iPad “playback” performance.  If you’ve got suggestions for the next version, I’d love to hear it.

SImpleLife 1.1 Screenshot

SimpleLife 1.1 iPhone Screenshot

The new toolbar icons are courtesy of the fine folks at app-bits.

Life Lexicon

In looking for nice patterns to use in screenshots of SimpleLife, I came across the life lexicon. I found this neat little “spaceship” that fits well into the iPad version:

        OOOO..............
	O...O.........O...
	O...........OO....
	.O..O..OO.....OOO.
	......OOO......OOO
	.O..O..OO.....OOO.
	O...........OO....
	O...O.........O...
	OOOO..............

Here is the spaceship in flight in SimpleLife:

A "spaceship"

A spaceship in the iPad version of SimpleLife

Here are some examples of fuses in the iPad version of SimpleLife:

A fuse in SimpleLife

A fuse in the iPad version of SimpleLife.

A fuse in SimpleLife

A fuse that produces "blinkers"

SimpleLife

Simple Life iPhone Screenshot

John Conway’s Game of Life is a well-known cellular automaton. In Life, “cells” live on a two-dimensional grid and survive, reproduce, and die based on four simple rules:

  • Any live cell with fewer than two live neighbours dies.
  • Any live cell with more than three live neighbours dies.
  • Any live cell with two or three live neighbours lives on.
  • Any dead cell with exactly three live neighbours becomes a live cell.
  • Start with a random configuration or input your own starting configuration with touch controls.

    Wikipedia has a good introduction to the game, including several interesting patterns that you can try in SimpleLife.

    SimpleLife supports iOS devices with iOS 3.0 or higher. It is available for free download on the iTunes Store.

    Special thanks to Devin Waterman and his dad Rob for helping me out with testing on the iPod touch. Thanks, guys!