Sending SMS Text Messages Using Python For one of my Pi projects I wanted the ability to send SMS text messages from a Python script. These messages would be sent to my mobile phone and alert
Sending SMS Text Messages Using Python
For one of my Pi projects I wanted the ability to send SMS text messages from a Python script. These messages would be sent to my mobile phone and alert me about specific events recorded by my Pi. There was no possibility of connecting the Pi to a mobile phone so I decided to send the SMS via the internet.
In previous years there were services that allowed you to send free text messages via the Internet. In general these services were either illegal or relied on loopholes in mobile phone networks that have since been fixed.
So the only reliable solution is to use an SMS Gateway.
Sending messages via a legitimate gateway costs per message but this cost is low (approximately 5p per message) and you know the service will be available and reliable.
I decided to use TxtLocal as they provided a low cost solution with the added bonus of example Python code to send messages from within a script. They provide full documentation on their SMS API Gateway page which includes code examples for PHP, ASP, C#, VB .Net, VBA, Java and Perl as well as Python. This was perfect for my Raspberry Pi project.
Step 1 – Create an Account
It was quick and easy to sign up for a free account. This gives you 10 free messages so you’ve got a chance to test your code before having to pay for more messages.
Step 2 – Get Your API Hash
You can get your unique API hash from the TextLocal Control Panel. You must enter your API hash into the example script below.
Step 3 – Example Python Code
Create a Python script on your Pi named “sendsms.py” and include the following content :
This script can be downloaded directly to your Pi from BitBucket using :
You can edit the script at anytime using :
Step 4 – Run
Assuming your Pi is connected to the Internet you can then run the script using :
If everything has worked you should see “SMS Sent!” displayed on your screen.
While you are testing you can make use of the “test_flag”. This allows the script to run but without using up your credits. Make sure you enter your own user name, hash and target mobile phone number. If everything looks good set the flag to “0” and run the script again. This will send a real message to your phone and deduct 1 credit from your TxtLocal account.
Once you’ve used up your initial 10 free messages you can buy additional credits. How expensive this facility becomes is really down to how many messages you get your Pi to send. For my purposes I only intend on sending a few hundred per year.
- You must edit the script to include YOUR own API hash and YOUR own username.
- As each message will use up your credits on TxtLocal so you should consider carefully how many messages your script sends. Any programming mistakes may result in you sending more messages than you planned.
- Make sure you enter the mobile phone number correctly so you do not send messages to the wrong person.
- The link I provide above to TxtLocal is an affiliate link. This means if you sign up using it any purchases you make via their system will earn me a small amount of commission. You don’t have to sign up via this link if you don’t want to but there is no reason not to.