Skip to content

Instantly share code, notes, and snippets.

@Utopiah
Last active October 24, 2020 14:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Utopiah/19159b5e61cfd091dd6f92f5c66f4bb5 to your computer and use it in GitHub Desktop.
Save Utopiah/19159b5e61cfd091dd6f92f5c66f4bb5 to your computer and use it in GitHub Desktop.
onst express = require('express')
var Gpio = require('onoff').Gpio; //include onoff to interact with the GPIO
var LED = new Gpio(21, 'out'); //use GPIO pin 4, and specify that it is output
var blinkInterval
function blinkLED() { //function to start blinking
if (LED.readSync() === 0) { //check the pin state, if the state is 0 (or off)
LED.writeSync(1); //set pin state to 1 (turn LED on)
} else {
LED.writeSync(0); //set pin state to 0 (turn LED off)
}
}
function endBlink() { //function to stop blinking
clearInterval(blinkInterval); // Stop blink intervals
LED.writeSync(0); // Turn LED off
}
//setTimeout(endBlink, 9000); //stop blinking after 5 seconds
const app = express()
app.get('/', function(req, res){
res.send("running")
});
app.get('/start', function(req, res){
blinkInterval = setInterval(blinkLED, 250); //run the blinkLED function every 250ms
res.send("starting")
});
app.get('/stop', function(req, res){
endBlink()
res.send("ended")
});
app.get('/shutdown', function(req, res){
LED.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