How to control a Raspberry Pi using WhatsApp

How to control a Raspberry Pi using WhatsApp

Do you use the Raspberry Pi for a headless project, such as a media player, NAS server, seed box or security camera? If you do, then your Raspberry Pi is probably tucked away somewhere that's not that easily accessible.

You can always log in to it remotely but how do you monitor it in real time? How do you know whether it's overheating? Or running out of disk space? In this project, we'll play God and make your Raspberry Pi self-aware, and give it the ability to communicate.

In more earthly terms, we'll install the sendxmpp tool on the Raspberry Pi, which allows it to communicate via the popular XMPP messaging protocol. We will use this to send notifications to us via instant messages whenever a predetermined event is triggered.

First up, you'll need to get a XMPP IM account for the Raspberry Pi. If you aren't using a XMPP server already, you can register with any of the publicly listed XMPP servers. We're using the Hot Chilli service, which gets a top-notch security rating from and allows you to register an account on the website itself.

Once you've registered an account for your Raspberry Pi, make sure you add it as a friend on your regular account, on which you want to receive notifications. Now log into the Raspberry Pi, update the repos and then download the sendxmpp tool with sudo apt-get install sendxmpp.

It's a Perl script and will pull in the required Perl dependencies. When it's installed, create a file named .sendxmpprc under your home directory with the credentials of the Raspberry Pi's XMPP account, such as:

$ nano /.sendxmpprc my-secret-password

Remember to replace the username and password with the credentials for the account you registered for the Raspberry Pi. After saving the file, you can send a message with:

$ echo "Hi, this is Pi!" | sendxmpp -t

The above command sends a message from the Raspberry Pi to the XMPP ID specified with the -t option. Swap out the ID in the example above with your own XMPP ID. If you're signed into your regular IM account, you'll receive the greeting as a regular message from the Raspberry Pi's XMPP account.

You can also pass output of Bash commands, such as:

$ echo "It is" $(date) | sendxmpp -t

This command sends the output of the date command. Here's another example that's a little more useful:

$ echo $(/opt/vc/bin/vcgencmd measure_temp) | sendxmpp -t

This command queries the temperature sensors on the Raspberry Pi using the utilities installed by the raspberrypi-firmwaretools package, which we then pipe to our regular IM user.

You can use this statement to monitor the Raspberry Pi and send you an alert over IM when the temperature crosses a preset threshold. Copy the contents of file called (click the link to see the code). Then set a crontab entry by running crontab -e and entering the following line:

*/5 * * * * /

Here we are asking the Raspberry Pi to run the script every five minutes. Remember to change the location of the file to the location on your Raspberry Pi.

So what's in the script? The script stores the temperature of the Raspberry Pi in a variable named 'temp' after stripping out the verbose text and the decimal, because Bash can only handle integers.

The script then checks whether the value is greater than 40°C, and if it is, alerts us with a message. You can extend this script to keep track of the goings-on in the Raspberry Pi. eg you can ask it to send you alerts whenever it finds a particular message in a log file, or whenever the status of a daemon changes.

The sendxmpp script helps you keep track of the activities on the Raspberry Pi – however, you can't act on them without logging in to the Raspberry Pi. But what if this isn't possible?

What if you get a temperature warning from the Raspberry Pi monitoring your home while you're away at work? Wouldn't it be great if you could control the Raspberry Pi via messages as well?

Mayank Sharma

With almost two decades of writing and reporting on Linux, Mayank Sharma would like everyone to think he’s TechRadar Pro’s expert on the topic. Of course, he’s just as interested in other computing topics, particularly cybersecurity, cloud, containers, and coding.