{"id":873,"date":"2010-03-16T14:44:17","date_gmt":"2010-03-16T19:44:17","guid":{"rendered":"http:\/\/www.tigoe.net\/pcomp\/code\/?p=873"},"modified":"2011-03-05T12:46:03","modified_gmt":"2011-03-05T17:46:03","slug":"networked-bed-presence-sensor","status":"publish","type":"post","link":"https:\/\/www.tigoe.com\/pcomp\/code\/arduinowiring\/873\/","title":{"rendered":"Networked Bed Presence sensor"},"content":{"rendered":"<p>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&#8217;d be useful someone we care about: a win-win situation.<\/p>\n<p>One of the ideas she had was a bed sensor, which would let her family know whether she&#8217;s in bed or not, so they can call to either wake her up, or tell her to get some rest if she&#8217;s up too late. Inspired by Mustafa Bagdatli and Diego Rioja&#8217;s <a href=\"http:\/\/itp.nyu.edu\/shows\/winter2009\/beddata\/\">BedData<\/a> project, and assisted by them, we thought we&#8217;d see if we could put something together quickly, connecting <a href=\"http:\/\/www.imagesco.com\/sensors\/force-sensors.html\">force sensing resistors<\/a> to an <a href=\"http:\/\/www.arduino.cc\">Arduino<\/a> and using an <a href=\"http:\/\/www.adafruit.com\/index.php?main_page=product_info&amp;cPath=17_21&amp;products_id=83&amp;zenid=758bf7fff227d789ba9b8f725b58abc0\">Ethernet shield<\/a> to upload data to <a href=\"http:\/\/www.pachube.com\">Pachube<\/a>. \u00a0The project has not been tested in the field yet, but it&#8217;s working in the lab, and was simple to set up. \u00a0It may be a useful example for other sensor projects.<\/p>\n<p><!--more--><strong>The Parts<\/strong><\/p>\n<ul>\n<li>1 <a href=\"http:\/\/www.imagesco.com\/sensors\/force-sensors.html\">force sensing resistor<\/a><\/li>\n<li>1 10kilohm resistor<\/li>\n<li>1\u00a0<a href=\"http:\/\/www.arduino.cc\">Arduino<\/a> Duemilanove<\/li>\n<li>1<a href=\"http:\/\/www.adafruit.com\/index.php?main_page=product_info&amp;cPath=17_21&amp;products_id=83&amp;zenid=758bf7fff227d789ba9b8f725b58abc0\">Ethernet shield<\/a><\/li>\n<\/ul>\n<h3>The Circuit<\/h3>\n<p>The Ethernet shield connected to the Arduino,and an FSR connects to analog input 0 using a voltage divider circuit:<\/p>\n<p><a href=\"https:\/\/tigoe.com\/pcomp\/code\/wp-content\/uploads\/2010\/03\/analogIn_bb.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-882\" style=\"margin: 5px;\" title=\"analogIn_bb\" src=\"https:\/\/tigoe.com\/pcomp\/code\/wp-content\/uploads\/2010\/03\/analogIn_bb-297x300.png\" alt=\"\" width=\"297\" height=\"300\" srcset=\"https:\/\/www.tigoe.com\/pcomp\/code\/wp-content\/uploads\/2010\/03\/analogIn_bb-297x300.png 297w, https:\/\/www.tigoe.com\/pcomp\/code\/wp-content\/uploads\/2010\/03\/analogIn_bb.png 456w\" sizes=\"auto, (max-width: 297px) 100vw, 297px\" \/><\/a><a href=\"https:\/\/tigoe.com\/pcomp\/code\/wp-content\/uploads\/2010\/03\/analogIn_schem.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-883\" style=\"margin: 5px;\" title=\"analogIn_schem\" src=\"https:\/\/tigoe.com\/pcomp\/code\/wp-content\/uploads\/2010\/03\/analogIn_schem-247x300.png\" alt=\"\" width=\"247\" height=\"300\" srcset=\"https:\/\/www.tigoe.com\/pcomp\/code\/wp-content\/uploads\/2010\/03\/analogIn_schem-247x300.png 247w, https:\/\/www.tigoe.com\/pcomp\/code\/wp-content\/uploads\/2010\/03\/analogIn_schem.png 455w\" sizes=\"auto, (max-width: 247px) 100vw, 247px\" \/><\/a><\/p>\n<p>Here are some photos of the module under the test bed, thanks to Diego:<br \/>\n<a href=\"https:\/\/tigoe.com\/pcomp\/code\/wp-content\/uploads\/2010\/03\/IMG_8567.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-thumbnail wp-image-892\" style=\"margin: 5px;\" title=\"IMG_8567\" src=\"https:\/\/tigoe.com\/pcomp\/code\/wp-content\/uploads\/2010\/03\/IMG_8567-150x150.jpg\" alt=\"\" width=\"150\" height=\"150\" \/><\/a><a href=\"https:\/\/tigoe.com\/pcomp\/code\/wp-content\/uploads\/2010\/03\/IMG_8564.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-thumbnail wp-image-891\" style=\"margin: 5px;\" title=\"IMG_8564\" src=\"https:\/\/tigoe.com\/pcomp\/code\/wp-content\/uploads\/2010\/03\/IMG_8564-150x150.jpg\" alt=\"\" width=\"150\" height=\"150\" \/><\/a><\/p>\n<p><a href=\"https:\/\/tigoe.com\/pcomp\/code\/wp-content\/uploads\/2010\/03\/IMG_8564.jpg\"><\/a><a href=\"https:\/\/tigoe.com\/pcomp\/code\/wp-content\/uploads\/2010\/03\/IMG_85481.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-thumbnail wp-image-890\" style=\"margin: 5px;\" title=\"IMG_8548\" src=\"https:\/\/tigoe.com\/pcomp\/code\/wp-content\/uploads\/2010\/03\/IMG_85481-150x150.jpg\" alt=\"\" width=\"150\" height=\"150\" \/><\/a><a href=\"https:\/\/tigoe.com\/pcomp\/code\/wp-content\/uploads\/2010\/03\/IMG_8548.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-thumbnail wp-image-889\" style=\"margin: 5px;\" title=\"IMG_8548\" src=\"https:\/\/tigoe.com\/pcomp\/code\/wp-content\/uploads\/2010\/03\/IMG_8548-150x150.jpg\" alt=\"\" width=\"150\" height=\"150\" \/><\/a><\/p>\n<h3>The Data<\/h3>\n<p>Before coding, I had to learn a little about Pachube. \u00a0Their <a href=\"http:\/\/community.pachube.com\/tutorials\">tutorials<\/a> are very good, so all I needed was the <a href=\"http:\/\/community.pachube.com\/?q=node\/4\">Quickstart Guide.<\/a> I set up a feed that&#8217;s a manual feed type, meaning I upload data when I want to.  My feed has just one value, for the sensor.<\/p>\n<p>From there, I learned that if I have my feed&#8217;s address, I can to make an HTTP PUT call with the sensor value as the content (comma-separated if there&#8217;s more than one value) that looks like this:<\/p>\n<pre>PUT \/api\/76.csv HTTP\/1.1\r\nHost:\u00a0<a title=\"www.pachube.com\" href=\"http:\/\/www.pachube.com\">www.pachube.com<\/a>\r\nX-PachubeApiKey: ENTER_YOUR_PACHUBE_API_KEY_HERE\r\nContent-Length: 19\r\nConnection: close\r\n\r\n12,14,66,23.03,text<\/pre>\n<p>The content-length field is a count of every character in the content, which is the line at the very end. \u00a0Content always comes after two newlines, at the end a <a href=\"http:\/\/en.wikipedia.org\/wiki\/HyperText_Transfer_Protocol\">HTTP PUT request<\/a>, as shown above. If I was planning to send only one sensor, my PUT statement would look more like this:<\/p>\n<pre>PUT \/api\/76.csv HTTP\/1.1\r\nHost:\u00a0<a title=\"www.pachube.com\" href=\"http:\/\/www.pachube.com\">www.pachube.com<\/a>\r\nX-PachubeApiKey: ENTER_YOUR_PACHUBE_API_KEY_HERE\r\nContent-Length: 3\r\nConnection: close\r\n\r\n126<\/pre>\n<h3>The Code<\/h3>\n<p>Using the Ethernet shield, that kind of a call is trivial. It&#8217;s just a series of print statements, like so:<\/p>\n<pre class=\"brush:arduino; light:true\">\r\n[include file=\"..\/..\/code\/Arduino\/pachube_client\/pachube_client.pde\" start=\"78\" end=\"109\" clean=\"true\"]\r\n<\/pre>\n<p>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:<\/p>\n<pre class=\"brush:arduino; light:true\">[include file=\"..\/..\/code\/Arduino\/pachube_client\/pachube_client.pde\" start=\"20\" end=\"38\" clean=\"true\"]<\/pre>\n<p>The setup just initializes the Ethernet client and the serial library:<\/p>\n<pre class=\"brush:arduino; light:true\">[include file=\"..\/..\/code\/Arduino\/pachube_client\/pachube_client.pde\" start=\"40\" end=\"46\" clean=\"true\"]<\/pre>\n<p>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&#8217;t need to see the server&#8217;s response.<\/p>\n<pre class=\"brush:arduino; light:true\">[include file=\"..\/..\/code\/Arduino\/pachube_client\/pachube_client.pde\" start=\"48\" end=\"58\" clean=\"true\"]<\/pre>\n<p>Next, I check to see if the server&#8217;s dropped the connection since the last time the main loop ran.  if so, I stop the client:<\/p>\n<pre class=\"brush:arduino; light:true\">[include file=\"..\/..\/code\/Arduino\/pachube_client\/pachube_client.pde\" start=\"62\" end=\"66\" clean=\"true\"]<\/pre>\n<p>Pachube&#8217;s quickstart guide makes it clear that you can&#8217;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&#8217;s not currently connected, and if more than ten seconds have passed, like so:<\/p>\n<pre class=\"brush:arduino; light:true\">[include file=\"..\/..\/code\/Arduino\/pachube_client\/pachube_client.pde\" start=\"70\" end=\"76\" clean=\"true\"]<\/pre>\n<p>That&#8217;s it for the main loop.  The only other method I needed was a routine to calculate the content length.  The sensor&#8217;s value is the content length, and it&#8217;s sent ASCII-encoded, for example:<\/p>\n<pre>1003<\/pre>\n<p>This means that there&#8217;s one ASCII character for every digit.  So to get the content length, I just count the digits like so:<\/p>\n<pre class=\"brush:arduino; light:true\">[include file=\"..\/..\/code\/Arduino\/pachube_client\/pachube_client.pde\" start=\"117\" end=\"130\" clean=\"true\"]<\/pre>\n<p>That&#8217;s the whole program.  Here&#8217;s a link to  the <a href=\"http:\/\/tigoe.net\/code\/Arduino\/pachube_client\/pachube_client.pde\">whole thing in its entirety<\/a>.<\/p>\n<hr \/>\n<p>Here&#8217;s what the <a href=\"http:\/\/www.pachube.com\/feeds\/6293\">Pachube live feed<\/a> looks like:<\/p>\n<p><script src=\"http:\/\/www.google.com\/jsapi\" type=\"text\/javascript\"><\/script><script src=\"http:\/\/apps.pachube.com\/google_viz\/viz.js\" type=\"text\/javascript\"><\/script><script type=\"text\/javascript\">\/\/ <![CDATA[\n createViz(6293,1,600,400,\"FF0066\");\n\/\/ ]]><\/script><\/p>\n","protected":false},"excerpt":{"rendered":"<p>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&#8217;d be useful &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.tigoe.com\/pcomp\/code\/arduinowiring\/873\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Networked Bed Presence sensor&#8221;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9,24,74],"tags":[93,94,95],"class_list":["post-873","post","type-post","status-publish","format-standard","hentry","category-arduinowiring","category-circuits","category-electronics","tag-biometrics","tag-ethernet","tag-pachube"],"_links":{"self":[{"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/posts\/873","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/comments?post=873"}],"version-history":[{"count":15,"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/posts\/873\/revisions"}],"predecessor-version":[{"id":1021,"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/posts\/873\/revisions\/1021"}],"wp:attachment":[{"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/media?parent=873"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/categories?post=873"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/tags?post=873"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}