(六)树莓派3B+ node-rpio库的使用--点亮LED

node-rpio又可以叫rpio。是一个node.js版本的树莓派GPIO库。关于介绍可以直接看文档:https://www.npmjs.com/package/rpio。

安装

1.sudo npm install rpio
这个时候是会报错的:

root@raspberrypi:/# sudo npm install rpio

> rpio@0.9.22 install /node_modules/rpio
> node-gyp rebuild

gyp: /root/.node-gyp/10.9.0/common.gypi not found (cwd: /node_modules/rpio) while reading includes of binding.gyp while trying to load binding.gyp
gyp ERR! configure error 
gyp ERR! stack Error: `gyp` failed with exit code: 1
gyp ERR! stack     at ChildProcess.onCpExit (/home/pi/node-v10.9.0-linux-armv7l/lib/node_modules/npm/node_modules/node-gyp/lib/configure.js:345:16)
gyp ERR! stack     at ChildProcess.emit (events.js:182:13)
gyp ERR! stack     at Process.ChildProcess._handle.onexit (internal/child_process.js:239:12)
gyp ERR! System Linux 4.14.62-v7+
gyp ERR! command "/usr/local/bin/node" "/home/pi/node-v10.9.0-linux-armv7l/lib/node_modules/npm/node_modules/node-gyp/bin/node-gyp.js" "rebuild"
gyp ERR! cwd /node_modules/rpio
gyp ERR! node -v v10.9.0
gyp ERR! node-gyp -v v3.7.0
gyp ERR! not ok 
npm WARN enoent ENOENT: no such file or directory, open '/package.json'
npm WARN !invalid#1 No description
npm WARN !invalid#1 No repository field.
npm WARN !invalid#1 No README data
npm WARN !invalid#1 No license field.

npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! rpio@0.9.22 install: `node-gyp rebuild`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the rpio@0.9.22 install script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /root/.npm/_logs/2018-08-27T06_29_10_218Z-debug.log

node-gyp的错误。
2.解决node-gyp的错误

(1)更新npm

sudo npm cache clean -f
sudo npm install npm -g

(2).重新安装node-gyp 来替换node自带node-gyp

sudo npm uninstall node-gyp -g
sudo npm uninstall node-gyp
sudo npm install node-gyp -g 

这个解决办法也是我找了很久才找到的一个实际可行,参考《解决 binding.gyp not found (xxx/xxx/xxx) while trying to load binding.gyp 问题》。
3.安装rpio:sudo npm install rpio

root@raspberrypi:/# sudo npm install rpio

> rpio@0.9.22 install /node_modules/rpio
> node-gyp rebuild

make: Entering directory '/node_modules/rpio/build'
  CC(target) Release/obj.target/rpio/src/bcm2835.o
  CXX(target) Release/obj.target/rpio/src/rpio.o
  SOLINK_MODULE(target) Release/obj.target/rpio.node
  COPY Release/rpio.node
make: Leaving directory '/node_modules/rpio/build'
npm WARN saveError ENOENT: no such file or directory, open '/package.json'
npm WARN enoent ENOENT: no such file or directory, open '/package.json'
npm WARN !invalid#1 No description
npm WARN !invalid#1 No repository field.
npm WARN !invalid#1 No README data
npm WARN !invalid#1 No license field.

+ rpio@0.9.22
added 3 packages from 9 contributors and audited 3 packages in 37.601s
found 0 vulnerabilities

4.验证gpio -v

root@raspberrypi:/# gpio -v
gpio version: 2.46
Copyright (c) 2012-2018 Gordon Henderson
This is free software with ABSOLUTELY NO WARRANTY.
For details type: gpio -warranty

Raspberry Pi Details:
  Type: Pi 3+, Revision: 03, Memory: 1024MB, Maker: Sony 
  * Device tree is enabled.
  *--> Raspberry Pi 3 Model B Plus Rev 1.3
  * This Raspberry Pi supports user-level GPIO access.

到这里表示rpio安装成功了。

点亮第一个LED

1.在/home/pi/目录下编写blink.js
(1)新建文件:touch blink.js
(2)打开文件:vim blink.js
(3)写入代码:

var rpio = require('rpio');
rpio.open(29, rpio.OUTPUT,rpio.LOW); // 第29引脚 设置为输出  默认输出低电平

function blink() {
   while(1){
     rpio.write(29, rpio.HIGH); // 输出高
     rpio.msleep(500);          // 延时500ms
     rpio.write(29, rpio.LOW);  // 输出低
     rpio.msleep(500);          // 延时500ms
   }
}
blink();

2.电路连接
(六)树莓派3B+ node-rpio库的使用--点亮LED_第1张图片
这里大家需要注意的,和我的上一篇:《
(五)树莓派3B+ wiringPi库的使用–点亮LED》这里写链接内容中的引脚对饮有关系。rpio使用的是无物理引脚BOARD编码
GPIO.21 –> led正极 –> led负极 –> GND
(六)树莓派3B+ node-rpio库的使用--点亮LED_第2张图片
3.执行:node blink.js
这里我们就可以看见LED开始间隔500毫秒的闪烁了。

总结

网上关于这个库的使用,基本都没有安装过程。库中的这段估计也是很多人迷惑的。

$ cat >/etc/udev/rules.d/20-gpiomem.rules <<EOF
SUBSYSTEM=="bcm2835-gpiomem", KERNEL=="gpiomem", GROUP="gpio", MODE="0660"
EOF

其实我也不知道,但是就这么执行一次就行。

你可能感兴趣的:(树莓派学习记录)