我正在尝试使用树莓派3模型B来眨眼,
我在我的机器上安装了所有必需的模块,即npm,nodejs,pi-gpio(修复了微小的变化以检测gpio)
代码是:
var gpio = require("pi-gpio");
gpio.open(16, "output", function(err) {
gpio.write(16, 1, function() {
gpio.close(16);
});
});
//reading the data on the pin i.e pin : 16
gpio.open(16, "output", function (err) {
gpio.read(16, function (err, value) {
console.log("Data is "+ value);
gpio.close(16);
});
});
但是上面的代码在运行时会抛出错误,
节点app.js
错误: 尝试打开16针时出错 gpio-admin:无法将数据刷新到/ sys / class / gpio / export:设备或资源繁忙
提前致谢
我可以看到电路图和代码的任何链接。
关注:我不想改变平台,即node.js
我正在使用的pi-gpio是:https://github.com/rakeshpai/pi-gpio答案 0 :(得分:4)
pi-gpio
只是在后台写入GPIO设备,因此您可以跳过node.js
和pi-gpio
并手动执行相同操作以进行测试。
示例:
gpio.open(16, ...
# is the same as writing in terminal:
echo 16 > /sys/class/gpio/export
和
... "output" ...
# is the same as writing in terminal:
echo "out" > /sys/class/gpio/gpio16/direction
等
首先,尝试重新启动Pi并查看是否可以解决问题。
如果这没有帮助,请尝试以root身份手动关闭/取消导出该引脚,然后重新运行该脚本。
# unexport the pin as root in case something's holding on to it
sudo echo 16 > /sys/class/gpio/unexport
以下命令基本上构成pi-gpio
API。这些行是您将用于控制GPIO的shell脚本。首先按照我写的顺序测试它们,如果它们没有工作,请尝试使用sudo。如果他们仍然无法工作,我认为您有布线/硬件问题或需要在其他地方更改某些系统设置。
# unexport the pin
sudo echo 16 > /sys/class/gpio/unexport
# export it again
sudo echo 16 > /sys/class/gpio/export
# make it an output
sudo echo "out" > /sys/class/gpio/gpio16/direction
# write a HIGH - is the LED on now?
sudo echo 1 > /sys/class/gpio/gpio16/value
# read the value of the pin - is it 1 after writing a 1 to the pin?
cat /sys/class/gpio/gpio16/value
# write a LOW - did it turn off?
sudo echo 0 > /sys/class/gpio/gpio16/value
答案 1 :(得分:1)
答案 2 :(得分:1)
因为pi-gpio已经修复了旧的和新的sysPath(问题#https://github.com/rakeshpai/pi-gpio)
但它依赖于quick2wire-gpio-admin lib。
在quick2wire-gpio-admin
中需要这么小的修复git clone https://github.com/quick2wire/quick2wire-gpio-admin.git
cd quick2wire-gpio-admin
src/gpio-admin.c
有
int size = snprintf(path, PATH_MAX, "/sys/devices/virtual/gpio/gpio%u/%s", pin, filename);
替换为:
int size = snprintf(path, PATH_MAX, GPIO_CLASS_PATH "gpio%u/%s", pin, filename);
然后转到cd quick2wire-gpio-admin
目录
然后sudo make uninstall
和
sudo make install
然后运行正常。
代码如下:(filename:blinking12.js)
var gpio = require("pi-gpio");
var intervalId;
var durationId;
var gpioPin = 12;
gpio.open(gpioPin, "output", function (err) {
var on =1 ;
console.log("GPIO pin "+gpioPin+" is open toggling LED every 100mS for 10s");
intervalId = setInterval( function () {
gpio.write(gpioPin, on, function () {
on = (on + 1)% 2;
});
}, 100);
});
durationId = setTimeout (function () {
clearInterval(intervalId);
clearTimeout(durationId);
console.log('10 seconds blinking completed');
gpio.write(gpioPin, 0, function () {
gpio.close(gpioPin);
//process.exit(0);
});
}, 10000);
运行代码:
node blinking12.js
我机器上的输出:
GPIO pin 12 is open toggling LED every 100mS for 10s
10 seconds blinking completed