2013/10/18

Flashing LED with Node.js on RPi

Compiling Nodejs for…before I went to sleep it took around 2 hours on my rpi. I think it’s quite stupid but the installation from apt-get didn’t work. Even I 'which node' there still nothing…so you know the story. If you want to compile it just follow this script from github.


So assume you have nodejs and npm installed, then we could start mess around with some modules. Lots of node modules for operating gpio like rpi-gpio, pi-gpio. For more you can check out here. Or search it with npm:



$ -> npm search gpio

image


I simply followed the instructions first install pi-gpio. With little js script:



var gpio = require("pi-gpio");

var intervalId;
var durationId;
var gpioPin = 22; // header pin 22 = GPIO port 25

// open pin 16 for output

gpio.open(gpioPin, "output", function(err) {
var on = 1;
console.log('GPIO pin '+gpioPin+' is open. toggling LED every 100 mS for 10s');
intervalId = setInterval( function() {
gpio.write(gpioPin, on, function() {
// toggle pin between high (1) and low (0)
on = (on + 1) % 2; }); }, 100
);

durationId = setTimeout( function(){
clearInterval(intervalId);
clearTimeout(durationId);
console.log('10 seconds blinking completed');
gpio.write(gpioPin, 0, function() {
// turn off pin 16
gpio.close(gpioPin); // then Close pin 16
process.exit(0); // and terminate the program
});
}, 10000); // duration in mS
});


And run it with sudo:



$ -> sudo node dev/gpio_test.js

Note that the var gpioPin isn’t the gpio pin, it should the header pin number. In my case I want to play with the LED connected on GPIO #25, then the gpioPin is 22. Check out this reference image.


image


Check out the self-content show-off video


http://cyberrob.tumblr.com/post/64373662871/clip-of-that-little-script-playing-with-led






via Tumblr http://cyberrob.tumblr.com/post/64373791101

沒有留言: