IntelAcademic_IoT_Lab_04_Node_Server_app

Download Report

Transcript IntelAcademic_IoT_Lab_04_Node_Server_app

Intel Do-It-Yourself Challenge
Lab 5: Controlling Galileo from a webpage
Nicolas Vailliet
www.Intel-Software-Academic-Program.com
[email protected]
Intel Software
2014-02-01
Prerequisites and objectives
We assume that:
- You are able to download an archive, to unzipped it and run a
program under the OS you’re familiar to.
- You are able to read pieces of code and comments written in C-like
language.
Our objective are:
- Given a Galileo board and a laptop connected on the same network,
you will be able to write node.js code to control galileo from a
webpage, displayed in your laptop’s browser.
In general,
Our goal is to teach you a little bit of node.js, as it is one of the most
employed technology in IoT.
Before you start
What do you need?
Desktop OS
We assume you’re under Microsoft Windows.
Linux and Mac users should not have any problem to do the same.
Hardware
- An Intel Galileo Development Board.
- The box comes with power supply and cable.
- A Wi-Fi adaptor (N-2200 Mini-PCIe)
- A micro SD card (8GB with the full Linux image we provide)
Software
- A SSH client like Putty for Windows. Mac and Linux users only need a terminal.
http://www.chiark.greenend.org.uk/~sgtatham/putty/download.html
- Set up system date to avoid certificate expiration issue
- Install Galileo-io node on your Galileo board
(Optional: You can install node.js on your laptop to test your
program before).
Web LED controller demo
Web LED controller demo
Galileo-io module
This node.js module allows you to access GPIO without dealing with files from Linux file
system (and the mapping stuff). Functions have the same name as Arduino library ones.
Importing this module
var Galileo = require("galileo-io");
var board = new Galileo();
TurnOnLed function
function turnOnLed(){
board.digitalWrite(8, 1);
}
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);
Web LED controller demo
A respond function with a basic html page
function respond() {
res.writeHead(200, { "Content-Type": "text/html" });
res.write("<!DOCTYPE html><html><body>");
res.write("<h1>Intel Galileo 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='window.location =
\"http://192.168.2.134:1337/ledOn\"' value='Turn LED
on'/></p>");
res.write("<p><input type='button' onclick='window.location =
\"http://192.168.2.134:1337/ledOff\"' value='Turn LED
off'/></p>");
res.write("</body></html>");
res.end();
}
Web LED controller demo
Looking the server-side 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();
Web LED controller demo
Open your browser
Access the URL : http://192.168.2.1XX:1337/
Click on buttons to test your implementation!
Next steps
Controlling servo motors
USB ports on Galileo are useful.
Let’s control servo motor!
Implement your own projects
Want a remote for Galileo? Do it under Android!
Another idea? Let’s make it!
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/