{"id":1216,"date":"2014-06-22T22:20:09","date_gmt":"2014-06-23T03:20:09","guid":{"rendered":"http:\/\/www.tigoe.com\/pcomp\/code\/?p=1216"},"modified":"2014-10-29T13:37:08","modified_gmt":"2014-10-29T18:37:08","slug":"node-js-on-the-arduino-yun","status":"publish","type":"post","link":"https:\/\/www.tigoe.com\/pcomp\/code\/arduinowiring\/1216\/","title":{"rendered":"Node.js on the Arduino Y\u00fan"},"content":{"rendered":"<p>Recently, Federico Fissore added node.js to the package repository for the Arduino Y\u00fan. Here&#8217;s how you get node to communicate with the Arduino processor on the Y\u00fan via the Bridge library.<\/p>\n<p>To do this, you&#8217;ll need an Arduino Y\u00fan, a microSD card, a microUSB cable and a wifi connection. You should be familiar with the basics of the Arduino Y\u00fan and node.js in order to get the most out of this post.<\/p>\n<p>All of the code for this post can be found on my <a href=\"https:\/\/github.com\/tigoe\/NetworkExamples\/tree\/master\/BridgeToNode\">GitHub repository<\/a>.<\/p>\n<p><!--more--><\/p>\n<p>First you&#8217;ll need to <a href=\"http:\/\/blog.arduino.cc\/2014\/05\/06\/time-to-expand-your-yun-disk-space-and-install-node-js\/\">install node on the Y\u00fan<\/a>. Make sure you&#8217;ve <a href=\"http:\/\/blog.arduino.cc\/2014\/04\/23\/upgrading-the-openwrt-yun-image-on-the-yun\/\">upgraded to the current Y\u00fan software image<\/a> and have connected to the internet via wifi. Then ssh into your Y\u00fan, or connect to the command line interface using the the YunSerialTerminal sketch.  <\/p>\n<p>You may want to expand the disk space on your Y\u00fan first, to make enough space for node and other tools. This means you&#8217;ll be moving most of the linux filesystem over to an SD card rather than using the built-in flash memory. If you try to install node.js and the install fails, lack of disk space could be one reason it woudl happen. There&#8217;s a <a href=\"http:\/\/arduino.cc\/en\/Tutorial\/ExpandingYunDiskSpace\">tutorial on how to expand the disk space<\/a> on the Arduino site. <\/p>\n<p>To install node.js and issue the following commands:<\/p>\n<p><code><br \/>\n$ opkg update<br \/>\n$ opkg install node<br \/>\n<\/code><\/p>\n<p>That&#8217;s it. Now you have node.js onboard. You can check that it&#8217;s okay by checking the version:<\/p>\n<p><code>$ node -v<\/code><\/p>\n<p>You should get the version number in reply.<\/p>\n<p>Once you&#8217;ve got that working, you&#8217;ll undoubtedly want to communicate with the Y\u00fan&#8217;s Arduino processor from node. You can do this using the Bridge library. On a microSD drive, make a directory for your node scripts. I called mine <code>\/arduino\/node<\/code>. Then insert it into your Y\u00fan. For reference, its path from the command line is <code>\/mnt\/sda1\/arduino\/node<\/code>. <\/p>\n<p><strong>Note:<\/strong> The Y\u00fan automatically treats the microSD card&#8217;s <code>\/arduino\/www\/<\/code> directory as a public web directory. Anything you put in there will be served out as static HTML. So you may not want to put your node scripts in this directory, so they&#8217;re not visible via the browser. That&#8217;s why I created a node directory at the same level as the www directory, but outside it.<\/p>\n<h3>Running a Simple Server<\/h3>\n<p>Once you know node&#8217;s working, try running a simple server on it. Create the following file in the <code>\/arduino\/node\/<\/code> directory, called <code>server.js<\/code>:<\/p>\n<pre class=\"brush: jscript; title: ; toolbar: true; notranslate\" title=\"\">\r\n\r\n&#x5B;github_cv url='https:\/\/github.com\/tigoe\/NetworkExamples\/blob\/master\/BridgeToNode\/server.js']\r\n\r\n<\/pre>\n<p>To run this, make sure you&#8217;re in the \/arduino\/node directory and type the following on the command line:<\/p>\n<p><code>$ node server.js<\/code><\/p>\n<p>Then in the browser, go to your Y\u00fan&#8217;s IP address, port 8080. For my machine on my network, it&#8217;s 192.168.1.110:8080. You&#8217;ll get a hello web page that tells you your client address, and you&#8217;ll see the same info on the command line of the Y\u00fan. Congratulations, you&#8217;ve got your first node script working on the Y\u00fan!<\/p>\n<h3>File Transfer to the Y\u00fan<\/h3>\n<p>You may find it tedious to edit text on the Y\u00fan, and equally tedious to continually remove your SD card to save files. You can use scp to transfer files as well. To do this, change directories on your local machine to\u00a0your project directory, then to transfer the server.js file to the <code>\/arduino\/node<\/code> directory on the Y\u00fan, type the following:<\/p>\n<p><code>scp server.js root@yournode.local:\/mnt\/sda1\/arduino\/node\/.<\/code><\/p>\n<p>You&#8217;ll be prompted for your Y\u00fan&#8217;s password and the file will automatically overwrite whatever&#8217;s there.<\/p>\n<h3>Communicating from Bridge to Node<\/h3>\n<p>Starting a node script from the Arduino processor using Bridge is easy. You use the Bridge library&#8217;s Process class and call the node script using <code>Process.runShellCommandAsynchronously()<\/code>. Anything that node sends via console.log will be sent to your Arduino sketch, and you can read it using Process.read(). Here&#8217;s how you&#8217;d launch the server.js script:<\/p>\n<p><code><br \/>\nProcess nodejs;<br \/>\nnodejs.runShellCommandAsynchronously(\"node \/mnt\/sda1\/arduino\/node\/server.js\");<br \/>\n<\/code><\/p>\n<p>You can use this BridgeToNode sketch, and replace <code>echo.js<\/code> with <code>server.js<\/code><\/p>\n<pre class=\"brush: cpp; first-line: 20; title: ; toolbar: true; notranslate\" title=\"\">\r\n\r\n&#x5B;github_cv url='https:\/\/github.com\/tigoe\/NetworkExamples\/blob\/master\/BridgeToNode\/NodeToSerial\/NodeToSerial.ino']\r\n\r\n<\/pre>\n<p>When you run this, it will launch server.js once you open the Serial Monitor (the line <code>while (!Serial);<\/code> stops the sketch until you do so), and if you go to the server in a browser as you did above, you&#8217;ll see the console.log results in the Serial monitor. Node is now talking to the Arduino processor. <\/p>\n<p>The <code>Process.runShellCommandAsynchronously()<\/code> is important in the sketch above. It tells Arduino to launch the sketch but not to wait for a result. This way, node can do its business and the Arduino sketch can do its business, and they&#8217;re not waiting for each other.<\/p>\n<h3>Communicating from Node to Bridge<\/h3>\n<p>Now that you have a server running and communicating with the Arduino processor it&#8217;s time to get the Arduino processor communicating back to node. Create the following node script in the node directory on your Y\u00fan:<\/p>\n<pre class=\"brush: jscript; title: ; toolbar: true; notranslate\" title=\"\">\r\n\r\n&#x5B;github_cv url='https:\/\/github.com\/tigoe\/NetworkExamples\/blob\/master\/BridgeToNode\/echo.js']\r\n\r\n<\/pre>\n<p>This node script uses the <a href=\"http:\/\/nodejs.org\/api\/readline.html\">readline module<\/a> to exchange data with the stdin and stdout. When the Bridge library calls this script, anything you print to console.log will get sent to Bridge, and anything that you send from the Arduino via Bridge with a newline at the end (for example, if you used <code>println()<\/code>) will appear as data in the <code>linereader.on()<\/code> function. Save this sketch to your node directory on your Y\u00fan.<\/p>\n<p>Run the NodeToSerial sketch above again, this time replacing server.js with echo.js again. The Arduino will launch echo.js. This script starts by saying hello, then echoes back to you anything you send it. It doesn&#8217;t do much by itself, but it shows you how to effect two-way communication between node and the Arduino processor. <\/p>\n<h3>Conclusion<\/h3>\n<p>Being able to communicate between a node script and the Arduino processor has lots of uses. You can write scripts in node to make http calls and filter the results for you, or write node servers as an alternative to the Y\u00fan&#8217;s built in REST server. With control of the programming on both the Arduino and the Linux processors, you can decide how you want to structure the communication between them, to take best advantage of both.<\/p>\n<p>All of the code for this post can be found on my <a href=\"https:\/\/github.com\/tigoe\/NetworkExamples\/tree\/master\/BridgeToNode\">GitHub repository<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Recently, Federico Fissore added node.js to the package repository for the Arduino Y\u00fan. Here&#8217;s how you get node to communicate with the Arduino processor on the Y\u00fan via the Bridge library. To do this, you&#8217;ll need an Arduino Y\u00fan, a microSD card, a microUSB cable and a wifi connection. You should be familiar with the &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.tigoe.com\/pcomp\/code\/arduinowiring\/1216\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Node.js on the Arduino Y\u00fan&#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,100,103,104],"tags":[37,106,116,105],"class_list":["post-1216","post","type-post","status-publish","format-standard","hentry","category-arduinowiring","category-code","category-javascript","category-node-js","tag-arduino","tag-bridge","tag-node-js","tag-yun"],"_links":{"self":[{"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/posts\/1216","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=1216"}],"version-history":[{"count":10,"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/posts\/1216\/revisions"}],"predecessor-version":[{"id":1438,"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/posts\/1216\/revisions\/1438"}],"wp:attachment":[{"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/media?parent=1216"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/categories?post=1216"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/tags?post=1216"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}