How to Receive Discord or Slack Alerts for Raspberry Pi Overheating

How to Receive Discord or Slack Alerts for Raspberry Pi Overheating

Stay informed and take action! Get instant alerts if your Raspberry Pi overheats Don't be left in the dark when it comes to temperature monitoring

Some Noticeable Information

To obtain the temperature of your Raspberry Pi, utilize the "vcgencmd measure_temp" command. Create a webhook on either Discord or Slack, and employ a Bash script to monitor the temperature. If it exceeds a certain threshold, trigger an alert through the webhook. Schedule its execution regularly using a systemd timer.

The Raspberry Pi’s stripped-back design means it has no onboard cooling. But how cool would it be if it sent you a Discord or Slack message if it was getting too hot?

Why Your Raspberry Pi Is Getting Hot

The Raspberry Pi has achieved remarkable success. These affordable, single-board computers have found countless applications in both households and workplaces. Makers and hobbyists have utilized them to develop a wide range of captivating and resourceful projects.

Numerous projects necessitate the Raspberry Pi to operate in a headless configuration, meaning it does not require a monitor, keyboard, or mouse. These devices can be discreetly positioned in cupboards or under desks, carrying out their tasks silently and seamlessly, just like any other network appliance designed for easy deployment.

However, it is important to not disregard them entirely. Similar to other computing devices, the Raspberry Pi produces heat. Yet, it lacks built-in cooling mechanisms. Due to the compact arrangement of its components on a single board, the heat becomes concentrated in a limited space.

If you utilize a regular Raspberry Pi case, the cooling effect is primarily achieved through convection. To enhance this process, it is possible to install a heat sink or, if noise is not a concern, a fan. Alternatively, certain Raspberry Pi cases come equipped with an integrated fan.

Instead of having to regularly check on it, a headless system offers the convenience of receiving notifications when there are any developments. Since we all have smartphones, it's sensible to enable our Raspberry Pi to monitor its temperature and send an alert to our phones if it approaches a critical level. Let's explore how we can achieve this for two widely used messaging platforms, Discord and Slack.

Getting the CPU Temperature on a Raspberry Pi

To start, you need to acquire the CPU temperature of your Raspberry Pi. In fact, you can employ the messaging techniques outlined in this guide to receive notifications whenever any crucial aspect of your Raspberry Pi exceeds tolerable limits.

The command to get the CPU temperature on a Raspberry Pi is:

vcgencmd measure_temp

If

How to Receive Discord or Slack Alerts for Raspberry Pi Overheating

doesn't achieve the desired outcome, consider utilizing the complete directory path for the command. On my Raspberry Pi, which is operating on the Oct. 10, 2023 version of the Raspberry Pi OS, the command can be found in the "/usr/bin/" directory. Consequently, the expanded form of the command would be as follows:

/usr/bin/vcgencmd measure_temp

How to Receive Discord or Slack Alerts for Raspberry Pi Overheating

There are three observations to make regarding the command's output. Initial observation is that it contains "temp=" and "'C", which is more than desired. The desired outcome is to obtain the temperature as a numerical value that can be compared to a threshold. Should the temperature exceed the threshold, it will prompt the sending of a message. The second point to note is that the temperature is indicated in degrees Celsius. Lastly, it is a floating point number. In our script, we will convert it to an integer value.

The temperature value can be extracted separately using awk. We specify the field delimiters, equals signs "=", and apostrophes "'", as flags to indicate the start and end of a field. Next, the second field, which represents the CPU temperature reading in our string, is printed.

awk -F "[=']" '{print($2)}' $(vcgencmd measure_temp)

How to Receive Discord or Slack Alerts for Raspberry Pi Overheating

This isolates the numerical value, and allows us to assign it to a variable.

pi_temp=$(vcgencmd measure_temp | awk -F "[=']" '{print($2)}')echo $pi_temp

How to Receive Discord or Slack Alerts for Raspberry Pi Overheating

If you’re happier working in Fahrenheit, we can add some math to convert the value for us.

