Tag: Python

Sending SMS Texts from a Raspberry Pi or Ubuntu Box

[Edit] It seems that Twilio trial accounts timeout after all. They used to keep going so long as you didn’t go 30 days or more without using it. So this was a short lived success. I’ll look for alternate solutions.

I wanted a simple and free way to send texts to myself from one of my Raspberry Pi’s. The Pi is running the Jessie release of Debian. Debian, as the universe and its dog knows, is the OS that Ubuntu is built on, so these instructions apply to Ubuntu as well. I have tested on Ubuntu 15.10 and 16.04.

I got my Pi to send texts to my mobile using a free account from Twilio. Twilio is a Platform as a Service (PaaS) company based in California. Their platform and API’s allow developers to build applications that can make telephone calls, send text messages and much, much more.

Of course, for all of this goodness there is a charge. This is a paid for service. But luckily for me, you can get a free trial account. And although it is a trial it seems to run forever, unless you stop using it for a thirty day period. There are restrictions, naturally. You can only send out to one mobile phone number, for one thing. I only wanted to send to one number (my own mobile phone) so that was perfect for me.

To get your Ubuntu box or Raspberry Pi to send texts you need to register with Twilio and sign up for a free account. You will have to nominate the mobile number you’re going to text out to. Twilio verify this number by sending a code to it that you need to enter back into the website during the registration process. Once you have completed the registration you will get three things. One is a virtual phone number that the texts will be sent from. The other two are an account Id and an API authentication token. You’ll need all three of these to make the API call that will send the text.

You also need to have Pyhton, pip and Twilio installed on your system. Most systems will have Python and some will have pip (my Pi did, my Ubuntu did not). You will need to install Twilio as well. If any of these are missing from your system, you can install them with:

sudo apt-get install python
sudo apt-get install python-pip
sudo pip install twilio

The minimum Python script that you need to send a text is shown below (with my details obscured). Of course, you need to replace the account=, token= and from= and to= values with your own account ID, authentication token, virtual telephone number and the number that you want to have the text sent to. This last one must be the mobile number you verified during the Twilio registration process.

import os

from twilio.rest import TwilioRestClient

client = TwilioRestClient(account=’AC79******************************’, token=’d2b*****************************’)

client.messages.create(to=’+4478********’, from_=’+4412********’, body=”Hello, World!”)

In a worthwhile script you’d probably want to include logic in it that queried the system parameters that were of interest to you. It would then compose the content of the text body into something valuable. But even a simple script like this with a single, unchanging message could be used as a sort of system heartbeat check if it was inserted into a scheduled Cron job.

So, to recap. Register with Twilio. Get your account details. Install Twilio. Enter your details into the above script, and save it as (say) send_text.py.

invoke it as follows

python send_text.py

And you should receive a text on your mobile.

Tags : , ,