PiJuice HAT and pHAT are a great portable power solution to your Raspberry Pi projects. However, your Raspberry Pi may be in a remote location or you may find yourself not having physical access to the Raspberry Pi and its important to keep tabs on how it performing especially when you find that your primary power has gone down or when the battery is getting low. It can be critical for you to know in an instant when power events occur so you can rectify any issues and keep your project powered and running.

This is where the platform Twilio comes in. Twilio is a communications platform designed to link and engage with users from a number of different platforms such as Email, SMS, WhatsApp, Voice and many more. The platform allow users to create their own applications using a number of different programming language and provided examples.

Note: Twilio is a paid platform. Initial sign up under the free Trial will give you credit of $15.50 to send and receive messages. Thereafter you can use a pay-as-you-go service to top up your credit. Each message sent using the WhatsApp Twilio API is approximately $0.003, which very inexpensive. You will also be required to setup a number to send message from Twilio, this is roughly $1 a month to maintain.


In this guide we will show you how to install Twilio and PiJuice software on your Raspberry Pi as well as how to setup a trial account with Twilio. You will be able to configure your Raspberry Pi to send messages to your smart device via WhatsApp when certain power events occur using the PiJuice API.

What you need

You will need the following parts to complete this exercise:

Create a Twilio Account

1. Head on over to https://www.twilio.com/try-twilio to sign up for a free trial account. You will need to verify your email address and personal phone number for security measures before you can try Twilio.

2. Once you finish signing up, you should see you Console Dashboard. This is your home for finding all your Twilio credentials, checking your usage and creating a phone number.

3. You will also see in the Dashboard that there is a small pre-loaded balance to test out some of Twilio’s functionality. You will not be charged for Twilio phone numbers until you upgrade your account.

4. When you signed up for your account you verified your personal phone number, where you can see a list of varied numbers on the Verified Caller ID page. Here you may verify as many number as you like.

5. WhatsApp for Twilio us currently in Beta mode and as such there is a sandbox developers environment we can use to test and implement in our project before it gets rolled out. In order to get started you will need to send a WhatsApp message from your verified phone to +1 415 523 8886 with the message “join applied-basket”.

Install PiJuice Software

For this project we are going to use the PiJuice API which is included in the PiJuice Base package. To install this open up a Terminal window on your Raspberry Pi or Login via SSH and enter the following command:

sudo apt-get update

sudo apt-get install pijuice-base

Install Twilio

To install Twilio on the Raspberry Pi simply open up a terminal window and type the following command:

sudo pip3 install twilio

Note: You must install Python 3 Twilio library as the PiJuice API is Python 3 only.


In this guide we will give a few examples on how to combine Twilio with the PiJuice Events.

1. First you will need to find and locate some of your Twilio credentials such as your account ID and authorisation token, which are both unique to you. Open up your Twilio Dashboard and you can find your Account ID and Auth Token.

2. Open up a terminal window on your Raspberry Pi and create a new python script:

sudo nano whatsapp.py

3. Add the following scrip but replacing the credentials with yours incising your verifying phone number:

from twilio.rest import Client 



client = Client(account_sid, auth_token) 

message = client.messages.create( 
                              body='Sent from Raspberry Pi',      

4. Run the script from the command line and you should receive a WhatsApp message on your phone:

sudo python3 whatsapp.py

Example 1 – Primary Power Removed


from time import sleep

from twilio.rest import Client

from pijuice import PiJuice # Import pijuice module

account_sid = 'xxxxxxxxxxxxxxxxxxxxxxx'

auth_token = 'xxxxxxxxxxxxxxxxxxxxxxxxxx'

event = 0

client = Client(account_sid, auth_token)

pijuice = PiJuice(1, 0x14) # Instantiate PiJuice interface object

while True:

        data = (pijuice.status.GetStatus()['data']) # Read PiJuice status.

        print (data["powerInput"])


        if data["powerInput"] is 'NOT_PRESENT'and event == 0:

                message = client.messages.create(


                                                body='PiJuice Primary power removed',





                event = 1

        elif data["powerInput"] is "PRESENT" and event == 1:

                event = 0

                message = client.messages.create(


                                                body='PiJuice power restored',




First published at 11:04am on May 14, 2019
Last updated at 11:04am on May 14, 2019