Skip to main content
7 of 7
replaced http://raspberrypi.stackexchange.com/ with https://raspberrypi.stackexchange.com/

Can't Set/reset GPIO pins using nodejs, why?

I'm using Raspberry Pi model B Type 2 with ARM6 and Type 3 with ARM7. I have tested this code several times and after a week, when I tried, it is not working.

Below is my node js program:

var express = require('express')
var app = express()
var bodyParser = require('body-parser')
var gpio = require("gpio")
var ip_add = require('./getip.js')
console.log(ip_add)


app.use(bodyParser.urlencoded({ extended: false }))

// parse application/json
app.use(bodyParser.json())

app.get('/', function (req, res) {
  res.send('Hello World!')
})


app.post('/status', function(req, res){
    var status = req.query.status;
    console.log(status);
    console.log(req.body);
    var direction = req.body.status;
    var actionMap = {
        north: 11,
        east: 12,
        south: 7,
        west: 13,
        test: 5
    };
    console.log(actionMap[direction])
    res.send("success");

    setgpio = gpio.export(actionMap[direction], {
    ready: function() {
//          intervalTimer = setInterval(function() {
         setgpio.set();
         setTimeout(function() { setgpio.reset(); }, 3000);
//      }, 1000);
   }
});
 
})

app.listen(3000, function () {

  console.log('Example app listening on port 3000!')

var http = require('http');

var options = {
    host: '172.16.0.233',
    port: 8080,
    path: '/sample/ip_pi.jsp?myip=' + ip_add
    
};
console.log(options)
http.get(options,function(resp){
    resp.on('data',function(chunk){
    console.log("chunk:" + chunk);
    });
}).on("error", function(e){
    console.log("error:" + e.message);
});


});

Then I have tried the sample program from npmjs.com which was working before. I have to light four LEDs alternatively depending up on the status received from another host, all other parts of the code is working fine except glowing LEDs connected to GPIO.

What would be the possible reason?

What is the possible solution?

As suggested by joan, i have carried out the test as below,

GPIO Test:

  1. test the GPIO with nothing connected:

Test Method: using pigpio library of joan, from the stack exchange answer

a) ./x_pigpiod_if2 : pigpio version 58 Mode/PUD/read/write tests. Test 1.1 to Test 9.4 :Pass

b) ./x_pigpio.py

Python module 1.34 Python 2.7.9 Test 1.1 to Test 13.13 Pass

c) ./x_pigs : all ok

d) ./x_pipw : all ok


  1. testing LED using command-line

    sudo -i

    echo "23" > /sys/class/gpio/export echo "out" > /sys/class/gpio/gpio23/direction

    echo "1" > /sys/class/gpio/gpio23/value echo "0" > /sys/class/gpio/gpio23/value

    echo "23" > /sys/class/gpio/unexport

result: Sucess

3)test the LED using python program:

using RPi.GPIO package and simple python program

Test result: Failed!!!

I have also tried to install npm on off and rpi-gpio as an alternative for the current library npm gpio it failed, my stack exchange question

Now it is clear the hardware is ok, What could be the reason which stops my program/package accessing GPIO?

jasim
  • 123
  • 7