pi_temp=$(vcgencmd measure_temp | awk -F "[=']" '{print($2 * 1.8)+32}')echo $pi_temp

How to Receive Discord or Slack Alerts for Raspberry Pi Overheating

To establish a threshold, it is crucial to determine the operational range of the Raspberry Pi. To gather this information, I referred to the datasheet of the Raspberry Pi 4, Model B that I was conducting tests with.

In the section titled “Temperature Range and Thermals,” it is stated that the recommended ambient operating temperature range is 0 to 50 degrees Celsius, equivalent to 32-122 degrees Fahrenheit. This temperature range is generally suitable for the entire board, while the CPU temperature can safely exceed these limits. However, it is important to note that the value mentioned in the datasheet represents a conservative estimate, ensuring that we prioritize lower temperatures rather than risk higher ones.

Setting Up Discord to Accept Messages

To enable messaging in Discord, you need to set up what is known as a webhook, which is a simple process. Start by creating a new server in Discord by clicking on the green "+" button.

How to Receive Discord or Slack Alerts for Raspberry Pi Overheating

In the “Create a Server” dialog, click “Create My Own.”

How to Receive Discord or Slack Alerts for Raspberry Pi Overheating

In the “Tell Us More About Your Server” dialog, click “For me and My Friends.”

How to Receive Discord or Slack Alerts for Raspberry Pi Overheating

Provide a name for your new server in the “Customise Your Server” dialog. Ours is called “PiNotifications.”

How to Receive Discord or Slack Alerts for Raspberry Pi Overheating

