How I Feed My Cats with Linux
December 1st, 2004 by Chris McAvoy in
Cats love toys. Our cats, Cotton and Tulip, slowly have taken over our house with their little plastic doo-dads—ping-pong balls, furry mice, bells, springs and things to scratch. The cats are rarely bored. On the weekends, my wife and I oblige the kittens by tossing their toys around the house, flinging strings and jingling bells. We scratch their backs and feed them treats. They're both in love with these little stinky fish treats; all we need to do is shake the can, and they stop whatever they're doing and dash to the kitchen. Their English lexicon now includes their names and the words good and treats.
Monday through Friday, nine to five, however, the cats are responsible for their own entertainment. While we're away, we're sure the cats have a good time with their toys. Our rugs almost always are moved around, ping-pong balls end up in water dishes and fur covers our chairs. The only real difference between the weekday and the weekend is our presence and the lack of treats.
We have to work, but that doesn't mean our cats should have to go without stinky little fish, right? Why should our economic necessities have a negative effect on their treat times? Isn't it our responsibility to build them an Internet-enabled, Linux-based, cat-feeding device?
Where do we start? Three ingredients are key to our Linux-based Internet cat feeder: logic on the system, a way to talk to a device and a device to talk to. I chose Python for the logic piece, talking over a serial port to a microcontrolled cat feeder of my own design. Let's start at the bottom, the device, and work our way up to the top, the logic.
I first heard about the BASIC Stamp microcontroller from an article on Slashdot in which three guys were using a BASIC Stamp to control a bolt gun. They had taken some great pictures of bolts destroying fruit. Microcontrollers, I soon learned, are everywhere. They're the bits of logic in our microwaves and our remote controls. They are tiny and often difficult to use.
Parallax, Inc., specializes in making microcontrollers for non-engineers, specifically for students and hobbyists. Parallax products are well documented, easy to use and relatively inexpensive. I bought the Homework Board, the most inexpensive starter kit, from Radio Shack for around $75 US. It came with a book, a bag of electronic components for the experiments in the book and the board and chip.
The Stamp itself actually is a PIC microcontroller with some memory. Typically, you need to program microcontrollers with a low-level language, such as Assembly. What sets the BASIC Stamp apart from a typical microcontroller is the programming language you use to make it do stuff. Parallax developed a superset of BASIC, called PBASIC, that makes it easy to build expressive, useful programs quickly. In addition, the Homework Board has an integrated solderless breadboard, which makes for quick rewiring of projects.
The BASIC Stamp has 16 I/O pins. Each pin is set to high, +5V, or low, 0V, based on programs you create. Say you want to make an LED blink. You attach one end to an I/O pin and the other to a ground pin. You write a program that says, every second, turn the I/O pin to high (on), wait for a second, then turn it to low (off). Now replace the LED with a servo, and we've got the start of the cat feeder.
The I/O pins also listen for +5V or 0V. PBASIC even has a built-in function that allows an I/O pin to read serial data, the basis of which are high/low charges that make up binary words. Don't worry too much about serial connections yet; we cover them more in the next section. For now, understand that the BASIC Stamp can receive a command easily from a Linux system over a serial cable and turn on a servo that drives our cat feeder.
Parallax has done a great job of creating a fun community of hobbyists. Two mailing lists are devoted to its products, and dozens of sites have ideas for projects. Although the best integrated development environment for the BASIC Stamp is available only for Microsoft Windows, a tool called bstamp has been created, with Parallax's help, to program a BASIC Stamp with Linux. An example of tokenizing a program and running it, follows:
# bstamp_tokenize catcode.bs2 catcode.tok PBASIC Tokenizer Library version 1.16 # bstamp_run catcode.tok Model: Basic Stamp 2 Firmware version BCD = 16 Ack = 0 Ack = 0 Ack = 0 Ack = 0 Ack = 0 Ack = 0 Ack = 0 Ack = 0 Ack = 0 Ack = 0 Ack = 0 Ack = 0 DEBUG OUTPUT: (Press [Control]-[C] to complete sequence) _________________________________________________________ Waiting for Command Received Command: B Feed the kitty! Waiting for Command Received Command: B Feed the kitty! Waiting for Command __________________________________________________________ Received [Control]-[C]! Shutting down communication!
Everything I know about serial, I learned from the excellent “Serial Howto” by David S. Lawyer and Greg Hankins. It's a thick document, with a lot of interesting, low-level information about the RS-232 standard.
Although the BASIC Stamp communicates with bstamp over a serial connection, the serial port provided with the Homework Board is not a good candidate for true serial communication. Parallax wired the port in a nontraditional way. For one thing, all commands sent to the port are echoed back to the host, which makes two-way communication difficult.
The RS-232 standard dictates that the electrical signals that travel along our cable be +/- 12V. Because of this, if we hook up a serial connection directly to our Stamp I/O pins, we most likely will burn it out, as it expects +5V. The solution is to use an intermediary to lower the 12V signal coming from the PC to 5V and boost the 5V signal coming from the Stamp to 12V. Such a chip does exist, and it is called a MAX232. Luckily, you can get a MAX232-based RS-232-compliant adapter specifically built for solderless breadboards from a Texan named Al Williams. The device is called the RS-1, and a link to his Web site is included in the on-line Resources for this article.
Starting with the 2.4 kernel, Linux names serial ports as /dev/ttyS0, 1, 2, 3 and so on. These device files act like any other file. You open them, read or write to them and close them. The OS buffers reads and writes with a 16k buffer, which is more than adequate for most serial communication. This is good; you don't have to worry about losing bits simply because you weren't reading at the exact moment your device sent them across the wire. It also means you need to flush the buffers explicitly on the OS side when you're ready to send.
Because the port is treated as a file, you need to set the permissions accordingly. In my case, because I ultimately want a CGI program to drive the feeder, I made apache the owner. If you're in a secure environment, you always could chmod 777 /dev/ttyS0, but this obviously is insecure. It's best to decide up front what you want to do with your port and set the permissions in as secure a way as possible.
Because Linux treats our serial port as a file, it's easy to use Python to talk to the Stamp. Python's file object makes it simple to read and write files:
>>> f = open("/tmp/cotton.txt",'w')
>>> f.write("Cotton loves treats!")
>>> f.close()
>>> f = open("/tmp/cotton.txt",'r')
>>> f.read()
'Cotton loves treats!'
>>> f.close()
As you can see, however, although it's easy to open and close a file, doing so could get tricky if that file actually is a serial port. Fortunately for us, our Python script needs to write only a letter at a time to tell the feeder to dispense a treat. That said, I wanted to use as robust a method of communication as possible, and all this opening and closing worried me, as I see this project as something that always will be a work in progress. Maybe the cats will want to hit a button that sends us a message at work, who knows? The point is, I wanted something that was more serial-aware than a straight file handle. Luckily, someone else wanted the exact same thing. Chris Liechti has been nice enough to create PySerial for exactly this sort of situation. Here's an example of PySerial in action:
>>> import serial
>>> sp = serial.Serial(0)
>>> sp.portstr
'/dev/ttyS0'
>>> sp.write("F")
>>> sp.readline()
We don't actually open /dev/ttyS0, we open 0. PySerial is smart enough to know we mean the first serial port and opens it accordingly. This also means that PySerial is cross-platform, so you don't have to know that your port is /dev/ttyS0 on one machine and /dev/ttya on another. It's all handled by PySerial.
Now that Python is talking over the serial port, we need to get it on-line. I admit, I'm not terribly fond of Python in a CGI environment. Don't let that stop you though; there's a working group whose mission it is to see the CGI libraries improved, and several Python Web frameworks make CGI unnecessary. In addition, mod_python, in its latest release, has included Python Server Pages (PSP), a PHP-like syntax for mixing Python directly into an HTML page. In short, you have a lot of options when it comes to using Python on-line. For our purposes, however, the Python CGI library is more than enough to keep our kittens well fed.
Here's a brief CGI example for a bare-bones cat feeder:
#!/usr/bin/env python
import serial
import cgi
class Feeder:
def __init__(self):
self.port = serial.Serial(0)
def feed(self):
self.port.write("B")
print 'Content-Type: text/html'
print # Blank line marking end of HTTP headers
cgiParameters = cgi.FieldStorage()
control = Feeder()
control.feed()
print "<p>Thanks for feeding the kittens!"
First of all, I import the PySerial and CGI modules and then I declare a class feeder. The constructor for the class opens the serial port. The class has one method, feed, which sends an arbitrary character, in this case B, down the wire to the feeder. On the other end, PBASIC is listening for the character B and dispenses a treat when it sees it.
I built the cat feeder with a carousel design, where the treats would be put into cells, divided by a rotating paddle, driven by a servo. When the paddles rotate, a load of treats drops through a cutout in one of the cells to a food bowl. I used a container meant for storing frozen waffles for the carousel, with a custom cut rotating paddle and a Parallax servo to drive it all. The whole assembly, including the BASIC Stamp circuit, is housed in a plastic storage box in my home office. The box is connected to my Web server on /dev/ttyS0 for the feeder and /dev/ttyS1 for the debug port. Figure 1 is a picture of the cat feeder on my shelf. I'm using Fedora Core 1, with Apache 2.0.48.

Figure 1. Cotton Getting a Treat from the Feeder
The initial problem was how do I determine that the paddles have rotated enough to drop the treats and stop them from rotating? The easiest solution was to put a small sensor on the side of the carousel that would detect a paddle passing in front of it. I chose a sensor from Parallax used primarily to find a black line on the ground. I put a flat black piece of posterboard on the edge of each paddle and the sensor on the bottom of the carousel right after the edge of the hole. When the feeder feeds, the sensor detects when the first paddle moves past; when the second paddle passes the sensor, the servo stops.
I wired up the Stamp relatively quickly over a few days of experimenting. The attached breadboard is a great feature of the Homework Board. I was able to rewire and test circuits quickly without having to solder and desolder. Figure 2 shows the completed schematic. Figure 3 is a picture of the wired-up Homework Board.
Writing the code for the BASIC Stamp mostly was a matter of cutting and pasting code examples from the Parallax Web site. Listing 1 is the final code that I used on the Stamp. PBASIC relies heavily on GOTO statements for flow control, which took some getting used to on my part.
Listing 2 shows the complete Python code we use to drive the feeder. The CGI script uses the built-in shelf module for Python; shelf allows you to store live objects in a DBM database. In addition to shelf, I'm also using the Cheetah Template engine. The line t = Template(open('feeder.tmpl.py').read()) opens an HTML template called feeder.tmpl.py, reads the contents and uses it as the Cheetah template. The template format looks something like <p>The cats have been fed $fed times</p>. When we set the template variable, t.fed, to some number (say 5), the line then becomes <p>The cats have been fed 5 times</p>. My wife, a graphic designer by trade, whipped up some graphics for the page.
The cat feeder is open for business. Occasionally, a treat jams up the works, but 95% of the time, the cats get a stinky little fish. We already have plans for cat feeder v.2.0. We'd love to add a Webcam to see the kittens during the day, as well as move the device to a wireless laptop in the kitchen. The feeder is, as with most of our projects, a work in progress. Feel free to go to the Web site and give Cotton and Tulip a treat.
Resources for this article: /article/7741.
Subscribe now!
Breaking News
| AMD Calls Out Intel...We Think. | 1 day 1 hour ago |
| Bye-Bye TorrentSpy, So Long MPAA's Money | 1 day 3 hours ago |
| Sun Finds the Keys to Unlock MySQL | 2 days 22 hours ago |
| New Powers on the Throne – or Heads on the Block – at OLPC | 3 days 21 hours ago |
Featured Video
Linux Journal Gadget Guy, Shawn Powers, takes us through installing Ubuntu on a machine running Windows with the Wubi installer.
Live From the Field
The latest posts from the Linux Journal team.


