Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/460.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何使用raspberry pi node js获取GPIO引脚的加载状态?_Javascript_Node.js_Socket.io_Raspberry Pi_Gpio - Fatal编程技术网

Javascript 如何使用raspberry pi node js获取GPIO引脚的加载状态?

Javascript 如何使用raspberry pi node js获取GPIO引脚的加载状态?,javascript,node.js,socket.io,raspberry-pi,gpio,Javascript,Node.js,Socket.io,Raspberry Pi,Gpio,我使用的是raspberry 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, a

我使用的是raspberry 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/', _去BounceTimeout:0, _读缓冲区:, _侦听器:[[Function]], _价值fD:13, _真的, _错误的:是的,
_轮询器:Epoll{closed:false}

在到处寻找之后,我发现它一直在我面前

Buttown.readSync确实为我提供了正确的高/低值