{"id":555,"date":"2012-09-02T13:52:29","date_gmt":"2012-09-02T17:52:29","guid":{"rendered":"http:\/\/blog.alanporter.com\/?p=555"},"modified":"2012-09-02T13:52:29","modified_gmt":"2012-09-02T17:52:29","slug":"garage","status":"publish","type":"post","link":"https:\/\/alanporter.com\/blog\/2012\/09\/02\/garage\/","title":{"rendered":"Garage Door Monitor"},"content":{"rendered":"<p>How many times have I gone outside in the morning, only to find that I had left my garage door open all night? Too many. I hate that uneasy feeling that I have just invited thieves into the garage for some easy pickin&#8217;s (truth be told, I&#8217;d be happy if they took <em>some<\/em> of that junk, but still).<\/p>\n<p>For a long time, I had thought of rigging up a light upstairs that would come on whenever the garage door was open. That way, at night, it would be pretty obvious that I had left the door open. But I let that idea sit dormant for a long time.<\/p>\n<p>Then I read an article in <a href=\"http:\/\/makezine.com\/\">Make magazine<\/a> (&#8220;<a href=\"http:\/\/makezine.com\/30\/universalremote\/\">12,000-Mile Universal Remote<\/a>&#8220;, issue 30, page 66). In it, the author built a board called an &#8220;XBee Pulse I\/O&#8221; that watched a door sensor, tapped the opener&#8217;s switch, and communicated with an XBee internet gateway. These talked to a cloud service called iDigi. This was not <strong>exactly<\/strong> what I wanted to do, but it pushed me in the direction I needed.<\/p>\n<p>I decided that I wanted to build an Arduino board that would monitor my garage door. I wanted it to communicate with my <a href=\"\/blog\/2008-12-18\/fit-pc\">home Linux server<\/a>, &#8220;Bender&#8221;, which I consider to be sort of a digital butler, keeping track of stuff around the house and reminding me of stuff that I need to know.<\/p>\n<h3>PONDERING WIRELESS SOLUTIONS<\/h3>\n<p>For the next few weeks, I pondered how I would hook the Arduino board in the garage to Bender, which lives in the bonus room over the garage. I was not too fond of the idea of running a wire, even though the distance is very short. I scoured the internet for ZigBee boards, but soon decided that these were very pricey for what they did.<\/p>\n<p>Inside the garage, I would need an &#8220;XBee&#8221; <a href=\"http:\/\/adafruit.com\/products\/128\">ZigBee radio module<\/a>, which costs about $24~$40. But I would also need a <a href=\"http:\/\/adafruit.com\/products\/126\">controller board<\/a> for another $10. This controller board acts as a front-end, providing a simple serial port to my project, and taking care of all of the ZigBee protocol stuff.<\/p>\n<p>Then, on Bender&#8217;s end, I would need a similar XBee module ($24) and a <a href=\"http:\/\/adafruit.com\/products\/247\">USB adaptor<\/a> ($20).<\/p>\n<p>That&#8217;s a total of $78~$110, just for the wireless link!<\/p>\n<p>Surely, dropping a Ben Franklin down for a home project is not such a big deal. But I was concerned that as my project grew, I might be adding many wireless links to it. And at $50 a pop, I figured it would be worthwhile to broaden my search a bit.<\/p>\n<h3>WHAT ABOUT BLUETOOTH?<\/h3>\n<p>I worked for Ericsson from 1998 until 2003, when they were first developing the Bluetooth standard. I remember reading presentations about the technology before they had come up with the Bluetooth name&#8230; internally they referred to it as &#8220;MC Link&#8221;, and occasionally, (my favorite) &#8220;wireless wire&#8221;! I knew that the goal for Bluetooth was to make these communication chips so low-power and so cheap that every device would include one&#8230; WORLD DOMINATION!<\/p>\n<p><a href=\"\/blog\/wp-content\/uploads\/2012\/09\/bt2s.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignleft size-full wp-image-575\" title=\"BT2S\" src=\"\/blog\/wp-content\/uploads\/2012\/09\/bt2s.jpg\" alt=\"\" width=\"160\" height=\"85\"><\/a><\/p>\n<p>With that in mind, I wondered whether there were simple Bluetooth serial communication boards that would work with an Arduino. I found one, called the &#8220;<a href=\"https:\/\/www.virtuabotix.com\/feed\/?p=373\">BT2S<\/a>&#8221; from <a href=\"http:\/\/virtuabotix.com\/\">Virtuabotix<\/a>. It was only $15, and it looked pretty easy to use. AND&#8230; I would only need ONE on the garage monitor side, because I could use Bender&#8217;s existing Bluetooth interface!<\/p>\n<p>I ordered two BT2S sticks from Virtuabotix. Later, I found another source for almost-identical boards. DealExtreme has the <a href=\"http:\/\/dx.com\/sku.104299\">JY-MCU Arduino Bluetooth Wireless Serial Port Module<\/a> for $8.60! (Be careful not to get the <a href=\"http:\/\/dx.com\/sku.80711\">TTL version<\/a> that looks very similar).<\/p>\n<h3>PUTTING IT TOGETHER &#8211; THE HARDWARE<\/h3>\n<p>I am not too fond of crawling up a ladder to reprogram my Arduino in my garage, so I decided to make the Arduino end of this project as simple and generic as possible.<\/p>\n<p>The BT2S module plugs in to the Arduino&#8217;s serial port, and the program on the Arduino simply listens for serial commands and responds. I decided to implement three commands: <strong>read digital<\/strong>, <strong>write digital<\/strong> and <strong>read analog<\/strong>. That way, nothing would be hard-coded into the Arduino. Instead, all of the smarts would be in the client program that runs on Bender.<\/p>\n<p><!-- http:\/\/vivin.net\/2009\/11\/05\/displaying-images-side-by-side-in-wordpress\/ --><\/p>\n<div style=\"display: inline-block; margin-right: 5px;\">\n<div style=\"width: 210px\" class=\"wp-caption alignnone\"><a href=\"\/blog\/wp-content\/uploads\/2012\/09\/schematic.png\"><img loading=\"lazy\" decoding=\"async\" title=\"schematic\" src=\"\/blog\/wp-content\/uploads\/2012\/09\/schematic.png\" alt=\"\" width=\"200\" height=\"200\"><\/a><p class=\"wp-caption-text\">Schematic<\/p><\/div>\n<\/div>\n<div style=\"display: inline-block;\">\n<div style=\"width: 276px\" class=\"wp-caption alignnone\"><a href=\"\/blog\/wp-content\/uploads\/2012\/09\/reedswitch.jpg\"><img loading=\"lazy\" decoding=\"async\" title=\"reed switch\" src=\"\/blog\/wp-content\/uploads\/2012\/09\/reedswitch.jpg\" alt=\"\" width=\"266\" height=\"200\"><\/a><p class=\"wp-caption-text\">Reed switch.<\/p><\/div>\n<\/div>\n<p>I plugged in a magnetic reed switch to one of the digital inputs, and an LED to a digital output. While I was at it, I put a light sensor on one of the analog inputs.<\/p>\n<p>Then I hung it up on the garage door frame. I stuck a very strong hard drive magnet on the door so it would be close to the reed switch whenever the door was open.<\/p>\n<h3>TALKING TO IT VIA BLUETOOTH<\/h3>\n<div id=\"attachment_581\" style=\"width: 190px\" class=\"wp-caption alignleft\"><a href=\"\/blog\/wp-content\/uploads\/2012\/09\/door-magnet-reedswitch.jpg\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-581\" class=\"size-full wp-image-581\" title=\"door, magnet, reed switch\" src=\"\/blog\/wp-content\/uploads\/2012\/09\/door-magnet-reedswitch.jpg\" alt=\"\" width=\"180\" height=\"240\"><\/a><p id=\"caption-attachment-581\" class=\"wp-caption-text\">Door, magnet, reed switch.<\/p><\/div>\n<p>Bender is a Linux machine, and so I needed a Linux-based program that would open a Bluetooth serial connection to the Arduino and then send serial commands to it. I needed a mixture of shell commands to configure the Bluetooth connection and low-level communication to the serial port. So I chose Python.<\/p>\n<p>Bluetooth is a complex protocol that supports a lot of different uses: real-time audio, serial communication, keyboard\/mouse, dial-up networking, and more. We&#8217;re using a simple serial link, which they call &#8220;rfcomm&#8221;. In Linux, we establish the serial link using the <code>rfcomm<\/code> command. It makes a Bluetooth connection to the device and creates a serial port device named <code>\/dev\/rfcomm0<\/code>.<\/p>\n<p>Once we have created a serial device, the python program simply reads and writes to that device just like any other file. I structured my program as a big loop with timers. Every so-many seconds, it sends a command to read the magnetic reed switch. It also sends commands to blink the LED, so I can tell from the garage that the python program is still running. When the reed switch response comes back, it writes the door status to a file. If I put that file in an area that is shared via my web server (<code>\/var\/www\/<\/code> or <code>$HOME\/public_html\/<\/code>), I can check on my door from any browser.<\/p>\n<p>If the door state has changed since the last time it was read, the python program sends a push notification (like a text message) to my phone. This is another idea that I stole from the same issue of Make magazine (&#8220;<a href=\"http:\/\/blog.makezine.com\/2010\/12\/29\/snail-mail-push-alerts\/\">You&#8217;ve Got (Snail) Mail<\/a>&#8220;, page 76). This uses an iPhone app called &#8220;Prowl&#8221; and a web service that goes along with it.<\/p>\n<h3>COMPLETING THE LOOP<\/h3>\n<p>Sure, it&#8217;s cool to hear my phone &#8220;bling&#8221; when someone opens the door at home. But my original motivation was to be warned when I had left the door open.<\/p>\n<p>Fortunately, that&#8217;s an easy job. I wrote a simple &#8220;cron&#8221; job that reads that status file at certain times of day (or night). If the door is open, it sends me another push notification saying &#8220;hey dummy, your garage door is still open!&#8221;.<\/p>\n<div id=\"attachment_592\" style=\"width: 410px\" class=\"wp-caption alignnone\"><a href=\"\/blog\/wp-content\/uploads\/2012\/09\/breadboard.jpg\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-592\" class=\"size-full wp-image-592\" title=\"breadboard\" src=\"\/blog\/wp-content\/uploads\/2012\/09\/breadboard.jpg\" alt=\"\" width=\"400\" height=\"178\"><\/a><p id=\"caption-attachment-592\" class=\"wp-caption-text\">Breadboard, with &#8220;Boarduino&#8221; and BT2S, sensors and LEDs<\/p><\/div>\n<h3>RESOURCES<\/h3>\n<p>Source code and other resources can be found on <a href=\"https:\/\/github.com\/sudoer\/doorman\/\">github<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>How many times have I gone outside in the morning, only to find that I had left my garage door open all night? Too many. I hate that uneasy feeling that I have just invited thieves into the garage for some easy pickin&#8217;s (truth be told, I&#8217;d be happy if they took some of that [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,4,5],"tags":[],"class_list":["post-555","post","type-post","status-publish","format-standard","hentry","category-software","category-gadgets","category-geek","count-0","even alt","author-alan","last"],"_links":{"self":[{"href":"https:\/\/alanporter.com\/blog\/wp-json\/wp\/v2\/posts\/555","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/alanporter.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/alanporter.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/alanporter.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/alanporter.com\/blog\/wp-json\/wp\/v2\/comments?post=555"}],"version-history":[{"count":0,"href":"https:\/\/alanporter.com\/blog\/wp-json\/wp\/v2\/posts\/555\/revisions"}],"wp:attachment":[{"href":"https:\/\/alanporter.com\/blog\/wp-json\/wp\/v2\/media?parent=555"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/alanporter.com\/blog\/wp-json\/wp\/v2\/categories?post=555"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/alanporter.com\/blog\/wp-json\/wp\/v2\/tags?post=555"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}