Skip to content

Commit

Permalink
Added led control web server.
Browse files Browse the repository at this point in the history
IoT.js-DCO-1.0-Signed-off-by: Ilyong Cho [email protected]
  • Loading branch information
ILyoan committed Oct 2, 2015
1 parent a3961b9 commit 8c77494
Show file tree
Hide file tree
Showing 2 changed files with 150 additions and 0 deletions.
72 changes: 72 additions & 0 deletions samples/device-server/led-web-server/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<html>
<head>
<style>
.button {
background-color:#599bb3;
border-radius:8px;
display:inline-block;
cursor:pointer;
color:#ffffff;
font-family:Arial;
font-size:20px;
font-weight:bold;
padding:13px 32px;
text-decoration:none;
text-shadow:0px 1px 0px #3d768a;
}
.light {
background-color:#000000;
border-radius:20px;
display:inline-block;
cursor:pointer;
color:#ffffff;
font-family:Arial;
font-size:20px;
font-weight:bold;
padding:13px 32px;
text-decoration:none;
text-shadow:0px 1px 0px #3d768a;
}
</style>
<script language='javascript'>
function toggle() {
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
updateLight();
}
};
xmlhttp.open("PUT", "toggle", true);
xmlhttp.send();
}
function updateLight() {
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
var state = xmlhttp.responseText;
if (state == 'on') {
var light = document.getElementById('light');
light.innerText = 'on';
light.style.backgroundColor = '#EEEE00';
} else {
var light = document.getElementById('light');
light.innerText = 'off';
light.style.backgroundColor = '#000000';
}
}
};
xmlhttp.open("GET", "light", true);
xmlhttp.send();
}
</script>
</head>
<body>
<div>
<a href='javascript:toggle();' class='button'>toggle</a>
<div class='light' id='light'>off</div>
</div>
<script language='javascript'>
updateLight();
</script>
</body>
</html>
78 changes: 78 additions & 0 deletions samples/device-server/led-web-server/server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
var fs = require('fs');
var http = require('http');
var gpio = require('gpio');

var port = 8080;
var result = '';

var ledPin = 16;

var server = http.createServer(function(req, res) {
console.log('on request - url: ' + req.url);
if (req.url == '/') {
onIndex(req, res);
} else if (req.url == '/light') {
onLight(req, res);
} else if (req.url == '/toggle') {
onToggle(req, res);
}
});


function onIndex(req, res) {
fs.readFile('index.html', function(err, data) {
if (err) {
res.writeHead(500);
res.end();
} else {
res.writeHead(200);
res.end(data);
}
});
}

function onLight(req, res) {
gpio.readPin(ledPin, function(err, value) {
if (err) {
res.writeHead(500);
res.end();
} else {
res.writeHead(200);
res.end(value ? "on" : "off");
}
});
}

function onToggle(req, res) {
gpio.readPin(ledPin, function(err, value) {
if (err) {
res.writeHead(500);
res.end();
} else {
gpio.writePin(ledPin, !value, function(err) {
if (err) {
res.writeHead(500);
res.end();
} else {
res.writeHead(200);
res.end(value ? "on" : "off");
}
});
}
});
}

gpio.initialize();

gpio.on('initialize', function() {
console.log('GPIO initilized');
gpio.setPin(ledPin, "out", function() {
console.log('GPIO led ready');
server.listen(port);
});
});

gpio.on('error', function(err) {
console.log(err);
process.exit(1);
});

0 comments on commit 8c77494

Please sign in to comment.