簡體   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