node.js - How to control relays using Raspberry PI and Nodejs? -


i using watch method onoff module watch status of input, callback function called change in input (from 0 1 or 1 0) want.

the problem when first run application if main on (inputmain 1 ) or main off (inputmain 0 ) callback function not executed because there no change in value of input. if main on can't call main() function until becomes off on, problem happens when first run application.

how around ? there better approach handle relays ?

var gpio = require('onoff').gpio,  inputmain = new gpio(17,'in','both'),  inputgen1 = new gpio(4,'in','both'),  inputgen2 = new gpio(27,'in','both'),  inputgen3 = new gpio(22,'in','both'),  outmain = new gpio(11,'high'),  outgen1 = new gpio(15,'high'),  outgen2p = new gpio(18,'high'), // generator 2 power  outgen2sm = new gpio(23,'high'), //generator 2 starting motor  outgen2 = new gpio(24,'high'), // generator 2 contactor  outgen3p = new gpio(25,'high'),  outgen3sm = new gpio(8,'high'),  outgen3 = new gpio(7,'high'),  objects =[outmain,outgen1,outgen2p,outgen2sm,outgen2,outgen3p,outgen3sm,outgen3]; 

// checking if there main or not refrence other power sources

 inputmain.watch(function(err,state){    if (state)       main(1);   }); 

// switch main contactor

function main (arg) {  console.log('test');  if (arg == 1) {  value = [0,1,1,1,1,1,1,1];}  else {  value = [0,0,0,0,0,0,0,0];  }  out(value);  } 

// turn on relays depending on values of array ...

function out(value) {    ( var = 0; i< value.length; i++) {      if (value[i] == 0 )  {         var = objects[i];     } else {           objects[i].writesync(value[i]);     }  }     settimeout(function() {     a.writesync(0);     },5000); } 

i'm afraid need call read or readsync that.

... outgen3sm = new gpio(8,'high'), outgen3 = new gpio(7,'high'), objects =[outmain,outgen1,outgen2p,outgen2sm,outgen2,outgen3p,outgen3sm,outgen3]; //forces first output update main(inputmain.readsync()); 

Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -