Page 1 of 1

basics of node.js & rpi-gpio module

Posted: Sat Jan 04, 2014 8:09 am
by PIsingh
All i want to do i something real basic, use the rpi- gpio module to turn a led on. Using the following code
var gpio = require('rpi-gpio');

gpio.setup(11, gpio.DIR_OUT, write);

function write() {
gpio.write(11, true, function(err) {
if (err) throw err;
console.log('Written to pin');
});
}
The code is taken from https://npmjs.org/package/rpi-gpio
So when i run this code all i see is in the pi terminal window " written to pin"
but i don't get nothing on the multimeter. I have hooked up the negative to pin 6 and positive to pin 11. what am i doing wrong.
i also have node.js installed conformed it works +rpi-gpio.

Problem is I am beginner don't really understood the technical details. I just know that i want to do is like those guys on youtube who turn leds on off using node.js and rpi-gpio.
helps!!!!! or any thoughts
thanks in advance

Re: basics of node.js & rpi-gpio module

Posted: Sat Jan 04, 2014 8:35 am
by joan
Perhaps you have switched the gpio off again by the time you have come to measure the voltage?

A LED + resistor would be useful.

Also make sure you know the gpio numbering scheme used by the software you are using. There are at least three 1) Broadcom gpio numbers, 2) header pin numbers, 3) a "logical" numbering which numbers the "easiest" to use gpios first.

Re: basics of node.js & rpi-gpio module

Posted: Sat Jan 04, 2014 10:50 am
by Heater
PIsingh.

You have a function there that will write to a pin but you have never used (called) that function. You need to have a call to write somewhere in your program.

Next problem is that if you just put a call to write at the end of you existing code it will set the pin and then the program will exit.
node.js runs all your code from top to bottom and then quits if it has no work to do.

You could get arond this if you like by calling write at regular intervals like so:

Code: Select all

var gpio = require('rpi-gpio');

gpio.setup(11, gpio.DIR_OUT, write);

function write() {
    gpio.write(11, true, function(err) {
        if (err) throw err;
        console.log('Written to pin');
    });
}

// This is a normal call to write
write();

// Call write every 100ms
setInterval (write, 100);

Re: basics of node.js & rpi-gpio module

Posted: Tue Jan 07, 2014 3:41 am
by PIsingh
Heater wrote:PIsingh.
You have a function there that will write to a pin but you have never used (called) that function. You need to have a call to write somewhere in your program.

Code: Select all

var gpio = require('rpi-gpio');

gpio.setup(11, gpio.DIR_OUT, write);

function write() {
    gpio.write(11, true, function(err) {
        if (err) throw err;
        console.log('Written to pin');
    });
}
Thx for the reply guys!!!
it was actually a wiring problem as i was using a ribbon cable, but i figured it out.
can i get a simple example on how to have simple button in html trigger the write() function. I am beginner when it come to web app or html etc

Code: Select all

<!DOCTYPE html PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html>
   <head>   
</head>

<script>
var gpio = require('rpi-gpio');
gpio.setup(11, gpio.DIR_OUT, write);

function write() {
    gpio.write(11, true, function(err) {
        if (err) throw err;
        console.log('Written to pin');
    });
}

// This is a normal call to write
write();
// Call write every 100ms
setInterval (write, 100);

</script>
      <title>
         Hello world
      </title>

<body>
   <H1>Hello world</H1>
<input type="submit" value="press me" onclick="write()">
   
</body>
</html>
How do i do this? or how will this work????????????????