Delicious
Digg
Reddit
Newsvine
Technorati






Another project
On September 23rd, 2007 CJB (not verified) says:
Perhaps this comment comes a few years too late, but I want to do a project that is very similar to yours. I have a problem with a fat cat eating out of another cat's food dish. I want to put a sensor on the smaller cat's collar that opens an auto feeding dish when she gets near it. (I could also try to train her to do something that triggers her dish to open). Coincidentally, I purchased the BASIC stamp controller kit you mentioned and am in the process of learning about it. I'm not a computer programmer. I'm going to have to read your report several times to really understand what you did.
Might you have some tips for me? Do you think I could purchase the motor, sensor, and other equipment I'd need to create my special food bowl from Parrallax? I operate a PC with Windows, and could hook up a computer to the bowl, or just the stamp controller. This sounds like a fun learning challenge for me, but I might be in over my head. Any advice you have would be appreciated!
Nice project!
On October 19th, 2006 Sqwishy (not verified) says:
Wow thats pretty cool. And i'm jealous of you. I'm really new to this whole microprocessor thingy so can you explain stuff to me :D
The main chipy thing is a basic stamp 2 from parallex right?
Did you write python code and somehow run it on the basic stamp chip or convert it to basic code first or what?
Is there an easy way for me to write python code and somehow send commands or something to a basic stamp 2 chip? like do i hafta get a converter prog?
and do i hafta run linux. i have a gentoo/windows dual boot at home but at school (where i need to work on the chipy stuff) all their compy's use windowz
Nice
On December 9th, 2005 Anonymous (not verified) says:
I respect you and very much wish you peace Lets talk about... I was send mail off
Flushing
On September 7th, 2006 Anonymous (not verified) says:
This is exactly the kind of thing that I have just started working on. But instead of a cat feeder, it's just a web-interface to flush a toilet. Sounds silly, I know -- and it is!
It might be a guy thing, it might be a hacker thing, but I find being able to flush a toilet remotely to be funny. Especially if someone is using it or in the shower (and you told them that they've been in there for an hour and it is now your turn).
Anyway, I just need to find a servo with enough strength to lift the lid, after that, it's all good. (I've actually already wired the toilet area up, so now all I have to do is get everything working.)
This is exactly the kind of s
On April 13th, 2005 OhMyAchingGut (not verified) says:
This is exactly the kind of stuff i live reading in LJ. I never in a million years would have thought of this...and its probably a good project to dip my feet into the world of microcontrollers.
I'm going to go put that renewal check in the mail. Keep up the good work guys :)
Great! Now Where's the WebCam?
On February 28th, 2005 Anonymous (not verified) says:
Fantastic article ... and definitely a cute web site ... but I'm a skeptic ... Where's the webcam to show the action of the cat feeder and to be able to actually SEE the kitty getting the treat?
*sweet*
On February 16th, 2005 RaH (not verified) says:
My wife loves cats, we also have two, she also has used a computer the last 10 years and is just now beginning to realize you can use it for more than just word processing. I am going to show her this article to prove that theres more than one way to feed a cat. Excellent job on the feeder. This seems like a good project to do with my daughter to introduce her to programming and electronics. Thank you for taking time to write it up and sharing it.
that's cool... you should def
On February 18th, 2005 Anonymous (not verified) says:
that's cool... you should definitely show your daughter.
I'll be starting CS grad school in a few months, but when I was a kid, the men in the family never bothered to teach any of the girls how to work with computers. And actually, my degree is in math... that article was still only barely legible to me...
Awesome
On February 16th, 2005 NSK (not verified) says:
That's an awesome article, please write more like this! Very good job.
Biometrics?
On February 15th, 2005 Lachek (not verified) says:
Very, very cool project...
Now, what I really want to see (and I'm only half serious :) is biometric support - a simple pawprint reader to dispense and ration food differently depending on which cat is requesting to be fed. It would greatly assist me in keeping my one overweight cat on a diet, while ensuring that my other cat gets the nutrition she needs...
rfid?
On February 15th, 2005 Kev (not verified) says:
if you had a passive sensor, it could be used to determine who is near the feeder. I though of this after seeing a dog door that was operated by a 'key' on the dogs collor.
Kev
Biometrics?! Or just a scale
On February 15th, 2005 Anonymous (not verified) says:
Biometrics?! Or just a scale or rf collar.
Junkfood
On February 15th, 2005 Anonymous (not verified) says:
The last thing these cats need is more treats. From the photo it obvious they're quite overweight (just like the majority of cats in the U.S.) just like the majority of people in the U.S.
Talk to your vet and he will (more democratically) tell you the same thing.
I think you mean diplomatical
On February 16th, 2005 Jason (not verified) says:
I think you mean diplomatically ...
jealous much?
On February 15th, 2005 Anonymous (not verified) says:
jealous much?
Of?
On February 15th, 2005 Anonymous (not verified) says:
Of?
Hmm
On October 17th, 2006 Dave (not verified) says:
You know technically that the USA is a 4th world country, right?
Nice Project!
On February 15th, 2005 Sascha M. (not verified) says:
Hi,
really nice project!
Would be nice to see more of such projects.
>^.^< -(miauu)
Your article is going to hit
On February 14th, 2005 Anonymous (not verified) says:
Your article is going to hit Slashdot. You may want to get rid of the "feed my cat" link....
Names
On February 3rd, 2005 Big Daddy (not verified) says:
Cotton and Tulip? Next cats you get name them something cool, like Tool and Flames.
Statistics?
On January 18th, 2005 BrownEyedGirl says:
Do you have any statistics on how often Tulip and Cotton get fed? What times of day seem to be popular? Do they sometimes get fed when you're at home? Just curious.
Added to Crazy Hacks.
On January 17th, 2005 Uwe Hermann (not verified) says:
This is a very very cool project, congratulations. I have taken the freedom to add it to my Crazy Hacks wiki, I hope you don't mind. Feel free to correct the entry, in case I have gotten anything wrong.
Uwe.
Other ways to interface computers to external mechanisms
On December 26th, 2004 Michael Shiloh (not verified) says:
Another way to interface motors and sensors to your computer is the Teleo System by MakingThings
Disclosure: I work for them
Teleo is popular in the electronic music and kinetic sculpture world, but it seems like a natural for programmers who would like to do things like your cat feeder. It's a series of modules that interface to a wide variety of sensors and actuators, and a simple C API.
I don't want this to look like an advertisement so I'll stop here, but I invite anyone interested to visit our website (www.makingthings.com) or to email me directly.
Sincerely,
Michael Shiloh
MakingThings
Ouch... the prices.
On February 15th, 2005 tuco (not verified) says:
I was interested until I saw an older model mini ITX mobo + 128MB CF + IDE CF reader selling for $859 bucks! If you purchaced those items seperately anywhere else, it would cost, what, $180. Can I assume the rest of product line is appropriately marked up as well?
Another way to feed a cat
On December 26th, 2004 Mark says:
Thanks Chris for putting the effort into writing this article. I have often wondered about extending the reach of my computer into the world I live in.
While looking around on the subject of microcontrollers I found an excellent device for direct serial to servo control.
If you are always going to drive your cat feeder from your PC this is a very simple solution.
I am going to get a board for myself to play with :)
Check out http://www.seetron.com/ssc.htm
At only $44US a pop too!! (postage is a bummer for OS residents)
regards
Mark Doukidis mdoukidis@gmail.com
Good article
On December 19th, 2004 kanzure says:
Good article, thanks. : )
Makes me want to feed my own cat for a change.
Is there any chance for more future linux toy projects of these likes?
Too much???
On February 15th, 2005 Anonymous (not verified) says:
Cotton and Tulip have been fed 8164 times. I sincerely hope the machine was taken offline, or they will end up with two dead cats smelling like smelly fish treads.
go away
On February 18th, 2005 Anonymous (not verified) says:
go away
Go away to where
On February 24th, 2005 Anonymous (not verified) says:
8-)))))
Shouldn't the author use Parallel Port instead
On February 16th, 2005 Ting (not verified) says:
Isn't an interface with some electronic via Parallel port a much cheaper solution?
http://www.dehne.nl/pp_powerSwitch/
http://parapin.sourceforge.net/
Why wast money on PLC controller? Take the shortest route!!! :-)
Why not just take an embedded
On February 17th, 2005 Ueli (not verified) says:
Why not just take an embedded webserver instead of using a whole computer only to feed some cats...
Likely because he already had
On April 13th, 2005 OhMyAchingGut (not verified) says:
Likely because he already had the PC lying there, because an old PC is cheaper than an embedded board, and can be used to do more than just this one job quite easily.
That said, an embedded board would be a sexier solution. Probably some opportunity there for someone with some initiative and $$$ to put together a slick commercial device.
peace
On December 29th, 2005 templar (not verified) says:
I is quite evident that this individual has never read the Quran nor studied its history and evolution. When people proclaim peace, peace, it is time to watch for war.
templar
What does one thing have to do with another?
On February 16th, 2006 Anonymous (not verified) says:
It's a cat feeder ... wft does that have to do with 9/11?
Someone slap that dude with a fish!
obvious really.
On October 6th, 2006 REdjohn (not verified) says:
In a "post 9-11" world there has never been more need for a cool way to remotely feed our cats using our computers, weblinks, pic controllers and human inspiration. If I cant log in and feed my cats from my cell-phone and some radioshack parts, then the terrorists have won!
Seriously though, great project. I feel the need to build a usb cat feeder. With the pic stuff, usb would give you that 5v+ instead of the serial 12v. Nevermind, must buy cat food.