We also clicked on the camera icon and uploaded a “warning bell” icon. (Once you've uploaded an icon, it obscures the camera icon.)

Click the blue “Create” button when you’re ready to proceed. Our new server now appears in our list of servers.

Select your new server, then click the arrow alongside its name.

How to Receive Discord or Slack Alerts for Raspberry Pi Overheating

Click the “Server Settings” option.

How to Receive Discord or Slack Alerts for Raspberry Pi Overheating

On the “Settings” page, click the “Integrations” option in the sidebar. Click the “Create Webhook” button. A new webhook is created and named for you.

How to Receive Discord or Slack Alerts for Raspberry Pi Overheating

Click the arrow “>” button to edit your webhook.

How to Receive Discord or Slack Alerts for Raspberry Pi Overheating

The webhook has been updated to "Pi Alerts" and is now set to send notifications to the "#general" channel on our server. To obtain the webhook URL, click on the "Copy Webhook URL" button, paste it into a text editor, and save the file. Make sure to keep the URL easily accessible for future use.

How to Receive Discord or Slack Alerts for Raspberry Pi Overheating

When you’re ready to proceed, click the green “Save Changes” button at the bottom of the page.

How to Receive Discord or Slack Alerts for Raspberry Pi Overheating

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. If you want the temperature in Fahrenheit, delete or comment out the Celsius line and uncomment the Fahrenheit line.

Using awk, we extract the integer element of the temperature value, which is the part before the decimal point, as naked Bash only supports integer arithmetic.

Next, the hostname is obtained and assigned to a variable named "this_pi". This hostname will be included in our alerting message to identify the Raspberry Pi that is sending the alert.

Set the "discord_pi_webhook" variable to the previously copied and saved URL. Ensure the quotation marks are preserved when pasting it into your script.

In an "if" statement, we compare the CPU temperature to the threshold value for testing. If the CPU temperature exceeds the trigger threshold, we employ curl to transmit the message to the webhook URL. Consequently, the alert becomes visible on our Discord server.

To test our script, we've initially set the threshold value to 15. This ensures that the "if" loop will always be executed. Once you're satisfied with the script's functionality, you can increase this value to a real-world temperature. For instance, on my Raspberry Pi 4, a real-world value would be approximately 44 degrees Celsius.

#!/bin/bash# Get CPU temperature in Celsiuspi_temp=$(vcgencmd measure_temp | awk -F "[=']" '{print($2)}')# For Fahrenheit temperatures, use this line instead# pi_temp=$(vcgencmd measure_temp | awk -F "[=']" '{print($2 * 1.8)+32}')# Round down to an integer valuepi_temp=$(echo $pi_temp | awk -F "[.]" '{print($1)}')# Obtain the hostname to identify the Pi from which the alert is sentthis_pi=$(hostname)discord_pi_webhook="Paste your webhook URL here"if [[ "$pi_temp" -ge 15 ]]; then  curl -H "Content-Type: application/json" -X POST -d '{"content":"'"Pi ${this_pi} CPU temperature is: ${pi_temp}"'"}' $discord_pi_webhookfi

Copy the script into an editor, paste in your webhook URL, save it as “cpu_temp.sh”, and close your editor. We’ll need to make our script executable.

chmod +x cpu_temp.sh

How to Receive Discord or Slack Alerts for Raspberry Pi Overheating

To test our script, we’ll call it from the command line.

/cpu_temp.sh

How to Receive Discord or Slack Alerts for Raspberry Pi Overheating

A moment later, our message appears in Discord.

How to Receive Discord or Slack Alerts for Raspberry Pi Overheating

Setting Up a Webhook on Slack

The Raspberry Pi we are using is named "htg-pi-server", and it currently has a CPU temperature of 33 degrees Celsius. After testing your script, make sure to modify the threshold value in the "if" statement to a real world value.

We have a step-by-step guide on how to set up a webhook on Slack. The process is similar to configuring it on Discord. You will need to copy the webhook URL and a secret key.

Here is the script to obtain the CPU temperature in Celsius:

#!/bin/bash

# get CPU temperature in Celsius

pi_temp=$(vcgencmd measure_temp | awk -F "[=']" '{print($2)}')

# for Fahrenheit temperatures, use this line instead

# pi_temp=$(vcgencmd measure_temp | awk -F "[=']" '{print($2 * 1.8)+32}')

# round down to an integer value

pi_temp=$(echo $pi_temp | awk -F "[.]" '{print($1)}')

# get the hostname, so we know which Pi is sending the alert

this_pi=$(hostname)

slack_pi_webhook="https://hooks.slack.com/services/paste-your-key-here"

if [[ "$pi_temp" -ge 15 ]]; then

curl -X POST --data-urlencode "payload={'channel': '#support', 'text': 'Pi ${this_pi} CPU temp is: ${pi_temp}'}" $slack_pi_webhook

fi

Replace "paste-your-key-here" with your secret key, ensuring that the entire URL is enclosed in quotation marks. Apart from this adjustment, minimal modifications are needed to adapt the script for Slack instead of Discord.

We have updated the variable name from "discord_pi_webhook" to "slack_pi_webhook" and modified the "curl" command to make it compatible with Slack. The command is now configured to post to a channel named "#support", but you have the flexibility to alter it based on the channel name you have on your Slack server.

How to Receive Discord or Slack Alerts for Raspberry Pi Overheating

Messages from your Raspberry Pi pop up in your nominated Slack channel.

Automating the Temperature Checks

To ensure periodic execution of the "cpu_temp.sh" script, you can convert it into a systemd timer. Starting with running the script every 15 minutes would be an appropriate initial interval, which can be modified later based on your requirement for more or less frequent checks.

Happy Monitoring

Remember to change the “if” statement trigger value to something more realistic than the test value. If you don’t, your Raspberry Pi will pepper you with false positives.

Editor's P/S

As a Raspberry Pi enthusiast, I am always looking for ways to improve the performance and longevity of my devices. Overheating is a common problem for Raspberry Pis, especially when they are used for intensive tasks or in warm environments. By following the steps in this article, I can set up Discord or Slack alerts for Raspberry Pi overheating, giving me peace of mind that I will be notified if my device is in danger.

In addition to the practical benefits of receiving these alerts, I also appreciate the fact that they allow me to stay connected to my Raspberry Pis even when I am not physically present. By setting up these alerts, I can ensure that I am always aware of the status of my devices and can take action if necessary.