简体   繁体   English

如何使用树莓派pi节点js获取加载时GPIO引脚的状态?

[英]How do I get status of GPIO pin on load using raspberry pi node js?

I am using a raspberry pi and I have loaded node.js, and socket.io. 我正在使用树莓派,并且已经加载了node.js和socket.io。 I am able to see when a switch is changed on pin 18. What I would like to know is how do I get the status of a switch when the webpage is first loaded. 我可以看到何时在针脚18上更改了开关。我想知道的是,在首次加载网页时如何获取开关的状态。

In my app.js I have 在我的app.js中

var Gpio = require('onoff').Gpio; //include onoff to interact with the GPIO
var pushButton = new Gpio(18, 'in', 'both'); //use GPIO pin 18 as input, and 'both' button presses, and releases should be handled


io.sockets.on('connection', function (socket) {// WebSocket Connection
console.log("gpio status pin : ", pushButton);
  var lightvalue = 0;
  pushButton.watch(function (err, value) { //Watch for hardware interrupts on pushButton
if (err) { //if an error
  console.error('There was an error', err); //output error message to console
  return;
}
lightvalue = value;
socket.emit('light', lightvalue); //send button status to client
  });
socket.on('light', function(data){
console.log("data: ", data);
  });

});

I have tried many steps and the closet thing I can see if the read buffer changes from 30 to 31. This is printed from the pushbutton. 我已经尝试了许多步骤,并且可以对壁橱中的东西进行查看,看看读取缓冲区是否从30变为31。这是从按钮打印的。 Can I use this? 我可以用这个吗?

gpio status pin : Gpio { _gpio: 18, _gpioPath: '/sys/class/gpio/gpio18/', _debounceTimeout: 0, _readBuffer: , _listeners: [ [Function] ], _valueFd: 13, _risingEnabled: true, _fallingEnabled: true, _poller: Epoll { closed: false } } gpio状态引脚:gpio {_gpio:18,_gpioPath:'/ sys / class / gpio / gpio18 /',_debounceTimeout:0,_readBuffer:,_listeners:[[Function]],_valueFd:13,_risingEnabled:true,_fallingEnabled:true ,_poller:Epoll {已关闭:false}}

After looking everywhere, I found it was in front of my face the whole time. 看了四处之后,我发现它一直都在我的面前。

pushButton.readSync() does give me the correct high/low value. pushButton.readSync()确实为我提供了正确的高/低值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM