In this short tutorial I'm going to show to you how to build a temperature alarm with the Raspberry Pi.
Raspbian is a free operating system based on Debian optimized for the Raspberry Pi hardware. An operating system is the set of basic programs and utilities that make your Raspberry Pi run. However, Raspbian provides more than a pure OS: it comes with over 35,000 packages, pre-compiled software bundled in a nice format for easy installation on your Raspberry Pi.
# watch -d cat /sys/class/thermal/thermal_zone0/temp
/sys/class/leds/led0
# echo none >/sys/class/leds/led0/trigger
# echo 1 >/sys/class/leds/led0/brightness
# echo 0 >/sys/class/leds/led0/brightness
# echo mmc0 > /sys/class/leds/led0/trigger
while (1)
{
temp = currentTemperature
if temp > 40
start flashing led 0
else
stop flashing led 0
sleep 5 seconds
}
#!/bin/bash
fault=0
while [ 1 ] ; do
temp=`cat /sys/class/thermal/thermal_zone0/temp`
if [ $temp -gt 40000 ]
then
fault=1
echo "Starting flash"
else
if [ $fault -eq 1 ]
then
fault=0
echo "Stopping flash"
fi
fi
sleep 5
done
# modprobe ledtrig_heartbeat
# echo heartbeat >/sys/class/leds/led0/trigger
# echo none >/sys/class/leds/led0/trigger
#!/bin/bash
modprobe ledtrig_heartbeat
fault=0
echo none >/sys/class/leds/led0/trigger
while [ 1 ] ; do
temp=`cat /sys/class/thermal/thermal_zone0/temp`
echo $temp
if [ $temp -gt 40000 ]
then
if [ $fault -eq 0 ]
then
fault=1
echo "Starting flash"
echo heartbeat >/sys/class/leds/led0/trigger
fi
else
if [ $fault -eq 1 ]
then
fault=0
echo "Stopping flash"
echo none >/sys/class/leds/led0/trigger
fi
fi
sleep 5
done
# chmod +x my.sh
root@raspberrypi:/home/pi# ./my.sh
40084
Starting flash
39546
Stopping flash
39546
39007
39546
39007
40084
Starting flash
40084
The only thing I'd change is your line:
temp=`cat /sys/class/thermal/thermal_zone0/temp`
to this:
read temp < /sys/class/thermal_thermal_zone0/temp
Reading the temperature this way keeps another shell from starting up for each loop to evaluate the 'cat'. Just another way to do it. I've been working on some OpenWRT-based routers on a project or two that uses up most of the memory available. Since there's not swap space… well, I just have to be careful with my memory. 🙂
Nonetheless, good little article!
-Rusty-