-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
IoT.js-DCO-1.0-Signed-off-by: Ilyong Cho [email protected]
- Loading branch information
Showing
2 changed files
with
150 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); |