Networked Bed Presence sensor

John Schimmel and I had a meeting with a friend the other day who has chronic condition, and is looking for tools to notify her family and friends of her activity at home, so she can live more independently. A lot of good ideas came up, making for a potentially exciting project that’d be useful someone we care about: a win-win situation.

One of the ideas she had was a bed sensor, which would let her family know whether she’s in bed or not, so they can call to either wake her up, or tell her to get some rest if she’s up too late. Inspired by Mustafa Bagdatli and Diego Rioja’s BedData project, and assisted by them, we thought we’d see if we could put something together quickly, connecting force sensing resistors to an Arduino and using an Ethernet shield to upload data to Pachube.  The project has not been tested in the field yet, but it’s working in the lab, and was simple to set up.  It may be a useful example for other sensor projects.

The Parts

The Circuit

The Ethernet shield connected to the Arduino,and an FSR connects to analog input 0 using a voltage divider circuit:

Here are some photos of the module under the test bed, thanks to Diego:

The Data

Before coding, I had to learn a little about Pachube.  Their tutorials are very good, so all I needed was the Quickstart Guide. I set up a feed that’s a manual feed type, meaning I upload data when I want to. My feed has just one value, for the sensor.

From there, I learned that if I have my feed’s address, I can to make an HTTP PUT call with the sensor value as the content (comma-separated if there’s more than one value) that looks like this:

PUT /api/76.csv HTTP/1.1
Content-Length: 19
Connection: close


The content-length field is a count of every character in the content, which is the line at the very end.  Content always comes after two newlines, at the end a HTTP PUT request, as shown above. If I was planning to send only one sensor, my PUT statement would look more like this:

PUT /api/76.csv HTTP/1.1
Content-Length: 3
Connection: close


The Code

Using the Ethernet shield, that kind of a call is trivial. It’s just a series of print statements, like so:

[include file="../../code/Arduino/pachube_client/pachube_client.pde" start="78" end="109" clean="true"]

My Arduino program starts with the usual including of the libraries I need (in this case the Ethernet library) and setting global variables. I have one to hold the instance of the Ethernet library, one to keep track of the last connection time, and one to keep track of whether I was connected last time through the main loop:

[include file="../../code/Arduino/pachube_client/pachube_client.pde" start="20" end="38" clean="true"]

The setup just initializes the Ethernet client and the serial library:

[include file="../../code/Arduino/pachube_client/pachube_client.pde" start="40" end="46" clean="true"]

The main loop starts by reading the sensor. Then it checks for any incoming bytes from the server, and printing them out. I used this for debugging only. Once the code is working, you don’t need to see the server’s response.

[include file="../../code/Arduino/pachube_client/pachube_client.pde" start="48" end="58" clean="true"]

Next, I check to see if the server’s dropped the connection since the last time the main loop ran. if so, I stop the client:

[include file="../../code/Arduino/pachube_client/pachube_client.pde" start="62" end="66" clean="true"]

Pachube’s quickstart guide makes it clear that you can’t upload data more than once every five seconds, so I wrote a main loop that keeps track of when the last time it connected was, and only connects if it’s not currently connected, and if more than ten seconds have passed, like so:

[include file="../../code/Arduino/pachube_client/pachube_client.pde" start="70" end="76" clean="true"]

That’s it for the main loop. The only other method I needed was a routine to calculate the content length. The sensor’s value is the content length, and it’s sent ASCII-encoded, for example:


This means that there’s one ASCII character for every digit. So to get the content length, I just count the digits like so:

[include file="../../code/Arduino/pachube_client/pachube_client.pde" start="117" end="130" clean="true"]

That’s the whole program. Here’s a link to the whole thing in its entirety.

Here’s what the Pachube live feed looks like: