Skip to content

Instantly share code, notes, and snippets.

@w4ilun
Last active August 29, 2015 14:10
Show Gist options
  • Save w4ilun/41b9959e98f3eeac5aa6 to your computer and use it in GitHub Desktop.
Save w4ilun/41b9959e98f3eeac5aa6 to your computer and use it in GitHub Desktop.
var m = require('mraa'); //IO Library
var app = require('express')(); //Express Library
var server = require('http').Server(app); //Create HTTP instance
var io = require('socket.io')(server); //Socket.IO Library
var blinkInterval = 1000; //set default blink interval to 1000 milliseconds (1 second)
var ledState = 1; //set default LED state
var myLed = new m.Gpio(13); //LED hooked up to digital pin 13
myLed.dir(m.DIR_OUT); //set the gpio direction to output
app.get('/', function (req, res) {
res.sendFile(__dirname + '/index.html'); //serve the static html file
});
io.on('connection', function(socket){
socket.on('changeBlinkInterval', function(data){ //on incoming websocket message...
blinkInterval = data; //update blink interval
});
});
server.listen(3000); //run on port 3000
blink(); //start the blink function
function blink(){
myLed.write(ledState); //write the LED state
ledState = 1 - ledState; //toggle LED state
setTimeout(blink,blinkInterval); //recursively toggle pin state with timeout set to blink interval
}
@w4ilun
Copy link
Author

w4ilun commented Jul 7, 2015

Thanks Rick! Fixed :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment