Skip to content

Instantly share code, notes, and snippets.

@surinoel
Last active August 7, 2019 15:12
Show Gist options
  • Save surinoel/e018c88056d0e7f9d18a07288e9fc523 to your computer and use it in GitHub Desktop.
Save surinoel/e018c88056d0e7f9d18a07288e9fc523 to your computer and use it in GitHub Desktop.
const express = require('express');
const app = express();
const port = 8000;
const Gpio = require('onoff').Gpio;
const red = new Gpio(23, 'out');
const grn = new Gpio(24, 'out');
const button = new Gpio(18, 'in', 'falling', {debounceTimeout : 20});
button.watch((err, value) => {
if(err) {
throw err;
}
red.writeSync(red.readSync() ^ 1)
});
app.use(express.static(__dirname + '/public'));
app.get('/red_on', function(req, res) {
red.writeSync(1);
console.log('send: led red on');
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('LED Red On\n');
});
app.get('/red_off', function(req, res) {
red.writeSync(0);
console.log('send: led red off');
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('LED Red Off\n');
});
app.get('/green_on', function(req, res) {
grn.writeSync(1);
console.log('send: led green on');
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('LED Green On\n');
});
app.get('/green_off', function(req, res) {
grn.writeSync(0);
console.log('send: led green off');
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('LED Green Off\n');
});
app.get('/all_on', function(req, res) {
grn.writeSync(1);
red.writeSync(1);
console.log('send: led green | red on');
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('LED Green | Red On\n');
});
app.get('/all_off', function(req, res) {
grn.writeSync(0);
red.writeSync(0);
console.log('send: led green | red off');
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('LED Green | Red Off\n');
});
app.listen(port, function(err) {
console.log('Connected port - ' + port);
if (err) {
return console.log('Found error - ', err);
}
});
process.on('SIGINT', () => {
red.unexport();
grn.unexport();
button.unexport();
});
<!DOCTYPE html>
<html lang=ko>
<head>
<title>Control LED on RPi</title>
</head>
<body>
<table border="0">
<tr>
<td rowspan="3" width="20" height="50"></td>
<td colspan="4"><h2>Control LED on RPi</h2></td>
</tr>
<tr>
<td align="center"><img src="./images/offRed.gif" id="rd"></td>
<td>
<a href="/red_on" target="response" align="right">
<h4><p onClick="red_on()" >On </p></h4>
</a>
</td>
<td>&nbsp;/ </td>
<td>
<a href="/red_off" target="response">
<h4><p onClick="red_off()">Off</p></h4>
</a>
</td>
</tr>
<tr>
<td align="center"><img src="./images/offGreen.gif" id="grn"></td>
<td>
<a href="/green_on" target="response" align="right">
<h4><p onClick="green_on()">On </p> </h4>
</a>
</td>
<td>&nbsp;/ </td>
<td>
<a href="/green_off" target="response">
<h4><p onClick="green_off()">Off</p></h4>
</a>
</td>
</tr>
<tr>
<tr align="center">
<td></td>
<td><a href="/all_on" target="response">
<p onClick="all_on()">On All </p></a></td>
<td><a href="/all_off" target="response">
<p onClick="all_off()">Off All </p></a></td>
</tr>
</table>
<table>
<tr height="30">
<td width="20"></td>
<td>Status Message: </br> &nbsp;</br> &nbsp;</td>
<td>
<iframe src="about:blank" width="300" height="30" frameborder="0"
marginwidth="0" marginheight="0" name="response"></iframe>
<p></p>
</td>
</tr>
</table>
<script>
function all_on(){
document.getElementById("rd").src = "./images/onRed.gif";
document.getElementById("grn").src = "./images/onGreen.gif";
}
function all_off(){
document.getElementById("rd").src = "./images/offRed.gif";
document.getElementById("grn").src = "./images/offGreen.gif";
}
function red_on(){
document.getElementById("rd").src = "./images/onRed.gif";
}
function red_off(){
document.getElementById("rd").src = "./images/offRed.gif";
}
function green_on(){
document.getElementById("grn").src = "./images/onGreen.gif";
}
function green_off(){
document.getElementById("grn").src = "./images/offGreen.gif";
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment