My Fish Just Sent Me A Text Message

The Internet of Things makes it easy for us to monitor our homes. Today I’m taking that concept one step further—getting our homes to report back to us. 

In early March, I wrote about using Raspberry Pi to quantify my fish tank—in short, I taught the $35 single-board computer to monitor the temperature of my home aquarium no matter where I was in the apartment. Of course, the limitations of this project were clear: I could only keep tabs on the tank while on my home network. What if I want my fish to text me when it needs my assistance? 

See also: The Quantified Fish: How My Aquarium Uses Raspberry Pi

The problem, until now, was that getting the Raspberry Pi to initiate communication was hard. I experimented with a Node.js receiving application, and contemplated buying a server from which to run it (since my Bluehost server space doesn’t support a Node installation). The other way to get the Raspberry Pi to talk to me was to teach it to text my phone. There are also many ways to achieve SMS support in Python, Pi’s primary language, but they all either involve money or writing programs that are way over my head. 

However, I wouldn’t be writing this article if I didn’t eventually find a way to do it. The answer turned out to be Twilio, a developer-friendly set of tools for creating SMS, voice, and VoIP applications. Twilio charges pennies for calls and text messages to any phone, but it’s free to develop programs that text your own phone. That second part might not sound useful at first, but it’s exactly what I needed to complete my fish tank project.

I met with Matthew Makai, Twilio’s DC-based developer evangelist, and he helped me solve the problem. It only took nine lines of code. 

If you’ve already finished the first tutorial, here’s all you need to do. 

Sign Up For Twilio

Don’t worry, it’s free. Signing up for Twilio will give you a phone number to assign to the Raspberry Pi and credentials for using the Twilio API.  

Your phone number will probably begin with the area code of wherever you sign up. 

These are my credentials. You’ll need both the AccountSID and the AuthToken for this project to work, since we’re making use of the Twilio API to save ourselves a lot of coding.

Raspberry Pi, Meet Twilio

Now you have a Twilio account, but the Pi doesn’t know that. We’re going to install Twilio on the Raspberry Pi via the command line using “pip,” the Python package manager. Just like npm is the package manager for Node.js, pip helps to easily install software packages written in Python. 

First type “pip” into the command line to make sure it’s installed. It usually installs with Python 3 or higher. If your Pi returns the message, “-bash: pip: command not found,” that means pip still needs to be installed. The official pip website explains the process for OS X, Windows and Linux.

Once you confirm that pip is installed on your Raspberry Pi, you can type: 

sudo pip install twilio

Be sure to use “sudo”—I didn’t at first, and after it installed the Raspberry Pi decided I didn’t have permission to actually install programs. After this process, however, Twilio will be fully installed. 

Tweak The Python Program

Once again, we’re using the code from this Adafruit tutorial. The program already queries the waterproof temperature sensor for data, but it doesn’t text just yet. We’re going to add that functionality in fewer than 10 additional lines.

Go into the directory where your python program is saved with the “cd” command. Then write:

sudo nano

That should open what you already have it in the nano text editor. 

At the top, just below the place where we’re importing three Python libraries, we need to import Twilio’s API. Obviously, fill in your own accountSID and auth token here:

from import TwilioRestClient
client = TwilioRestClient(account='abc', token='123')

Next, we need to define a condition for when the program needs to text. For me, this was when the fish tank reaches 80 degrees Fahrenheit, which would be too hot for my fish:


At first, however, I set the maximum temperature at something ridiculously low, just to be sure it would text me and test the program no matter what it was sensing. I might recommend you do the same.

Finally, we need to add a few lines to the “While True:” section. Right now, this loop reads:

while True:

And all it does is print the temperature to your computer screen every second forever.

We’re going to add some substance to this:

while True:
    c, f = read_temp()
    if f > MAX_F_TEMP:
    		body="fish tank overheating!!")

Now the program reads the temperature, and judges whether the temperature it reads is higher than the maximum acceptable temperature. If it is, it sends a text to your number, from the number you got with your Twilio account. It continues to do this every 500 seconds (about 8 minutes) until you rectify the situation or turn off the Raspberry Pi. 

Here’s the final code saved in a GitHub gist. 

Test And Text

Save and exit your program with Command + X and wait. If all goes well and you’ve set your temperature very low, you should get a text relatively quickly. 

Ignore that first text you see above—I was messing with Twilio SMS. But the second one is literally a text from my fish tank! 

I am currently using this sensor to monitor my fish tank, but there are numerous applications for a project like this—whether you have a swimming pool, a pond, or a home brewery where the beer needs to be at a specific temperature. You can also set up a variable to keep the temperature from going below a certain degree, or one on either end of a spectrum.

Next up, I’ll be working with Makai to create a custom connected home tutorial for ReadWrite readers. Tell us what you would like to build, and we’ll show you how it’s done.

Photos and screenshots by Lauren Orsini; first screenshot via Twilio blog

Facebook Comments