{"id":2370,"date":"2018-08-11T12:38:29","date_gmt":"2018-08-11T12:38:29","guid":{"rendered":"http:\/\/inspiredtoeducate.net\/inspiredtoeducate\/?p=2370"},"modified":"2018-08-11T12:50:30","modified_gmt":"2018-08-11T12:50:30","slug":"remotely-control-iot-devices-using-nodejs-firebase-and-johnny5","status":"publish","type":"post","link":"http:\/\/inspiredtoeducate.net\/inspiredtoeducate\/remotely-control-iot-devices-using-nodejs-firebase-and-johnny5\/","title":{"rendered":"Remotely Control IoT devices using NodeJs, Firebase, and Johnny5"},"content":{"rendered":"\n<!-- Facebook Like Button v1.9.6 BEGIN [http:\/\/blog.bottomlessinc.com] -->\n<iframe src=\"http:\/\/www.facebook.com\/plugins\/like.php?href=http%3A%2F%2Finspiredtoeducate.net%2Finspiredtoeducate%2Fremotely-control-iot-devices-using-nodejs-firebase-and-johnny5%2F&amp;layout=standard&amp;show_faces=false&amp;width=450&amp;action=like&amp;colorscheme=light\" scrolling=\"no\" frameborder=\"0\" allowTransparency=\"true\" style=\"border:none; overflow:hidden; width:450px; height: 30px; align: left; margin: 2px 0px 2px 0px\"><\/iframe>\n<!-- Facebook Like Button END -->\n<p>Hello, makers!\u00a0 In our blog post today, I wanted to share a simple way to remotely control IoT devices using NodeJs and Google Firebase.\u00a0 Let&#8217;s say you&#8217;re trying to remotely control a small lego crane like this.\u00a0 You&#8217;ll notice there are two servo motors connected to an Arduino.\u00a0 You can learn more about how you can build this in <a href=\"http:\/\/inspiredtoeducate.net\/inspiredtoeducate\/building-lego-crane-using-arduino\/\">our post on Arduino and Lego motor control<\/a>.\u00a0 \u00a0For the scope of this blog post, let&#8217;s say we wanted to remotely control the servo motor at the bottom from any place in the world.\u00a0 How would we do that?<\/p>\n<p><a href=\"http:\/\/inspiredtoeducate.net\/inspiredtoeducate\/wp-content\/uploads\/2015\/04\/crane1.jpg\"><img loading=\"lazy\" class=\"alignnone size-full wp-image-1780\" src=\"http:\/\/inspiredtoeducate.net\/inspiredtoeducate\/wp-content\/uploads\/2015\/04\/crane1.jpg\" alt=\"Lego Crane\" width=\"500\" height=\"281\" srcset=\"http:\/\/inspiredtoeducate.net\/inspiredtoeducate\/wp-content\/uploads\/2015\/04\/crane1.jpg 500w, http:\/\/inspiredtoeducate.net\/inspiredtoeducate\/wp-content\/uploads\/2015\/04\/crane1-300x168.jpg 300w\" sizes=\"(max-width: 500px) 100vw, 500px\" \/><\/a><\/p>\n<p>Firstly, check out <a href=\"http:\/\/johnny-five.io\/\">Johnny 5,<\/a>\u00a0 a very nice NodeJs library for controlling IoT devices like Arduino&#8217;s, Raspberry Pi, and more.\u00a0 I really appreciate the clarity of their documentation and API.\u00a0 You can do a lot with a small amount of javascript.<\/p>\n<p>I started to wonder if we could connect Johnny 5 to the real-time\u00a0database of Firebase.\u00a0 \u00a0What&#8217;s a real-time\u00a0database?\u00a0 In a traditional relational database like MySQL, you need to declare database tables and structures.\u00a0 You can make a database table to store a list of persons and their addresses (<a href=\"https:\/\/www.w3schools.com\/sql\/sql_create_table.asp\">see sample code here<\/a>).\u00a0 \u00a0After doing that, you can insert data into that table.\u00a0 In this traditional database world, you can&#8217;t listen for inserts into a database table and easily write code to reach to that event.<\/p>\n<p>The Firebase real-time database organizes information in a tree structure.\u00a0 You can store information in that tree any way that you want.\u00a0 Other users who have access to the database can listen for data changes at various locations in the tree and write code to react to that event.\u00a0 Check out the following video to learn how the Firebase Real-time\u00a0database works. Especially listen to how the value change event works.<\/p>\n<p><iframe loading=\"lazy\" src=\"https:\/\/www.youtube.com\/embed\/noB98K6A0TY\" width=\"560\" height=\"315\" frameborder=\"0\" allowfullscreen=\"allowfullscreen\"><\/iframe><\/p>\n<p>This link review the details of getting started with FireBase on the web or NodeJS: <a href=\"https:\/\/firebase.google.com\/docs\/database\/web\/start\">https:\/\/firebase.google.com\/docs\/database\/web\/start<\/a><\/p>\n<p>So, let&#8217;s explore the code for moving a single servo motor using Johnny 5 and Firebase.<\/p>\n<p><script src=\"https:\/\/gist.github.com\/michaelprosario\/ca136f90230c7cf28000f949d738f6d0.js\"><\/script><\/p>\n<p>At the top of the JavaScript file, we import johnny-five and firebase-admin. We start our firebase<br \/>\ndatabase session by calling &#8220;initializeApp.&#8221;<\/p>\n<p>On line 9, we create a &#8220;Board&#8221; object. I have my Arduino connected to my computer by a serial cable. Johnny5 handles this situation by default.<\/p>\n<p>Once the board enters &#8220;ready&#8221; state, we create an instance of a servo motor connected to pin 10.<\/p>\n<p>On line 14 and 15, we connect to a storage location called &#8216;servo_angle.&#8217; Using the servo_angle &#8220;on value&#8221; event, we listen for changes to this location and<br \/>\nset the angle of the servo. And that&#8217;s it!<\/p>\n<p>To write values into &#8220;servo_angle&#8221;, check out the following code.<br \/>\n<script src=\"https:\/\/gist.github.com\/michaelprosario\/3b0fcb2cb3d6dcfba926b548583cd27d.js\"><\/script><\/p>\n<p>In this script, we connect to the Firebase database in the same way. On line 10, we accept an angle from command line arguments. On line 16, we write that angle to &#8216;servo_angle&#8217;.<\/p>\n<p>It&#8217;s a very simple pattern for making internet connected robots or home automation.<\/p>\n<p>We love to hear from our readers. Leave a comment below if you get other ideas for internet connected robots, toys or devices.<\/p>\n<p><b>Related Blog Posts<\/b><\/p>\n<ul>\n<li><a href=\"http:\/\/inspiredtoeducate.net\/inspiredtoeducate\/how-to-create-collaborative-digital-art-using-javascript\/\">How To Create Collaborative Digital Art using #JavaScript<\/a><\/li>\n<li><a href=\"http:\/\/inspiredtoeducate.net\/inspiredtoeducate\/5-powerful-javascript-frameworks-for-2d-and-3d-graphics-javascript\/\">5 Powerful JavaScript Frameworks for 2D and 3D Graphics #javascript<\/a><\/li>\n<li><a href=\"http:\/\/inspiredtoeducate.net\/inspiredtoeducate\/5-tools-for-robot-building-android-and-javascript\/\">5 Tools for Robot building, Android and JavaScript<\/a><\/li>\n<li><a href=\"http:\/\/inspiredtoeducate.net\/inspiredtoeducate\/sumobot-jr-fun-open-source-robot-kit-using-arduino-and-javascript\/\">Sumobot Jr: Fun Open Source Robot Kit using Arduino and JavaScript<\/a><\/li>\n<\/ul>\n\n<!-- Facebook Like Button v1.9.6 BEGIN [http:\/\/blog.bottomlessinc.com] -->\n<iframe src=\"http:\/\/www.facebook.com\/plugins\/like.php?href=http%3A%2F%2Finspiredtoeducate.net%2Finspiredtoeducate%2Fremotely-control-iot-devices-using-nodejs-firebase-and-johnny5%2F&amp;layout=standard&amp;show_faces=false&amp;width=450&amp;action=like&amp;colorscheme=light\" scrolling=\"no\" frameborder=\"0\" allowTransparency=\"true\" style=\"border:none; overflow:hidden; width:450px; height: 30px; align: left; margin: 2px 0px 2px 0px\"><\/iframe>\n<!-- Facebook Like Button END -->\n","protected":false},"excerpt":{"rendered":"<p>Hello, makers!\u00a0 In our blog post today, I wanted to share a simple way to remotely control IoT devices using NodeJs and Google Firebase.\u00a0 Let&#8217;s say you&#8217;re trying to remotely control a small lego crane like this.\u00a0 You&#8217;ll notice there are two servo motors connected to an Arduino.\u00a0 You can [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[24,23,8],"tags":[],"_links":{"self":[{"href":"http:\/\/inspiredtoeducate.net\/inspiredtoeducate\/wp-json\/wp\/v2\/posts\/2370"}],"collection":[{"href":"http:\/\/inspiredtoeducate.net\/inspiredtoeducate\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/inspiredtoeducate.net\/inspiredtoeducate\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/inspiredtoeducate.net\/inspiredtoeducate\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/inspiredtoeducate.net\/inspiredtoeducate\/wp-json\/wp\/v2\/comments?post=2370"}],"version-history":[{"count":4,"href":"http:\/\/inspiredtoeducate.net\/inspiredtoeducate\/wp-json\/wp\/v2\/posts\/2370\/revisions"}],"predecessor-version":[{"id":2374,"href":"http:\/\/inspiredtoeducate.net\/inspiredtoeducate\/wp-json\/wp\/v2\/posts\/2370\/revisions\/2374"}],"wp:attachment":[{"href":"http:\/\/inspiredtoeducate.net\/inspiredtoeducate\/wp-json\/wp\/v2\/media?parent=2370"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/inspiredtoeducate.net\/inspiredtoeducate\/wp-json\/wp\/v2\/categories?post=2370"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/inspiredtoeducate.net\/inspiredtoeducate\/wp-json\/wp\/v2\/tags?post=2370"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}