Posted on April 13, 2017 at 3:30 PM
Twilio is an company that has some cool software out for sending SMS text messages from a python script. Seeing as the Pi loves python, it just makes sense to setup my Raspberry Pi to text! The ability to text will give me piece of mind during server reboots, etc. Additionally, there are a few projects in the works that will ultilize texting to send updates on things, such as motion captured by a camera (see PiCamPart1), or temperature in a room.
Twilio does allow for a trial account, however the actual costs of the service makes it a good option for makers looking to not break the bank. Head on over to Twilio.com and register an account, verify your account through your phone number, etc. Twilio will assign you a unique "account" series of letters/numbers, and a unique "token". Keep these safe, they are how you get your python programs to verify with Twilio that you have an account.
Once your Twilio account is setup and verified through Twilio's website, it's time to send your first text! Open up a terminal window on the pi, and navigate to a folder you want to save your python scripts in. For me, I have a folder called /scripts. To create this folder, you need to run the following command:
sudo mkdir /scripts
Now, navigate to the folder within ther terminal with the following command:
Once in this folder, we need to install Twilio. To do this, we are going to use python's equivelant of 'apt-get', 'pip'. Run the following command to install pip:
sudo pip install twilio
Modern Raspbian images come with pip by default, however if you get a '-bash' error, you may need to install pip yourself. Do that with the following command:
sudo apt-get install python-pip
Now that Twilio is installed, it's time to start the code. To send just a basic text with Twilio, you need 3 lines of code. So first, lets open a python script to put the code it:
sudo nano firstText.py
Now we have created a python script called 'firstText.py' and we are editing it, in a text editor called 'nano'. First thing we need to do is import Twilio. Doing this tells python that we intend on using Twilio, and gives us access to the Twilio library of software. Put the following line of code in the editor:
from twilio.rest import twilioRestClient
Next we need to authenticate with the Twilio servers. This is where the 'account' and 'token' string from earlier come in. Type the following in, putting your unique strings of characters in the correct spots:
client = twilioRestClient(account = 'account_string' , token = 'token_string')
Ok, so at this point we have imported Twilio, and authenticated with the servers. Now, to send our first text! The next line of code walks you through sending a single text, based on the number Twilio gave you as the from number, the number you are sending to, and the body. Notice: Twilio needs phone numbers with the country code in front, so if you want the use the number '2358675309', you will need to send it with a '+1' in the United States, so tell Twilio the number is '+12358675309'
client.messages.create(to = '+1number' , from_ = '+1number' , body = 'Hello World')
That's it for the code, to exit nano hit 'ctrl+x' then 'y' then 'enter'. This runs the exit command, says 'yes' to saving changes, and confirms. If everything worked correctly, type in the following command:
sudo python firstText.py
Give it a few seconds, and you should receive the text! If you dont, verify your "from" number is the same that Twilio assigned you, verify the "to" number, and check spelling. Other than that, python has decent error reporting, and hopefully you can debug. I can do some debugging with you if you need, but check sites like stackoverflow.com for help with that stuff first, as they are far better coders than I.
At this point, we have setup your Pi to send texts! I'll cover receiving and replying in a future guide, for sake of brevity.
Enter your Name and Email to receive notifications about new posts
Feel free to email me at firstname.lastname@example.org with any questions!