繁体   English   中英

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

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

我正在使用树莓派,并且已经加载了node.js和socket.io。 我可以看到何时在针脚18上更改了开关。我想知道的是,在首次加载网页时如何获取开关的状态。

在我的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);
  });

});

我已经尝试了许多步骤,并且可以对壁橱中的东西进行查看,看看读取缓冲区是否从30变为31。这是从按钮打印的。 我可以用这个吗?

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

看了四处之后,我发现它一直都在我的面前。

pushButton.readSync()确实为我提供了正确的高/低值。

暂无
暂无

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

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