Monday, March 10, 2014

The Quantified Fish: How My Aquarium Uses Raspberry Pi

cats get a lot of love on ReadWrite. Owen uses a fitness tracker to monitor his terrier. Adriana uses the Internet of Things to keep tabs on her cats while she’s at work. 

As the owner of a betta fish, I didn’t think I’d also get a chance to join in on the fun. However, with the help of a Raspberry Pi, I found a way to make the world’s most low-maintenance pet even easier to care for than before. 
The solution? A Raspberry Pi waterproof temperature sensor. With the help of the Pi, the thermometer delivers the exact real-time temperature to any computer on the local network that queries for it. The hack can even be set up to deliver the temperature to any computer on the Internet, provided you have a server configured to receive its requests. 

This project required both hardware and software tinkering. I’ve never done any hardware hacking before, but I was able to make it work on the first try with a tutorial from Adafruit.
Here’s how I used a Raspberry Pi to quantify my fish. 

Get Ready: Assembling Hardware And Software

Clockwise from center: breadboard, temperature sensor, Pi Cobbler, power source, jumper wires, 4.7k resistors, Raspberry Pi Model B, and PiBow rainbow case. Clockwise from center: breadboard, temperature sensor, Pi Cobbler, power source, jumper wires, 4.7k resistors, Raspberry Pi Model B, and PiBow rainbow case.
I’m kind of a crazy fish person. My pet, Fintan, is the subject of much aquarium testing, a mathematically calculated diet, and plenty of photo shoots. No price is too high for making his life better.
In this case, the price was about $50, including the Raspberry Pi. Aside from the Pi, its case, and its power source, I bought: 
  • A breadboard, which is a construction base for engineering prototypes. It’s great for beginning electrical engineers since there’s no soldering required.
  • A pack of jumper wires. Mine came with the breadboard.
  • A DS18B20 waterproof digital temperature sensor. This is the one Adafruit recommends. It looks simple, but it can transmit a lot of data. 
  • A Pi Cobbler, which is Adafruit’s cute name for a 26-pin header. It has abbreviations on the sides that show you where to insert wires into the breadboard.
  • A 4.7k resistor. The temperature sensor requires a very low voltage, so I used this to reduce the amount of current that goes through the project.
  • A wireless USB adaptor (not pictured), to operate the Pi "headlessly"—without its own monitor, mouse, or keyboard—and get it operating on my local network.
After I took a good shot of the materials for my article, it was time to actually do some work. I put my Raspberry Pi in its case, slid my SD card—with its initial setup software NOOBS already installed—into the slot, and connected the Pi to my TV screen and the power outlet. 
In my experience, every time you do a new project with Raspberry Pi, you need to do a fresh install of NOOBS (New Out Of the Box Software), so in my house we keep a copy of it on an SD card. In this case, as with every other Pi project I’ve done so far, I used it to install Raspbian, a version of the Linux operating system Debian customized especially for the Pi. 
A Raspbian installation. It'll take about 20 minutes to get to 100%. A Raspbian installation. It'll take about 20 minutes to get to 100%.

Tinker Like A Mad Scientist

It takes 20 minutes for Raspbian to install, so in the meantime it was time to assemble the hardware parts—the breadboard, Pi cobbler, wires, and temperature sensor. Fortunately, if you’re using the Adafruit tutorial, all you have to do is match your wires to the picture
The labels on the sides of the Pi Cobbler show you where to put wires next to it. The labels on the sides of the Pi Cobbler show you where to put wires next to it.
This is where the Pi Cobbler really came in handy. I don’t know much about circuits, and I only have a shaky high school understanding of Ohm’s Law. But the Cobbler shows you where to put the wire for power, the wire for data, and the wire for grounding. 
The hardest part, in fact, was taping the wires inside the temperature sensor to the wires that fit inside the breadboard. Actually, the tutorial says to just stick the temperature sensor wires in the breadboard, but I just kept fraying and breaking the copper when I tried to do that. 
I made one attempt one at taping, which you can see in the image nearby. When it later didn’t work, my husband retaped it. 

The Software: Or, Why Python Is Basically English 

Used painter's tape for the first attempt; regular Scotch tape for the second. Used painter's tape for the first attempt; regular Scotch tape for the second.
When Raspbian finally installed, it was time to write a program to tell the temperature sensor how I wanted my data delivered. Of course, this non-developer just copy-pasted the code from Adafruit. 
Here’s the gist of the program: First, it sets variables for the computer so it knows where the sensor is writing its data and how to call it up. Next, it tells the computer to read that data into those variables. Finally, it plugs the temperature value, which is returned in Celsius, into a conversion formula to get Fahrenheit, and prints out both those values.
I never learned Python and I could tell that much even before I asked a Python programmer to spell it out for me. (He said I already had it right.) It’s English with a funny syntax. This is why it’s lauded as a language to teach to kids, and also why it’s been around 23 years.  
Python and Raspberry Pi also go great together. Inventor Eben Upton said it was named for Python, but he thought spelling it like an irrational number was too good to pass up. Since Raspberry Pi was designed to show kids how to code, it makes sense that Upton wanted to make sure it would run one of the easiest languages to teach to anyone.
I saved the program as Fintan.py in the Raspberry Pi’s home folder. You can name it anything as long as you remember what you named it. 

Putting It All Together: Test, Dunk, And Go

This isn’t part of Adafruit’s tutorial, but it was important to me to check the aquarium’s temperature remotely. Otherwise, why set it up?
I began by putting the wireless USB adaptor in the Raspberry Pi’s USB port in order to connect it to the Internet. The goal is to be able to query the Raspberry Pi from any computer on the network, but the computer won’t know where to look until you set a static IP address for the Pi. You do this through your home router, and it’s different depending on which brand you have. Look in your router’s documentation to see how you set static IPs.
My Raspberry Pi’s static IP is 192.168.2.16. So in order to make sure the Pi would transmit data headlessly, I queried it from my Mac’s Terminal application. (If you want to do this from a PC, you’ll need to install PuTTY before trying it out on the command line.)
In order to access the Pi remotely, I first queried it by my username and its IP address. 
ssh pi@192.168.2.16
The Pi responds by asking me for user Pi’s password:
pi@192.168.2.16's password:
The password is always “raspberry” by default.
Once logged in, it’s time to access the Python program we wrote earlier. You need to do this as a superuser, which means you type “sudo” (short for “super user do”) to tell the computer to do something that it would otherwise ignore from users with fewer permissions:
sudo python fintan.py
Immediately, I got a line of temperature readings that updated once per second. I tested it by putting my finger on it to see it warm up instantly. It was time to set up the Pi:
sudo shutdown now
Fintan surveying his new high-tech setup. Fintan surveying his new high-tech setup.
I unplugged the Pi from my TV monitor and the wall outlet, and moved it over to Fintan’s aquarium, dunking the waterproof end of the thermometer in the tank. Now, I can check the temperature from anywhere on my local wireless connection.
The long term goal is to configure a Web server to receive requests from the Pi and deliver them to a website, or better yet, an alert on my phone. I’m still researching how to best do that. 
For now, I’m satisfied just having the most high-tech aquarium on the block.

No comments:

Post a Comment