Whoever uses a Raspberry Pi 3 must had notice it really gets hot “<irony>just a little</irony>” when the cpu usage reaches or gets close to 100%.
The point is, for who has a Pi 3 is knowing if depending how it is used, it will be the case the Pi 3 reaches 100% of cpu use. Before hand I can tell if the use is for a console emulation or for some how a video convertion (software video decoding fits that too) is about to “fry” the Pi 3.
The following script reads the Raspberry Pi 3 (I believe it works on other Raspberrys too) temperature in Celsius degrees.
pi@retropie:~/sbin $ cat show_temp.sh #!/bin/bash # This script shows the CPU temperature in Celsius. # To show in Fahrenheit append the following line bellow: # CPU_TEMP=$CPU_TEMP*9/5+32 # # Author: Marcelo Borges Ribeiro # CPU_TEMP=$(($(cat /sys/class/thermal/thermal_zone0/temp)/1000)) echo $CPU_TEMP
Running this script, in a low processing environment the temperature should be around 40 C (104 F).
To prove this is really serious, just run an emulator, for example a Play Station 1, and keep monitoring the temperature through this script. In less than 10 minutes the temperature gets over than 80 C (176 F)!
Wouldn’t be interesting if somehow the user could be warned when the temperature cross some limit ?
In order to achieve that let’s modify the script thar verifies the temperature into a daemon (service) that checks every “x” seconds the temperature and, in case it rises above some critical value, a message will be sent to our cell phone (this requires an internet connection for both, the cell phone and the Raspberry Pi).
Let’s reuse a script from that previous article, that sends any message to a certain cell phone.
Here is the script (let’s save all scripts in /home/pi/sbin). Jus replace the content of “API” by a hash of a cell phone that is going to receive the notifications and save it as push_to_phone.sh
#This script receives a message as a parameter and sends to a push bullet enabled device. #!/bin/bash # Author: Marcelo Borges Ribeiro # #Device unique id, available on your pushbullet account. API="bxrj46KOnhAmgen2ZMG2jnr47onwPUbH" MSG="$1" curl -u $API: https://api.pushbullet.com/v2/pushes -d type=note -d title="Alert" -d body="$MSG"
Let’s save also the first script, the one that shows the temperature, also in “sbin” named “show_temp.sh”.
Now, we just have to code a script that runs “show_temp”, compare its output with any temperature we want and run “push_to_phone.sh ” if necessary.
#!/bin/bash # #Maximum temperature in celsius (change to Fahrenheit if you have to). Beyond this, sends a message. #Author: Marcelo Borges Ribeiro # THRESHOLD=69 SHOW_TEMP="/home/pi/sbin/show_temp.sh" PUSH_TO_PHONE="/home/pi/sbin/push_to_phone.sh" TEMPERATURE=$($SHOW_TEMP) if [ $TEMPERATURE -ge $THRESHOLD ]; then MESSAGE="Processor is overheating! Temperatrure now is $TEMPERATURE celsius" $PUSH_TO_PHONE "$MESSAGE" fi
In this script you just have to edit a variable called “THRESHOLD” and put there the temperature desired to be the maximum (in this case is 69). Let’s name it “monitor_temperature.sh” and put this inside “sbin” directory.
With all the scripts inside the “sbin” directory you just have to enable the execution bit with the following:
chmod a+x /home/pi/sbin/*.sh
It is important to kow that until now, it was not necessary to do anything with root priviledge. Unfortunally, in order to schedule in the system this command to run from time to time, we will do this as an administrator. You just have to, as root, edit the file “/etc/crontab” and add the following:
* * * * * /home/pi/sbin/monitor_temperature.sh >/dev/null 2>&1 In this example, the monitoring will run once per minute.
In order to test this, just edit “monitor_temperature.sh” and replace the limiar of 69 degrees for some temperature the for sure will trigger the alarm, for example 29 degress.
In case everything works fine, the following message should apears in the cell phone.
In case everything works fine, just replace back “threshold” to 69 (or whatever you want) and save it.