【发布时间】: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