使用Node.js的Raspberry Pi 3 B型上的GPIO [英] GPIO over Raspberry Pi 3 model B using Node.js

查看:150
本文介绍了使用Node.js的Raspberry Pi 3 B型上的GPIO的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用树莓派3 B型来闪烁LED

I am trying to blink the led using raspberry pi 3 model B,

我已在计算机上安装了所有必需的模块,即npm,nodejs,pi-gpio(已修复了较小的更改以检测gpio)

I have all the required modules installed on my machine i.e. npm , nodejs , pi-gpio (fixed the minor changes to detect the 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);
    });
});

但是上面的代码在运行时会引发错误,

But above code throws error while running it,

节点app.js

错误: 尝试打开针脚16时出错 gpio-admin:无法将数据刷新到/sys/class/gpio/export:设备或资源繁忙

error : Error when trying to open pin 16 gpio-admin : could not flush data to /sys/class/gpio/export : Device or resource busy

预先感谢

可以在其中看到电路图和代码的任何链接.

Any links where I can see the circuit diagram and code.

关注点:我不想更改平台,即node.js

Concern : I dont want to change the platform i.e. node.js

pi-gpio是: https://github.com/rakeshpai/pi-gpio

pi-gpio which i am using is : https://github.com/rakeshpai/pi-gpio

推荐答案

由于pi-gpio已经修复了旧的和新的sysPath(issue#

As pi-gpio has already fixed the old and new sysPath (issue# https://github.com/rakeshpai/pi-gpio )

但这取决于quick2wire-gpio-admin库.

But it is dependent on 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

然后运行正常.

的代码如下:(文件名:blinking12.js)

The Code for is as follows : (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

这篇关于使用Node.js的Raspberry Pi 3 B型上的GPIO的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