1

I am writing a daemon using node.js, running under systemd and it will monitor the status of the power supply voltage on a raspberry pi 2 B.

I am using the node.js wrapper to the pigpio library

The code I started with was as follows:

var Gpio = require('pigpio').Gpio,
gpio;

gpio = new Gpio(35);

const checkIntervalDuringBrownout   = 5000;
const checkIntervalDuringGoodSupply = 100;
var timer = 100;

function startTimer() {
  setTimeout(function() {
    var brownoutState = isUnderVoltage();

    if(brownoutState) {
      console.log("Brownout - telling the syslog ...");
      console.log("Brownout ... Power Supply Voltage went below 4.63 V");
      timer = checkIntervalDuringBrownout;
    }
    else {
      timer = checkIntervalDuringGoodSupply;
   }
    startTimer();
  }, timer);
}

function isUnderVoltage() {
  var ledStatus = gpio.digitalRead(); 
  if(ledStatus === 1) {
    return 0;
  }
  else if (ledStatus === 0) {
    //console.log("brownout");
    return 1;
  }
  else {
    console.log("We got neither a 1 or a 0: ", ledStatus);
  }
}

startTimer();

was getting mysterious error messages in the syslog:

Aug  2 00:22:10 chintu systemd[1]: Started Brownout Monitor and Logger written using node.js.
Aug  2 00:22:11 chintu brownout-monitor[5203]: 2017-08-02 00:22:11 initMboxBlock: init mbox zaps failed
Aug  2 00:22:11 chintu brownout-monitor[5203]: /home/pi/sandbox/raspi2-power-monitor/node_modules/pigpio/pigpio.js:11
Aug  2 00:22:11 chintu brownout-monitor[5203]: pigpio.gpioInitialise();
Aug  2 00:22:11 chintu brownout-monitor[5203]: ^
Aug  2 00:22:11 chintu brownout-monitor[5203]: Error: pigpio error -1 in gpioInitialise
Aug  2 00:22:11 chintu brownout-monitor[5203]: at Error (native)
Aug  2 00:22:11 chintu brownout-monitor[5203]: at initializePigpio (/home/pi/sandbox/raspi2-power-monitor/node_modules/pigpio/pigpio.js:11:12)
Aug  2 00:22:11 chintu brownout-monitor[5203]: at new Gpio (/home/pi/sandbox/raspi2-power-monitor/node_modules/pigpio/pigpio.js:25:3)

and

Aug  2 00:17:11 chintu systemd[1]: Starting Brownout Monitor and Logger written using node.js...
Aug  2 00:17:11 chintu systemd[1]: Started Brownout Monitor and Logger written using node.js.
Aug  2 00:17:12 chintu brownout-monitor[4555]: 2017-08-02 00:17:12 initInitialise: Can't lock /var/run/pigpio.pid
Aug  2 00:17:12 chintu brownout-monitor[4555]: /home/pi/sandbox/raspi2-power-monitor/node_modules/pigpio/pigpio.js:11
Aug  2 00:17:12 chintu brownout-monitor[4555]: pigpio.gpioInitialise();
Aug  2 00:17:12 chintu brownout-monitor[4555]: ^
Aug  2 00:17:12 chintu brownout-monitor[4555]: Error: pigpio error -1 in gpioInitialise
Aug  2 00:17:12 chintu brownout-monitor[4555]: at Error (native)
Aug  2 00:17:12 chintu brownout-monitor[4555]: at initializePigpio (/home/pi/sandbox/raspi2-power-monitor/node_modules/pigpio/pigpio.js:11:12)
Aug  2 00:17:12 chintu brownout-monitor[4555]: at new Gpio (/home/pi/sandbox/raspi2-power-monitor/node_modules/pigpio/pigpio.js:25:3)
Aug  2 00:17:12 chintu brownout-monitor[4555]: at Object.<anonymous> (/home/pi/sandbox/raspi2-power-monitor/index.js:7:8)
Aug  2 00:17:12 chintu brownout-monitor[4555]: at Module._compile (module.js:570:32)
Aug  2 00:17:12 chintu brownout-monitor[4555]: at Object.Module._extensions..js (module.js:579:10)
Aug  2 00:17:12 chintu brownout-monitor[4555]: at Module.load (module.js:487:32)
Aug  2 00:17:12 chintu brownout-monitor[4555]: at tryModuleLoad (module.js:446:12)
Aug  2 00:17:12 chintu brownout-monitor[4555]: at Function.Module._load (module.js:438:3)
Aug  2 00:17:12 chintu brownout-monitor[4555]: at Module.runMain (module.js:604:10)
Aug  2 00:17:12 chintu systemd[1]: brownout-monitor.service: main process exited, code=exited, status=1/FAILURE
Aug  2 00:17:12 chintu systemd[1]: Unit brownout-monitor.service entered failed state.

Looking up these errors led me to a page where the solution said -

I'll have to check the code but off the top of my head the most likely is that the GPU (which allocates mbox DMA memory) has no spare memory.

How can this be fixed?

0

The solution that fixed the errors was:

Handling the signals that systemd sends to the node process and ensuring that the pigpio library does a clean termination using the pigpio.terminate() call in the handler.

var pigpio = require('pigpio');
var Gpio = require('pigpio').Gpio,
    gpio;

//....

process.on('SIGHUP', shutdown);
process.on('SIGINT', shutdown);
process.on('SIGCONT', shutdown);
process.on('SIGTERM', shutdown);

function shutdown() {
  pigpio.terminate();
  clearInterval(timerVar);
  console.log('raspi2-brownout-watcher must exit, performed cleanup.');
  process.exit(0);
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.