简体   繁体   English

使用Node.js的Raspberry Pi 3 B型上的GPIO

[英]GPIO over Raspberry Pi 3 model B using Node.js

I am trying to blink the led using raspberry pi 3 model B, 我正在尝试使用树莓派3 B型来闪烁LED,

I have all the required modules installed on my machine ie npm , nodejs , pi-gpio (fixed the minor changes to detect the gpio ) 我已经在我的机器上安装了所有必需的模块,即npm,nodejs,pi-gpio(修复了微小的更改以检测gpio)

Code is : 代码是:

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, 但是上面的代码在运行时会引发错误,

node app.js 节点app.js

error : Error when trying to open pin 16 gpio-admin : could not flush data to /sys/class/gpio/export : Device or resource busy 错误: 尝试打开16针时出错gpio-admin:无法将数据刷新到/ sys / class / gpio / export:设备或资源繁忙

Thanks in advance 提前致谢

Any links where I can see the circuit diagram and code. 我可以在其中看到电路图和代码的任何链接。

Concern : I dont want to change the platform ie node.js 关注:我不想更改平台,即node.js

pi-gpio which i am using is : https://github.com/rakeshpai/pi-gpio 我正在使用的pi-gpio是: https : //github.com/rakeshpai/pi-gpio

pi-gpio is just writing to the GPIO device in the background so you can skip node.js and pi-gpio and do the same manually for testing purposes. pi-gpio只是在后台写入GPIO设备,因此您可以跳过node.jspi-gpio并手动执行相同操作以进行测试。

Example: 例:

gpio.open(16, ...
# is the same as writing in terminal:
echo 16 > /sys/class/gpio/export

and

... "output" ...
# is the same as writing in terminal:
echo "out" > /sys/class/gpio/gpio16/direction

etc. 等等

First of all, try rebooting the Pi and see if that takes care of the issue. 首先,尝试重新启动Pi,看看是否可以解决此问题。

If that didn't help, try manually closing/unexporting the pin as root and then re-run the script. 如果那没有帮助,请尝试以根用户身份手动关闭/取消导出该引脚,然后重新运行脚本。

# unexport the pin as root in case something's holding on to it
sudo echo 16 > /sys/class/gpio/unexport

The commands below basically constitute the pi-gpio API. 以下命令基本上构成了pi-gpio API。 These lines are what you would put in a shell script to control your GPIOs. 这些行就是您要在Shell脚本中放置以控制GPIO的内容。 Test them without sudo first in the order I've written them and if they don't work, try with sudo. 首先按照我编写它们的顺序在没有sudo的情况下对其进行测试,如果它们不起作用,请尝试使用sudo。 If they still don't work, I think you have a wiring/hardware problem or need to change some system settings elsewhere. 如果它们仍然不起作用,我认为您在布线/硬件方面有问题,或者需要在其他地方更改某些系统设置。

# 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

Let's use 'rpio' module instead of 'pi-gpio'. 让我们使用“ rpio”模块代替“ pi-gpio”。

https://github.com/jperkin/node-rpio https://github.com/jperkin/node-rpio

It works fine on Pi3, zero etc. 它在Pi3,零等上正常工作。

As pi-gpio has already fixed the old and new sysPath (issue# https://github.com/rakeshpai/pi-gpio ) 由于pi-gpio已经修复了旧的和新的sysPath(issue# https://github.com/rakeshpai/pi-gpio

But it is dependent on quick2wire-gpio-admin lib. 但这取决于quick2wire-gpio-admin lib。

So small fix required in quick2wire-gpio-admin 在quick2wire-gpio-admin中需要很小的修复

git clone https://github.com/quick2wire/quick2wire-gpio-admin.git
cd quick2wire-gpio-admin

The src/gpio-admin.c has src/gpio-admin.c具有

int size = snprintf(path, PATH_MAX, "/sys/devices/virtual/gpio/gpio%u/%s", pin, filename);

replace with : 用。。。来代替 :

int size = snprintf(path, PATH_MAX, GPIO_CLASS_PATH "gpio%u/%s", pin, filename);

Then go to cd quick2wire-gpio-admin directory 然后转到cd quick2wire-gpio-admin目录

Then sudo make uninstall and 然后sudo make uninstall

sudo make install

Then it is running fine. 然后运行正常。

The Code for is as follows : (filename : blinking12.js) 的代码如下:(文件名: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);

To run the code : 运行代码:

node blinking12.js

Output on my machine : 在我的机器上输出:

GPIO pin 12 is open toggling LED every 100mS for 10s
10 seconds blinking completed

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM