Play it Cool: Solving Thermal Problems, Page 3
Implementing a Script To Monitor the Button
Many methods can be used to monitor the signals on a serial line. Most telecom programs have built-in scripting to monitor the signals and perform actions accordingly. If the scripting language cannot invoke the audio script directly, it could write a log file that another script could monitor and act on accordingly.
However, sticking with our lightweight, modular design, I chose to use a little utility called statserial, which simply displays the status of a specified serial port. For example, running statserial with no parameters displays the status of serial port 2, /dev/ttyS1, as shown in Listing 2.
Listing 2: The default statserial display
Device: /dev/ttyS1 Signal Pin Pin Direction Status Full Name (25) (9) (computer) Name ----- --- --- --------- ------ ----- FG 1 - - - Frame Ground TxD 2 3 out - Transmit Data RxD 3 2 in - Receive Data RTS 4 7 out 1 Request To Send CTS 5 8 in 0 Clear To Send DSR 6 6 in 0 Data Set Ready GND 7 5 - - Signal Ground DCD 8 1 in 0 Data Carrier Detect DTR 20 4 out 1 Data Terminal Ready RI 22 9 in 0 Ring Indicator
This display is updated every second. The statserial utility also has an option to display the status of the port in decimal form, coded using the scheme shown in Listing 3.
Listing 3: The encoding used by statserial for displaying the status of the serial port.
+---+---+---+---+---+---+---+---+---+ | 8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 | +---+---+---+---+---+---+---+---+---+ |DSR|RI |DCD|CTS|XXX|XXX|RTS|DTR|XXX| +---+---+---+---+---+---+---+---+---+
The script will only have to check bit 8 of the result (DSR). When it's set high, the script runs the audio script, and the temperature is read aloud. The final shell script is shown in Listing 4.
Listing 4: The final script to monitor the analog button (buttonsay.sh).
#!/bin/sh
DIR=/home/sschafer/officetemp
chkDSR() {
LINESTAT=`statserial -d /dev/ttyS1`
# If bit 8 is set, DSR is on
# (button pressed)
if [ $(( $LINESTAT & 256 )) -eq 256 ]; then
DSR=1
else
DSR=0
fi
}
# Repeat endlessly
while [ true ]
do
chkDSR
# If DSR is high, run audio script
if [ $DSR == 1 ]; then
$DIR/saytemp.sh >/dev/null 2>&1
# Loop while line remains hot
while [ $DSR -eq 1 ]
do
chkDSR
sleep 2
done
fi
The script is executed in the background every time the machine is rebooted. Standard script control signals can be used to stop and kill the script as necessary.
Next Time
This article showed how the serial communication between the computer and the temperature sensor was improved by using a dedicated script, and an analog button was added for on-demand actions.
The last article in this series will show how the data can be sent to various reporting applications so it can be charted and trended appropriately.
About the Author
Freelance 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.
