Play It Cool: Remote Monitoring of Equipment Room Temps, Page 2
The next step in this process is to translate the returned character into a temperature value. I only need the Fahrenheit value, so the following equation is what I used, where <char> contains the character retrieved from the sensor:
( ord(<char>) / 2 * 9 / 5) + 32
Unfortunately, this is one area where the Linux shell and the abundant utilities available came up short. None of the simple, command-line math tools had a built-in ord() or character-to-decimal-ASCII conversion function. Although there are several ways around this shortcoming—including building my own ASCII table for looking up values—I ended up writing a short Perl script to do the calculation for me (see Listing 4).
Listing 4: Perl script to convert a character received from the sensor into a Fahrenheit value (conv_temp.pl)
#!/usr/bin/perl -w #declare variables my ($temp, $char); $char = (shift @ARGV); $temp = ( ord($char) / 2 * 9 / 5) + 32; print "$tempn";
Now, I had the required pieces to get the temperature from the sensor:
- A communications script to send the "read" command and return the result
- A short script to do the required calculation from character to temperature value
To tie the pieces together, I used a simple Bash shell script, as shown in Listing 5:
Listing 5: Shell script to get the current temperature from the sensor (get_temp.sh)
#!/bin/sh # Get reading from sensor SENSOR='kermit get_temp.ksc' # Convert reading to Fahrenheit and display ./conv_temp.pl $SENSOR
Now that I had a complete script, I could easily schedule it to run at regular intervals, pipe the temperature to other monitoring and reporting utilities, and so on.
This article detailed the thermal problem plaguing my home office and how I chose to monitor it—including the hardware and basic scripts to read the current temperature. These same techniques can be used in a variety of applications—from systematically monitoring other connected devices, to collecting data from other systems.
The next article in this series expands on this capability, incorporating a few reporting mechanisms for passing the data on to other programs, systems, and users—while illustrating techniques that can be used for a variety of data collection and distribution purposes.
About the AuthorFreelance consultant Steve Schafer has written multiple technology books and articles. In the business world, he most recently worked in-house as COO/CFO of Progeny Linux Systems in Indianapolis. Serving as president and CEO in the company's startup years, Steve led Progeny's business and financial turnaround during the tech crash of the early 2000s. Prior to joining Progeny, he was a senior title manager for Macmillan Digital USA, overseeing the operating system and entertainment software products and tripling Macmillan's Linux product line revenue. He partnered Macmillan with Mandrake, bringing Mandrake's Linux distribution to the competitive retail market.
Page 2 of 2