NodeJS onoff issue

Thanks for these instructions. Like to use onoff for the reasons you have stated rather than go the WiringPi route. However, when following the instructions to the letter, I do still get a problem with onoff.

All software is installed OK and have valid version numbers.

When I call onoff in my script:

var onoff = require('/usr/lib/node_modules/onoff');

and run node:

sudo NODE_PATH=/usr/lib/node_modules node ./test3.js

I get the following error:

/usr/lib/node_modules/onoff/node_modules/bindings/bindings.js:96
throw err
^

Error: Could not locate the bindings file. Tried:
 → /usr/lib/node_modules/onoff/node_modules/epoll/build/epoll.node
 → /usr/lib/node_modules/onoff/node_modules/epoll/build/Debug/epoll.node
 → /usr/lib/node_modules/onoff/node_modules/epoll/build/Release/epoll.node
 → /usr/lib/node_modules/onoff/node_modules/epoll/out/Debug/epoll.node
 → /usr/lib/node_modules/onoff/node_modules/epoll/Debug/epoll.node
 → /usr/lib/node_modules/onoff/node_modules/epoll/out/Release/epoll.node
 → /usr/lib/node_modules/onoff/node_modules/epoll/Release/epoll.node
 → /usr/lib/node_modules/onoff/node_modules/epoll/build/default/epoll.node
 → /usr/lib/node_modules/onoff/node_modules/epoll/compiled/6.12.0/linux/arm/epoll.node
      at bindings (/usr/lib/node_modules/onoff/node_modules/bindings/bindings.js:93:9)
      at Object.<anonymous> (/usr/lib/node_modules/onoff/node_modules/epoll/epoll.js:1:99)
      at Module._compile (module.js:570:32)
      at Object.Module._extensions..js (module.js:579:10)
      at Module.load (module.js:487:32)
      at tryModuleLoad (module.js:446:12)
      at Function.Module._load (module.js:438:3)
      at Module.require (module.js:497:17)
      at require (internal/module.js:20:19)
      at Object.<anonymous> (/usr/lib/node_modules/onoff/onoff.js:4:27)

If I run default Blynk client:

export PATH=$PATH:/opt/nodejs/bin/
unset NODE_PATH
blynk-client MyAuthToken

I get the following when I press a GP button on and then off in the app:

Connecting to: blynk-cloud.com 8441
SSL authorization...
Connected
Authorized
Blynk ready.
No direct pin operations available.
Maybe you need to install mraa or onoff modules?
No direct pin operations available.
Maybe you need to install mraa or onoff modules?
^C

Calling node-dht-sensor works fine and my 2 sensors are read correctly into Blynk app.

I wish to use onoff to control some relays.

I have a Raspberry Pi 3b running Rapbian v9 (Stretch) and use the Blynk cloud service.

Do you have any suggestions how to fix this?

Hello and Welcome.

Because the topic you linked to is rather old and we prefer not to merge multiple issue into one long topic if possible, I moved your issue into your own current topic.

Not sure how much of this matters, but when you installed Node, did you follow the Manual or Automatic method?

Are you running Blynk Globally or did you create a new Node.js module with local Blynk library dependency?

If with Local dependency did you install onoff like this:

npm install onoff --save

I don’t load onoff like this in my sketch, so I am unsure how you are using it in yours… actually I try to avoid direct GPIO control from App as I find it iffy (particularly PWM)… I prefer virtual pins in the sketch and use this in my sketch:

var Gpio = require('onoff').Gpio;  // links variable 'Gpio' to the all important onoff GPIO control library

and then control the GPIO pins like this for a physical button input and Widget LED output:

button = new Gpio(17, 'in', 'both');  // Sets up the BCM pin 17 as an input registering both rising and falling for the variable "button"
button.watch(function (err, value) {  // Watches for button press and assigns 0/1 to value
  if (err) {
    throw err;  // Some form of error handling
  }
  if (value  == 0) {
    blynk.virtualWrite(2, 0);  // V2 Widget (BLUE) LED off
    blynk.virtualWrite(3, 255);  // V3 Widget (RED) LED on
  }   else if (value == 1) {
    blynk.virtualWrite(2, 255);  // V2 (BLUE) Widget LED on
    blynk.virtualWrite(3, 0);  // V3 (RED) Widget LED off
  }
});

Can you paste your whole sketch here?

Thank you Gunner. Will post new topics going forward. For anyone else reading this, this is the Blynk instructions that gives me the error:

http://help.blynk.cc/how-to-connect-different-hardware-with-blynk/raspberry-pi/how-to-install-nodejs-library-on-linux

Also, these Raspberry Pi - specific instructions give me the same error:

http://docs.blynk.cc/#hardware-set-ups-raspberry-pi

When I installed node, I used the automatic method. As noted, this completed without error or warning

I installed onoff with:

sudo npm install onoff -g

I have now installed with:

pi@clim-crtl:~ $ npm install onoff --save
pi@1.0.0 /home/pi
└── onoff@1.1.8

