Page 1 of 1

gpio - how to use unexport() correctly (node)

Posted: Mon Apr 08, 2019 10:43 am
by ebolisa
Hi,

I'm using the code shown below to trigger a relay for a second. Because the relay is shared by various programs, it's imperative that the gpio pin is released from this code.

However, the code crushes when unexport() function is reached. Without it, the code works fine.

A similar code works fine in Python.

Where do I err?

TIA
(BTW, I'm new to node)

Code: Select all

#!/usr/bin/env node

var Gpio = require('onoff').Gpio;
var Blynk = require('blynk-library');
var AUTH = '****';

var blynk = new Blynk.Blynk(AUTH,
    options = {
        connector: new Blynk.TcpClient()
    });

//set physical pins
const relayBlinds = new Gpio(21, 'out');

//set virtual pins
var btnBlinds = new blynk.VirtualPin(1);

const allRelaysOff = () => {
    relayBlinds.writeSync(1);
}
//turn off all relays @ start
allRelaysOff();

btnBlinds.on('write', function(param) {
    if (param) {
        relay_on(relayBlinds);
        setTimeout(function () {
          relay_off(relayBlinds);
        setTimeout(function () {
          relayBlinds.unexport();
        }, 1000);
      }, 1000);
     }
});

function relay_on(gpio) {
  gpio.writeSync(1);
}

function relay_off(gpio) {
  gpio.writeSync(0);
}

// Handle Ctrl+C exit cleanly 
process.on('SIGINT', () => {
    allRelaysOff();
    process.exit();
})

Re: gpio - how to use unexport() correctly (node)

Posted: Mon Apr 08, 2019 1:41 pm
by Heater
What exactly is the error message you get at that point?

Re: gpio - how to use unexport() correctly (node)(Solved)

Posted: Mon Apr 08, 2019 1:47 pm
by ebolisa
Once executed unexport(), the pin cannot be reused which is what the code was doing. So, said pin needs to be reinitialized before of its use.