Reliability and magnetic sensors

RunHedgie suffered an outage of about 3 days without tweeting. There were two reasons for this which I'll explain in the following lines.

Don't forget to add a reboot strategy for your embedded system

In the first days of RunHedgie's rollout I'd come back from the office, connect via SSH to the Raspberry Pi that receives the packets from the wireless node which senses the wheel counts, change a few stuff and re-run the program that manages it all.

The thing is that one day we had a power outage, the system rebooted and I never noticed that the internal server was not running, so there were no counts and even no tweeting at all.

Solving it is quite simple, just add a command in your /etc/rc.local file and that will be executed on startup.

Reed Switches are unreliable

The counting system consists of a magnet, a Reed Switch and a microcontroller that is interrupted whenever an edge ocurrs (with a fraction of a second for debouncing). The effectiveness of this depends heavily on the distance between the magnet and the switch, the angle with which the switch is placed and how centered the magnet is with respect to the switch. That leaves us with very little margin.

Edit: @rbasoalto pointed out an excelent piece on Reed Switches, which confirms my empirical observations. A very nice illustrative explanation is included in the paper, which I'll show you here:

That's an image of a reed switch along with the magnetic field generated by a permanent magnet. As you can see, the switch's state will depend on the angle and how centered it is.

Hedgehogs are a bit filthy, they 'poop' in their wheels, so they have to be cleaned up frequently. That's why I made the whole system to be easily detachable so that it can be cleaned easily. One time after cleaning it I placed the switch a bit to far away from the magnet, resulting in no counts that night.

I've considered using a Hall Efect sensor. They seem to be much more reliable, but on the downside they need to be powered continously, since I need to detect edges, not only states. And at 3 mA of current that would drain the battery pretty quickly.

Anyways, I ordered a couple of them along with an Infrared Camera so that you can watch him run all night. They'll be arriving in a couple of weeks, so stay tuned!

RunHedgie's setup

I've been working on RunHedgie for a while. It's an automated tweeting treadmill for my wife's African Pigmy Hedgehog called Antu. Every morning he'll tweet how much he ran, just like RunKeeper :).

Antu is quite shy, just as a normal hedgie. To feel that he interacts a little more I connected a wireless node with a magnetic switch so I could count how many laps he runs every night, and therefore the equivalent distance.

The physical setup

Here's an image of the treadmill:

And a closeup of the switch/magnet components:

Hardware side

I used a custom wireless node based on Arduino. It is basically an ATMega328p microcontroller with an RFM12B radio at 433 MHz and a LiPo battery.

The board and the software is design to support different sensors, but for this case it consists of a simple reed switch and a DS18B20 temperature sensor.

Here's a picture of the node:

The orange cables are the ones that go to the reed switch. I wanted to keep everything separated since the wheel needs to be cleaned regularly.

Software side

I have an identical node connected to a Raspberry Pi, who receives the packet through the radio link and sends through the serial port of the Pi. That node also has a humidity sensor and an ambient light sensor I'd like to integrate soon.

In the Pi, there's a python program that listens to new lines and whenever it receives one then it posts the payload to an internal server.

Each request is then sent to a Redis queue which is then emptied by a worker. The actual payload processing will be covered in another post.

Everything is saved on a PostgreSQL database, which is then queried to calculate the ran distance.

It might seem that this setup is an overkill, but I did it that way since I wanted to learn these technologies hands-on.

Have a look at the code if you're interested!

Future goals

I'm moving the backend to Go, but I'm struggling with pretty much everything there :)

I'd like to expose an API and give away API keys so that pattern analysis could be made with the data. Very little is known from hedgies activities, so it'd bee cool to have a look at that.

Thanks for reading!