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.

A pile of Raspberry Pis on a wooden table.

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.

Using the vcgencmd to obtain the CPU temperature

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.

Using the vcgencmd with its full directory path to obtain the CPU temperature

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.

Using awk to parse the temperature value from the vcgencmd output

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.

Assigning the CPU temperature to a variable

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.

Using awk to parse the temperature value from the vcgencmd output and convert it to Fahrenheit

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.

The green “+” button in Discord that creates a new server

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.

The “Create my own” option in the “Create a server” dialog

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.

The “For me and my friends” option in the “Tell Us More About 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.)

Naming the new server in the “Customise Your Server” dialog

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.

The drop down menu arrow beside the server name in Discord

nudge the Server tweaks option.

On the controls page, nudge the Integrations option in the sidebar.

tap the Create Webhook button.

The “Server Settings” option in the drop down menu

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.

The “Create Webhook” button on the “Integrations” page

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.

The arrow head on the newly created webhook entry

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”.

The “Copy Webhook URL” button in the webhook dialog

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 “Save Changes” button

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.

Using the chmod command to make the script executable

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.

Running the script on 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.

The temperature alert message in 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.

An alerting message displayed in Slack

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.