User avatar
ebolisa
Posts: 68
Joined: Sat Dec 30, 2017 3:35 pm
Location: Madrid, Spain

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

Mon Apr 08, 2019 10:43 am

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();
})
I know that I know nothing

Heater
Posts: 13672
Joined: Tue Jul 17, 2012 3:02 pm

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

Mon Apr 08, 2019 1:41 pm

What exactly is the error message you get at that point?
Memory in C++ is a leaky abstraction .

User avatar
ebolisa
Posts: 68
Joined: Sat Dec 30, 2017 3:35 pm
Location: Madrid, Spain

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

Mon Apr 08, 2019 1:47 pm

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.
I know that I know nothing

Return to “Other programming languages”