npm WARN pi@1.0.0 No repository field.

If I run “sudo npm install onoff -g --save”, I do not get the repository warning

In either case, this gives the same onoff errors.

When I run node using your suggested:

var Gpio = require('onoff').Gpio;

I get the same bindings epoll errror.

When I run default Blynk client, I get the same response:

Maybe you need to install mraa or onoff modules?

So far I have not got to run any code yet in my sketch and just want to successfully reference the onoff library.

As I am running Raspberry Pi 3b with an ARMv7 processor, I have changed the following command:

curl -sL https://deb.nodesource.com/setup_6.x | sudo -E bash -

to

curl -sL https://deb.nodesource.com/setup_7.x | sudo -E bash -

After purging the old version as per top of page instructions:

sudo apt-get purge node nodejs node.js -y
sudo apt-get autoremove
curl -sL https://deb.nodesource.com/setup_6.x | sudo -E bash -
sudo apt-get update && sudo apt-get upgrade
sudo apt-get update && sudo apt-get upgrade --fix-missing
sudo apt-get install build-essential nodejs -y
sudo npm install blynk-library -g
sudo npm install onoff -g

Your code suggests that a physical button should switch on a LED light on Blynk. I am trying to achieve the opposite. That is to use a Blynk on-screen button to manually switch a relay connected to a GPIO pin to turn on a fan.

See my sketch below. As noted the 2 temp/humidity sensors work fine. The relay controls do not.

// Include relevant libraries
var blynkLib = require('/usr/lib/node_modules/blynk-library');
var sensorLib = require('/usr/lib/node_modules/node-dht-sensor');

// Authentication string for Blynk on my mobile phone
var AUTH = 'MyAuthString';

// Setup Blynk
var blynk = new blynkLib.Blynk(AUTH);

// Setup relays
var Gpio = require('onoff').Gpio,
  relay = new Gpio(19, 'out');
var button = new blynk.VirtualPin(1);
button.on('write', function(param) {
  console.log('V1:', param);
  relay.writeSync(param.toString());
});

// Setup Indoor and Outdoor sensors, read Temperature and Humidity from each physical pin, send reading to console 
and Blynk virtual pins
var sensor = {
        sensors: [ {
                name: "Indoor",
                type: 22,
                pin: 3
        }, {
                name: "Outdoor",
                type: 22,
                pin: 4
        } ],
        read: function() {
                for (var a in this.sensors) {
                        var b = sensorLib.read(this.sensors[a].type, this.sensors[a].pin);
                        console.log(this.sensors[a].name + ": " +
                        b.temperature.toFixed(1) + "C, " +
                        b.humidity.toFixed(1) + "%");
                        blynk.virtualWrite(a*2+1, b.temperature.toFixed(1));
                        blynk.virtualWrite(a*2+2, b.humidity.toFixed(1));
                }
                setTimeout(function() {
                        sensor.read();
                }, 2000);
        }
};

sensor.read();

Latest error message is now slightly different pointing to a software problem:

pi@clim-crtl:~ $ sudo node blynk-sensor-test3.js
Connecting to: blynk-cloud.com 8441
/home/pi/node_modules/bindings/bindings.js:88
        throw e
        ^

Error: The module '/home/pi/node_modules/epoll/build/Release/epoll.node'
was compiled against a different Node.js version using
NODE_MODULE_VERSION 48. This version of Node.js requires
NODE_MODULE_VERSION 51. Please try re-compiling or re-installing
the module (for instance, using `npm rebuild` or`npm install`).
    at Object.Module._extensions..node (module.js:598:18)
    at Module.load (module.js:488:32)
    at tryModuleLoad (module.js:447:12)
    at Function.Module._load (module.js:439:3)
    at Module.require (module.js:498:17)
    at require (internal/module.js:20:19)
    at bindings (/home/pi/node_modules/bindings/bindings.js:81:44)
    at Object.<anonymous> (/home/pi/node_modules/epoll/epoll.js:1:99)
    at Module._compile (module.js:571:32)
    at Object.Module._extensions..js (module.js:580:10)

My apologies for the long post summarising my feeble attempts, but how do I get tot the correct version of node?

Dooh! The answer was in the post:

npm rebuild

This correctly lined up the version numbers and the node error has gone away. No more onoff bindings error messages.

Will look to get some code to get my Blynk button to control my relays now through my sketch.

1 Like

The script is now working nicely. Control 3 fans and a heater and monitor the DHT22 indoor and outdoor temperature/humidity via Blynk.

called script automatically from etc/systemd/system with a .service script and been running OK for past day.

Case solved :slight_smile:

Next task; automate the fans and heater to control the climate. Will share as a project when done.

// Link onoff Gpio control library
var Gpio = require("onoff").Gpio;

// Include relevant libraries
var blynkLib = require('/usr/lib/node_modules/blynk-library');
var sensorLib = require('/usr/lib/node_modules/node-dht-sensor');

