Quick Links
Summary
The Raspberry Pis stripped-back design means it has no onboard cooling.
Why Your Raspberry Pi Is Getting Hot
The Raspberry Pi is a tremendous success story.
Its been used by makers and hobbyists to create a myriad of interesting and ingenious projects.
Jason Fitzpatrick / How-To Geek
Many of these projects require the Raspberry Pi to run inheadlessconfiguration.
That is, it doesnt have a monitor, keyboard, or mouse attached to it.
But you dont want to forget about them completely.
Like all computing devices, the Raspberry Pi generates heat.
But there is no onboard cooling.
If you are using a standard Raspberry Pi case, the only cooling effect is convection.
Some Raspberry Pi cases incorporate an integral fan.
Lets see how we can do this for two popular messaging platforms, Discord and Slack.
Firstly, it includes temp= and C, which is more than we want.
We want to have the temperature as a number that we can compare to a threshold.
If the temperature rises above the threshold itll trigger sending a message.
The second point is, the temperature is in degreesCelsius.
Finally, its a floating point number.
Well convert that to an integer value in our script.
To extract the temperature value on its own, we can use awk.
This isolates the numerical value, and allows us to assign it to a variable.
If youre happier working inFahrenheit, we can add some math to convert the value for us.
for set a threshold, we need to know what the operational range of the Raspberry Pi is.
I checked theRaspberry Pi datasheetfor the Raspberry Pi 4, Model B I was testing with.
That equates to a temperature range of 32-122 degrees Fahrenheit.
In Discord, go for the green + button to create a new server.
In the Create a Server dialog, click Create My Own.
In the Tell Us More About Your Server dialog, click For me and My Friends.
Provide a name for your new server in the Customise Your Server dialog.
Ours is called PiNotifications.
We also clicked on the camera icon and uploaded a warning bell icon.
(Once you’ve uploaded an icon, it obscures the camera icon.)
smack the blue Create button when youre ready to proceed.
Our new server now appears in our list of servers.
Select your new server, then poke the arrow alongside its name.
nudge the Server tweaks option.
On the controls page, nudge the Integrations option in the sidebar.
tap the Create Webhook button.
A new webhook is created and named for you.
poke the arrow > button to edit your webhook.
You’ll need easy access to the URL later.
When youre ready to proceed, nudge the green Save Changes button at the bottom of the page.
Pressing the Esc key will take you back into the normal Discord desktop view.
Creating Our Script to Send an Alert to Discord
Our script starts by obtaining the CPU temperature.
We then useawkto extract the integer element of the temperature value (the part that comesbeforethe floating point .)
because naked Bash only supports integer arithmetic.
We then obtain the hostname, and assign it to a variable called “this_pi”.
it’s crucial that you set the discord_pi_webhook variable to the URL that you copied and saved earlier.
When you paste it into your script, verify you keep the quotation marks.
We test the CPU temperature against the threshold value in an if statement.
The alert then appears in our Discord server.
So that we can test our script, weve set the threshold value to 15.
This means the if loop will definitely be executed.
In Celsius, thatd be about 44 degrees for my Raspberry Pi 4.
Well need to make our script executable.
To test our script, well call it from the command line.
A moment later, our message appears in Discord.
Our Raspberry Pi is called htg-pi-server, and its CPU temperature is 33 degrees Celsius.
The process is very similar to the steps you should probably take with Discord.
You’ll need to copy the webhook URL and a secret key.
Paste your secret key in the script where it reads “paste-your-key-here.”
Making sure there are quotation marks around the entire URL.
Weve renamed the discord_pi_webhook variable to slack_pi_webhook and changed the curl command for one that works with Slack.
Messages from your Raspberry Pi pop up in your nominated Slack channel.
Running the script every 15 minutes or so is a good start point.
you might always adjust that later if you think you need more frequent or less frequent checks.
If you dont, your Raspberry Pi will pepper you with false positives.