【问题标题】:How do I get status of GPIO pin on load using raspberry pi node js?如何使用树莓派节点 js 在加载时获取 GPIO 引脚的状态?
【发布时间】:2018-06-21 18:56:42
【问题描述】:

我使用的是树莓派,我已经加载了 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:[[功能]], _valueFd: 13, _risingEnabled:真, _fallingEnabled:真, _poller: Epoll { 关闭: false } }

【问题讨论】:

    标签: javascript node.js socket.io raspberry-pi gpio


    【解决方案1】:

    四处寻找之后,我发现它一直在我的面前。

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

    【讨论】:

    • 哦,是的,它就在那里。还是谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-06-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多