我使用的是覆盆子pi,我已经加载了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 { closed: false }}
https://stackoverflow.com/questions/50975770
复制相似问题