Daphne’s tweeting catflap

Daphne the Wondercat: monarch, fluffball, evil genius.

daphcrown-2_400x400Daphne often takes to social media to rant about the inadequate service provided by her staff (tech journalist Kate Bevan). This activity is cathartic, and highly recommended for any household pet. Unfortunately, Daphne’s catflap was until recently mute, and couldn’t tell the world about its thoughts and feelings.

This was a pity, because Daphne’s catflap actually has a lot to tell the world. You see, the catflap *loves* daphne. Each time daphne passes through, its universe lights up with joy. Every time Daphne’s whisker brushes against it, a tremor of excitement passes through its little plastic body.

In this project, we gave the catflap a voice.

Each time Daphne walks through, the catflap will take a photo and tweet it, along with a little paean to Daphne’s greatness:

The technology stack

OK, so the phrase “technology stack” is a bit grandiose to describe this project. Technology teetering edifice more like it:


Raspberry Pi

The Raspberry Pi is a cheap, credit card sized computer that was originally designed to be used in schools to teach computing, but seems to have been most enthusiastically adopted by middle-aged geeks nostalgic for the days when you had to build your own computers.

The RPi was the perfect base for this project as it runs linux so all my favourite languages and frameworks are available. It’s a bit of a power hog compared to, say, Arduino, but we don’t care about that because we’re running off mains.


One of the things that makes the RPi great for this kind of project is its GPIO (general purpose input output) connections – that’s the row of pins along the top of the board. These can be configured either to apply a voltage (output) or detect the voltage that is applied to them (input).

Magnetic door sensor

A £3 reed switch from Maplin provides our input. This is wired to the GPIO sensors using a couple of resistors that prevent the Pi from being damaged by drawing too much current:


All neatly soldered together, it looks like this:



The reed switch is duct-taped to the catflap so that when Daphne walks through, the voltage on a GPIO pin changes and the software can spring into action.


Getting hardware to work on Linux is all about carefully choosing your device for good Linux compatibility. Or so I’m told. We picked a camera solely on the basis of looks:


Little known fact: for every man, woman and child on this planet there are three open source linux video utilities that claim to be able to capture still images from a UVC webcam. And 98% of them don’t work on the RPi, or they only work at certain odd resolutions, or they produce blurry or dark images, or 1 in 50 images is flat green.

Fortunately, since there are so many, it’s not necessary to figure out why each one isn’t working – you just move on to the next. After trying uvccapture, mplayer, and v4l2grab, eventually fswebcam worked.

This is another real advantage of the RPi over Arduino. A massive amount of software is just a sudo apt-get install fswebcam away.

Generative grammar

A generative grammar is a way of creating sentences that meet certain rules. It works by taking a standard sentence structure and replacing nodes in the sentence with one of several options.

Here’s an example:

I’m a little {noun-phrase}, {adjective} and {adjective}.

When this grammar is expanded, each of the three {nodes} will be replaced with an appropriate sentence fragment. Of course, the above is incomplete because a computer doesn’t know what a “noun-phrase” is. So you have to tell it:

I’m a {noun-phrase}, {adjective} and {adjective}.

 {adjective} {noun-phrase}

 biscuit barrel


Hopefully the above is easy enough to follow. Each [section] contains a list of possible values for the {node} of the same name. Values can themselves contain references to other {nodes}. So expanding each node in the sentence, the above could become:

I’m a little teapot, short and stout


I’m a biscuit barrel, curiouser and curiouser

Note how the {noun-phrase} rule is recursive – one of its values includes itself. This means that the adjectives can repeat, so “fragrant musical teapot” is a valid {noun-phrase}.

The generative grammar file for the tweeting catflap is here and the python code that expands the grammar is here.

Glue, duct tape and sticking plasters


Finally, the glue that holds all the components together is Python. It’s Python that monitors the magnetic switch through the GPIO pins, triggers the webcam, expands the generative grammar, collects the results together and posts it all to twitter.

Python’s clear syntax and simple learning curve make it significantly easier to program in while tipsy – an important consideration for a weekend project.

The result

Alternate pictures show a head…

… and a big fluffy arse.

Nighttime venturing


Hey that’s not a cat! Testing before go-live.

6 thoughts on “Daphne’s tweeting catflap”

  1. I like this, i am looking to do something similar and got very excited when i saw the make of catflap. I want to capture the chip id of each of my 3 cats so i can see when they have been coming and going. So far i have not found away to interrogate the circuit on my spare flap. Alas you have taken a fifferent approach but still a good idea.

    1. Belatedly, as I’ve only just seen this @Slam, if it’s a Sureflap, speak to them; they’re very responsive. I know they’re developing some kind of IoT catflap, and it’s on my list to ask them if they’re going to provide an API. When I first started looking at how to put @Daphneflap on Twitter, I did a bit of digging around whether it could be triggered by the chip-reader on the Sureflap and had quite a long correspondence with the Raspberry Pi people about RFID. That was a dead end, and then Bernie came on board and made it all happen, but it’s definitely worth talking to Sureflap.

  2. I LOVE this. Want to make one (modified a bit!) for my greyhound (hence the required modifications!)
    I’m fully versed in coding (C and various others, but novice Python) and electronics, but a total Raspberry Pi novice. Planning to use such a project to get myself up to speed over the Christmas hols. But time is precious, so would love a few more pointers as to how to get started.
    Any essential links you could point me to? :-)

    1. Not really, whenever I needed to figure out how to do something I just hit up Google and tried lots of different things until I found something that worked. Lots of the info is in the form of technical discussions on various web forums and email group archives. Feel free to come back with specific questions if you get stuck. I’m on bernie@berniecode.com.

  3. Love this! Currently trying to implement it myself but a) my cats do not like the new catflap I brought and b) my USB webcam (a old ps3eyetoy) appears to have given up on life after years of being demoted to the loft.

    Any pointers for getting this to work with the rpi-cam which I have abundances of??

    1. If you’re using the code that I put on GitHub, then it would be easy to swap out another camera. Edit capture-image.sh and replace it with an alternative Linux shell command that captures an image from your camera and saves it to a file called capture.jpg. I assume that the rpi-cam has a suitable command line utility?

Leave a Reply

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