Internet of Things with Intel Edison Web controller Pierre Collet www.Intel-Software-Academic-Program.com paul.guermonprez@intel.com Intel Software 2014-10-14 Objectives Objective Edison is highly connected, so let’s use the network : Edison as a web server, your laptop as a network client. We’ll interact with a LED, because that’s a thing you could not do on a regular PC. NodeJS We’ll use NodeJS : it’s a JavaScript based framework, but commonly used from a command line interpreter, not in a browser window. NodeJS is also asynchronous / functional programming, making it very easy to develop network interactions. It’s very popular in the IoT field, and there’s tons of libraries in NodeJS. What do you need? Software On Windows, we use Putty as ssh client. http://www.chiark.greenend.org.uk/~sgtatham/putty/download.html On mac or linux, ssh is part of the regular shell. We’ll run NodeJS framework on the Edison board itself, as a server language. But you can also install NodeJS on your laptop to run some scripts locally. Run script The code discussed today is available from the folder : labs/nodeserver. To execute the script, run : node ./webcontroller.js Web LED controller demo Architecture Architecture – Network side NodeJS will create html and send it over the network. Your browser will display the result. Architecture – LED side NodeJS will launch 2 scripts to control the LED : onLed.sh and offLed.sh They are controlling the pin 7. Architecture – NodeJS library or System call ? In the future, you may find a NodeJS modules to interface sensors on Edison and you won’t need to call shell scripts. We already have galileo-io for Intel Galileo. But it’s good to know you don’t need a native NodeJS module for simple things. It’s important for other scripting languages where no Edison module will come : remember you can call libmraa binaries or shell scripts. The Code # Import child_process function var system = require(‘child_process’).exec; # TurnOnLed function function turnOnLed(){ system(‘sh ./onLed.sh’); /* execute the shell script */ ledstatus = “on”; } # A simple HTTP server var http = require('http'); http.createServer(function (req, res) { function respond() { res.writeHead(200, { "Content-Type": "text/html" }); res.end(“Hello World"); }).listen(1337); The Code # A function to send the default home page function respond() { res.writeHead(200, { "Content-Type": "text/html" }); res.write("<!DOCTYPE html><html><body>"); res.write("<h1>Intel Edison web controller</h1>"); res.write("<p>Sensor value : "); res.write(sensorvalue); res.write("</p><p>LED Status : "); res.write(ledstatus); res.write("</p><p>Action</p>"); res.write("<p><input type='button' onclick='location.reload();' value='Refresh'/></p>"); res.write("<p><input type='button' onclick=‘location.pathname = \"/ledOn\"' value='Turn LED on'/></p>"); res.write("<p><input type='button' onclick=‘location.pathname = \"/ledOff\"' value='Turn LED off'/></p>"); res.write("</body></html>"); res.end(); } The Code Analyzing URL to determine the command console.log("Request: " + req.url); if(req.url === "/ledOn") turnOnLed(); else if(req.url === "/ledOff") turnOffLed(); respond(); }).listen(1337); console.log('Server running at http://localhost:1337/'); } Running the server startServer(); Demo Open your browser Access the URL : http://192.168.XXX.XXX:1337/ Click on buttons to test your implementation! Conclusion Network – Object With this demo, we’ve seen we can interact with an IoT object (LED, sensor) on one side and the network on the other side. That’s the definition of an IoT. Full Server Edison is not only sending data over the network. It can act as a server. We used NodeJS, but larger stacks like Apache or JBoss run very well. NodeJS NodeJS is a great framework for networked IoT. Even if there’s no module to do what you need, just call a script or a libmraa binary ! License Creative Commons – By 3.0 You are free: • to Share — to copy, distribute and transmit the work • to Remix — to adapt the work • to make commercial use of the work Under the following conditions: • Attribution — You must attribute the work in the manner specified by the author or licensor (but not in any way that suggests that they endorse you or your use of the work). With the understanding that: • Waiver — Any of the above conditions can be waived if you get permission from the copyright holder. • Public Domain — Where the work or any of its elements is in the public domain under applicable law, that status is in no way affected by the license. • Other Rights — In no way are any of the following rights affected by the license: – – – • Your fair dealing or fair use rights, or other applicable copyright exceptions and limitations; The author's moral rights; Rights other persons may have either in the work itself or in how the work is used, such as publicity or privacy rights. Notice — For any reuse or distribution, you must make clear to others the license terms of this work. The best way to do this is with a link to this web page. http://creativecommons.org/licenses/by/3.0/