Using a Pi Zero to monitor some input pins, a change on the input will trigger an event.
This is using NPM onff (https://www.npmjs.com/package/onoff).
The example code with a button worked well for a single input, but to have some cleaner code that had an array of inputs an ability to identify the pin that triggered the event was required. The watch callback only takes two parameters, so a closure (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Closures ) is needed (hints here https://www.raspberrypi.org/forums/viewtopic.php?t=152105 ).
The code below is a simple example of this. An array of sensorPin values that are acting as the input triggers. The Gpio inputs are
/** * Simple code to have notifications (interrupts) when the signal to the input pins change. */ const Gpio = require('onoff').Gpio; var sensorPins = [27,22,5,6,13,26,23,18]; var sensors = []; // Loop through each configured sensor and create the onoff for (var i=0; i<sensorPins.length; i++) { // Use a closure to identify the pin in the callback function createSensor(pin) { var sensor = new Gpio(pin,'in','rising', {debounceTimeout:10}) sensor.watch(function (err, value) { if (err) { throw err; } console.log("Pin "+pin+":"+value); }); }; var newSensor = createSensor(sensorPins[i]); sensors.push(newSensor); } /** * Ctrl-c handler */ process.on('SIGINT', function () { for (var i=0; i<sensorPins.length; i++) { sensors[i].unexport(); } });
gist: https://gist.github.com/jastill/929a09f80c03a44b21b5710cf16c1b81