Last active
October 25, 2020 08:13
-
-
Save Utopiah/fe5ce76b0cc840bc080dd743b85da077 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
//in crontab as @reboot node ~/Prototypes/blink_node_webserver/redlight.js | |
const express = require('express') | |
var Gpio = require('onoff').Gpio; //include onoff to interact with the GPIO | |
var YELLOW = new Gpio(4, 'out'); //use GPIO pin 4, and specify that it is output | |
var RED = new Gpio(21, 'out'); //use GPIO pin 4, and specify that it is output | |
var GREEN = new Gpio(26, 'out'); //use GPIO pin 4, and specify that it is output | |
var blinkInterval | |
const app = express() | |
app.get('/', function(req, res){ | |
res.send(` | |
<html> | |
<meta name="viewport" content="width=device-width, initial-scale=1"> | |
<a href=/start/red> | |
<div width=100% height=500px style=background-color:red> | |
<br><br><br><br> | |
</div> | |
</a> | |
<a href=/start/yellow> | |
<div width=100% height=500px style=background-color:yellow> | |
<br><br><br><br> | |
</div> | |
</a> | |
<a href=/start/green> | |
<div width=100% height=500px style=background-color:green> | |
<br><br><br><br> | |
</div> | |
</a> | |
</html> | |
`) | |
}); | |
app.get('/start/yellow', function(req, res){ | |
YELLOW.writeSync(1); //set pin state to 0 (turn LED off) | |
res.send(` | |
<html> | |
<meta name="viewport" content="width=device-width, initial-scale=1"> | |
red on <a href=/stop>stop</a> | |
`) | |
}); | |
app.get('/start/red', function(req, res){ | |
RED.writeSync(1); //set pin state to 0 (turn LED off) | |
res.send(` | |
<html> | |
<meta name="viewport" content="width=device-width, initial-scale=1"> | |
red on <a href=/stop>stop</a> | |
`) | |
}); | |
app.get('/start/green', function(req, res){ | |
GREEN.writeSync(1); //set pin state to 0 (turn LED off) | |
res.send(` | |
<html> | |
<meta name="viewport" content="width=device-width, initial-scale=1"> | |
green on <a href=/stop>stop</a> | |
`) | |
}); | |
app.get('/stop', function(req, res){ | |
RED.writeSync(0); //set pin state to 0 (turn LED off) | |
GREEN.writeSync(0); //set pin state to 0 (turn LED off) | |
YELLOW.writeSync(0); //set pin state to 0 (turn LED off) | |
res.redirect('/') | |
}); | |
app.get('/shutdown', function(req, res){ | |
RED.unexport(); // Unexport GPIO to free resources | |
GREEN.unexport(); // Unexport GPIO to free resources | |
YELLOW.unexport(); // Unexport GPIO to free resources | |
res.send("shutdown") | |
}); | |
app.listen(8888, '0.0.0.0', _ => { console.log("Listening port8888")}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment