Send SMS Messages from Raspberry Pi

The back story to this article is pretty simple. I have a Raspberry Pi, what is a raspberry pi, it is one of those super small ARM computers that cost about $35 (more information on the raspberry pi can be found at raspberrypi.org). I use the Raspberry Pi to run Nagios and monitor my servers (a tutorial on that will be coming later). I wanted to be able to have my Raspberry Pi send me an SMS message should specific alert happen on my servers, but I really did not want to pay a SMS gateway for this luxury (email works find I would just prefer SMS). So I started looking for a solution. The following is how to text from computer allowing you to send a text message.

2014-02-10 09.32.19

I stumbled upon https://github.com/korylprince/pygvoicelib which allowed me to use a simple python script to send SMS messages. The following is how to get your Pi sending text messages with ease.

Step 1: Ensure your Raspberry Pi is setup with your distro of choice and has internet access.
252_raspbian_os

Step2: Sign up for a google voice account
a Gmail account will be needed to do this.
gvoice

Step3: Enable 2-step verification of your google account.
a link can be found on the following page. https://accounts.google.com/b/0/IssuedAuthSubTokens
2-step

Step4: Create an Application Specific password.
Navigate to https://accounts.google.com/b/0/IssuedAuthSubTokens scroll to the bottom and in the name field put “Raspberry Pi” (or whatever other name you want to identify the device). Click on Generate password.
app-pass

You will then be given a one-time use password.
app-pass2

Step5: Log into your Pi and open the terminal or SSH using Putty
Next cd to your home directory in my case its /home/pi

cd  /home/pi

 

Step6: Clone the git repository.
First run the following command to prevent SSL errors:

export GIT_SSL_NO_VERIFY=1

git_ssl

Next run the following command to clone the git repository:

git clone https://github.com/korylprince/pygvoicelib.git

git_clone

Step7:  Authorize your Raspberry Pi with Google.
cd to the newly created folder pygvoicelib
run the following command:

python get_auth.py

get_auth
You will be prompted for your Gmail account and your Application password you got in step 4. After entering the information you will see a number of variables show up in the terminal. Make note of the following variables you will need them in the next step:
username
apppass
auth_token
rnr_se

Step8:  Create a script to send a text message.
Run the following command (you can use nano if you wish):

vi text.py

Place the following code in the file we just created:

 

#!/usr/bin/python
import pygvoicelib
number = raw_input('number:')
txtmsg = raw_input('message:')
client = pygvoicelib.GoogleVoice(username,apppass,auth_token,rnr_se)
client.sms(number,txtmsg)

 

Replace username, apppass, auth_token, and rnr_se with the variable values that were displayed with step 7
Example:

#!/usr/bin/python
import pygvoicelib
number = raw_input('number:')
txtmsg = raw_input('message:')
client = pygvoicelib.GoogleVoice('[email protected]','asdfahrwsthjtrh','4k3EozF_Qmrg3tD2_m56nQtFHCVSaTdUxb7HvcaN6g3PV929VH0eH4GGVOVpbVK2O6EaGFzMDYA6PhPjaEHr0ZGjO1GQN3RGhQLXqePWfglbXnA2n7XpUophOk5qztQyv2fYM7eYgtVCYeO6txTqbDQAAANsAAABZ7d0GTL2pJsUauPkH4Z3cpbJFqjfLZYfhok1b11pIMDnEOypZgIcOVdPEt8jEMx7oY9hHJeJoDQZYndDJDu8uoDbDWgxl87GMy990snKWR8iy8VIB17769eVWboa3224U8DLZLUWMpP0d4hfsDK5MQ','L6tph126BjmNjDcfTZGaWYeb+sk=')
client.sms(number,txtmsg)

 

 

Step8: Send a text message.
Run the following command:

python text.py

It will prompt you for a phone number and then for the message you wish to send.

Step9: Celebrate

Open a beer and relax

13 comments on “Send SMS Messages from Raspberry Pi
  1. Hi Damien. Is this code Pi specific or should it run on Linux/OSX too? It looks like there are no Pi specifc elements to the process. That said I’m getting errors at step 7..

    • Ensure you are signed up completely for a United states google voice account. I have gotten feedback that it doesn’t work outside the US and possibly Canada. It is not Pi specific i also got it running on my CentOS server.

      Let me know what the error output is and I will try to duplicate it.

      • Hey Damien. Just retrying now and it does actually seem to work through without error! However sadly no message comes through, but thats probably as I’m in UK. Humbug 🙁

        • I have gotten feed back that it only works in the US and possibly in canada. A work around might be to setup a US Googlevoice for your cellphone and install the Googlevoice app then you would be able to get it.

          • Thanks for the tip. Its specified in the Google Voice T&Cs. I think UK might have been included with Canada by now if it weren’t that our telecomms companies rely too much on PAYG sms fees.

  2. Pingback: Citycorpse | Build your own instant messenger server Part 1

  3. Hi, nice work. I’m getting error trying to send SMS.
    Traceback (most recent call last):
    File “texter.py”, line 13, in
    client.sms(number,txtmsg)
    File “/usr/local/bin/tide/pygvoicelib/pygvoicelib.py”, line 341, in sms
    ‘_rnr_se’:self.rnr_se}, mode=’raw’)
    File “/usr/local/bin/tide/pygvoicelib/pygvoicelib.py”, line 224, in get_auth_url
    raise ServerError(err_code, resp)
    pygvoicelib.ServerError: (500, ‘\n\nInternal Server Error\n\n\nInternal Server Error\nError 500\n\n \n’)

    • Ensure you are signed up completely for a United states google voice account. I have gotten feedback that it doesn’t work outside the US and possibly Canada.

      What step are you having the error?

  4. Hi,

    I am trying to use this setup, but get a HTTP 404 error. After investigating further, i camme across Google site ,which said ClientLogin has been deprecated and that is the reason why I am getting 404 error. Did you update your code/steps to support OAuth as mentioned below:

    https://developers.google.com/identity/protocols/AuthForInstalledApps

    Important: ClientLogin has been officially deprecated since April 20, 2012 and is now no longer available. Requests to ClientLogin will fail with a HTTP 404 response. We encourage you to migrate to OAuth 2.0 as soon as possible.

  5. Hello,

    Thanks for your sharing!
    It’s well explained
    Have you a tutorial for configuring the nagios server with sms alerting for monitor service ?

    Thank you

Leave a Reply

Your email address will not be published. Required fields are marked *