// Authentication string for Blynk on my mobile phone
var AUTH = 'MyAuthString';

// Setup Blynk
var blynk = new blynkLib.Blynk(AUTH);

// Setup button widgets
var FanOutButton = new blynk.VirtualPin(6);
var FanInButton = new blynk.VirtualPin(7);
var FanCirculateButton = new blynk.VirtualPin(8);
var HeaterButton = new blynk.VirtualPin(9);

// Assign relay ports
FanOutRelay = new Gpio(26, 'out');
FanInRelay = new Gpio(19, 'out');
FanCirculateRelay = new  Gpio(13, 'out');
HeaterRelay = new Gpio(6, 'out');

// Switch off all fans and heater
FanOutRelay.writeSync(1);
FanInRelay.writeSync(1);
FanCirculateRelay.writeSync(1);
HeaterRelay.writeSync(1);

// Poll Blynk buttons and switch relays as needed
FanOutButton.on('write', function(param1) { // Watch for V6 button
  console.log('FanOut: ', param1.toString()); // print value to CLI
  if(FanOutRelay.readSync() == 0) { // toggle gpio 19 from high to low
    FanOutRelay.writeSync(1);
    }
    else {
  FanOutRelay.writeSync(0);
    }
  });
 FanInButton.on('write', function(param2) { // Watch for V7 button
  console.log('FanIn: ', param2.toString()); // print value to CLI
  if(FanInRelay.readSync() == 0) { // toggle gpio 19 from high to low
    FanInRelay.writeSync(1);
    }
    else {
    FanInRelay.writeSync(0);
    }
  });
FanCirculateButton.on('write', function(param3) { // Watch for V8 button
  console.log('FanCirculate: ', param3.toString()); // print value to CLI
  if(FanCirculateRelay.readSync() == 0) { // toggle gpio 19 from high to low
    FanCirculateRelay.writeSync(1);
    }
else {
    FanCirculateRelay.writeSync(0);
    }
  });
HeaterButton.on('write', function(param4) { // Watch for V9 button
  console.log('Heater: ', param4.toString()); // print value to CLI
  if(HeaterRelay.readSync() == 0) { // toggle gpio 19 from high to low
  HeaterRelay.writeSync(1);
   }
  else {
  HeaterRelay.writeSync(0);
}
});

// Setup Indoor and Outdoor sensors, read Temperature and Humidity from each physical pin, send reading to console 
and Blynk virtual pins
var sensor = {
        sensors: [ {
                name: "Indoor",
                type: 22,
                pin: 3
        }, {
                name: "Outdoor",
                type: 22,
                pin: 4
        } ],
        read: function() {
                for (var a in this.sensors) {
                        var b = sensorLib.read(this.sensors[a].type, this.sensors[a].pin);
                        console.log(this.sensors[a].name + ": " +
                        b.temperature.toFixed(1) + "C, " +
                        b.humidity.toFixed(1) + "%");
                        blynk.virtualWrite(a*2+1, b.temperature.toFixed(1));
                        blynk.virtualWrite(a*2+2, b.humidity.toFixed(1));
                }
                setTimeout(function() {
                        sensor.read();
                }, 2000);
        }
};

sensor.read();
1 Like

Thanks for this… it is nice to have more Blynk Client with NodeJS examples :+1:

Hi everyone and Gunner,

I have the same issue with the user mhorst; He finally solve his issue by running “npm rebuild” command. But it didn’t worked for me. I have already done all the same steps from top to the bottom as he mentioned. (with the instruction links motioned of 2)

I am trying test.js file with the following one line code:
var Gpio = require('onoff').Gpio;

Because of I run “npm rebuild” the following error has gone:

Error: The module '/home/pi/node_modules/epoll/build/Release/epoll.node'
        was compiled against a different Node.js version using
        NODE_MODULE_VERSION 46. This version of Node.js requires
        NODE_MODULE_VERSION 51. Please try re-compiling or re-installing
        the module (for instance, using `npm rebuild` or`npm install`).

After I solved this error, I run blynk-client <token>
Then open mobile app and click the button LED ON/OFF switch. The following error message ocurrs:

 No direct pin operations available.
 Maybe you need to install mraa or onoff modules?

Could you please help. There are some other post that issuing the same problem, even they marked as Solved, but there are no real action suggested in them.

Note: After I had posted this, I started on working another Raspberry Pi 3 which have a fresh OS installation. Now I am stucked on the step “sudo npm install onoff -g”. It raises an infinite loop of following messages:

gyp WARN EACCES attempting to reinstall using temporary dev dir     "/usr/lib/node_modules/onoff/node_modules/epoll/.node-gyp"
    gyp WARN EACCES user "root" does not have permission to access the dev dir "/usr/lib/node_modules/onoff/node_modules/epoll/.node-gyp/4.8.2"

i faced the same problem and i tried “npm rebuild”
but my problem was not solved iam still getting the same error “npm WARN pi@1.0.0 No repository field”