Control Raspberry Pi GPIO pins with node.js. Fast and easy to use.
npm install rpio2



Export elegant OOP APIs to control Raspberry Pi GPIO pins with Node.js. Based on node-rpio which is a high performace library.
``bash`
npm install rpio2 --production
By default the module will use /dev/gpiomem when using simple GPIO access.gpio
To access this device, your user will need to be a member of the group,
and you may need to configure udev with the following rule (as root):
`console`
$ cat >/etc/udev/rules.d/20-gpiomem.rules <
EOF
For access to i²c, PWM, and SPI, or if you are running an older kernel which
does not have the bcm2835-gpiomem module, you will need to run your programs/dev/mem
as root for access to .
Synchronously
`js
const Gpio = require('./lib/index.js').Gpio;
const gpio = new Gpio(40);
gpio.open(Gpio.OUTPUT);
for(var i = 0; i < 10; i++){
gpio.toggle();
gpio.sleep(500);
}
gpio.close();
`
Asynchronously
`js
const Gpio = require('../lib/index.js').Gpio;
const gpio = new Gpio(40);
gpio.open(Gpio.OUTPUT);
void function loop(){
Promise.resolve(gpio.toggle())
.then(gpio.sleep.bind(null, 500, true))
.then(loop)
}();
process.on("SIGINT", function(){
gpio.close();
console.log('shutdown!');
process.exit(0);
});
`
Toggle with button
`js
const Gpio = require('../lib/index.js').Gpio;
const button = new Gpio(32);
const output = new Gpio(40);
button.open(Gpio.INPUT);
output.open(Gpio.OUTPUT, Gpio.LOW);
//button down
button.on('rising', function(){
output.toggle();
});
process.on("SIGINT", function(){
button.close();
output.close();
console.log('shutdown!');
process.exit(0);
});
`
#### Methods
* [Gpio(pin[, activeLow]) - Constructor](#gpiopin-activelow)
* [open(mode[, state]) - Export the GPIO to userspace](#openmode-state)
* close() - Unexport the GPIO
* read() - Get GPIO value
* write(value) - Set GPIO value
* toggle() - Change GPIO value
* [sleep(ms[,async]) - Sleep for a few milliseconds](#sleepmsasync)
* [createReadStream(pin[, options]) - Create a readable stream from a pin](#createreadstreampin-options)
* [createWriteStream(pin[, options]) - Create a writable stream for a pin](#createwritestreampin-options)
* [group(pins[, activeLow]) - Create a group of GPIOs](#grouppins-activelow)
#### Statics
* Gpio.init(options) - initialize GPIO's global settings
* Gpio.sleep(ms) - Sleep for a few milliseconds
#### Properties
* value:0|1 - Set/Get GPIO value
* state:0|1|2 - Set/Get GPIO state
* mode:0|1 - Set/Get GPIO input/output direction
* activeLow:boolean - GPIO activeLow setting
#### Events
* rising - Triggering an event when GPIO signal rising
* falling - Triggering an event when GPIO signal falling
* change - Triggering an event when GPIO signal rising or falling
#### Constants
- Gpio.UNKNOWN = -1;
- Gpio.HIGH = 1;
- Gpio.LOW = 0;
- Gpio.INPUT = 0;
- Gpio.OUTPUT = 1;
- Gpio.PULL_OFF = 0;
- Gpio.PULL_DOWN = 1;
- Gpio.PULL_UP = 2;
- Gpio.PULL_DEFAULT = -1;
- Gpio.POLL_NONE = 0;
- Gpio.POLL_LOW = 1;
- Gpio.POLL_HIGH = 2;
- Gpio.POLL_BOTH = 3;
* [GpioGroup(pins[, activeLow]) - Constructor](#gpiogrouppins-activelow)
---
##### Gpio(pin[,activeLow])
new Gpio(pin[,activeLow]) creates a GPIO pin instance. The arguments pin use the physical numbering (P01-P40) by default:
``
let pin1 = new Gpio(40); //create P40 (gpio21)
The option parameter activeLow specifies whether the values read from or written to the GPIO should be inverted. The interrupt generating edge for the GPIO also follow this setting. The valid values for activeLow are true and false. Setting activeLow to true inverts. The default value is false.
The pin number mapping to GPIO number as below (Pi2 B+):
| + 3.3v | 1 | 2 | + 5v |
| I2C SDA / GPIO 2 | 3 | 4 | + 5v |
| I2C SCL / GPIO 3 | 5 | 6 | Ground |
| Clock / GPIO 4 | 7 | 8 | TX / GPIO 14 |
| -- | 9 | 10 | RX / GPIO 15 |
| GPIO 17 | 11 | 12 | GPIO 18 |
| GPIO 27 | 13 | 14 | -- |
| GPIO 22 | 15 | 16 | GPIO 23 |
| + 3.3V | 17 | 18 | GPIO 24 |
| SPI MOSI / GPIO 10 | 19 | 20 | -- |
| SPI MISO / GPIO 9 | 21 | 22 | GPIO 25 |
| SPI SCLK / GPIO 11 | 23 | 24 | SPI CE0 / GPIO 8 |
| -- | 25 | 26 | SPI CE1 / GPIO 7 |
| Model A+ and Model B+ additional pins | |||
| ID_SD | 27 | 28 | ID_SC |
| GPIO 5 | 29 | 30 | -- |
| GPIO 6 | 31 | 32 | GPIO 12 |
| GPIO 13 | 33 | 34 | -- |
| GPIO 19 | 35 | 36 | GPIO 16 |
| GPIO 26 | 37 | 38 | GPIO 20 |
| -- | 39 | 40 | GPIO 21 |
If you want to map to gpio number directly, see Gpio.init(options)
---
##### open(mode[, state])
Open a pin for input or output. Valid modes are:
- Gpio.INPUT: pin is input (read-only).
- Gpio.OUTPUT: pin is output (read-write).
For output pins, the second parameter defines the initial value of the pin, rather than having to issue a separate .write() call. This can be critical for devices which must have a stable value, rather than relying on the initial floating value when a pin is enabled for output but hasn't yet been configured with a value.
For input pins, the second parameter can be used to configure the internal pullup or pulldown resistors state, see mode:0|1 state:0|1 for more details.
##### close()
Unexports a GPIO from userspace and release all resources.
##### read()
Set a velue to a GPIO.
##### write(value)
Set a velue to a GPIO.
##### toggle()
Change GPIO value.
##### sleep(ms[,async])
Sleep for a few milliseconds. If async is true, it will return a promise.
##### createReadStream(pin[, options])
Experimental. See the following example:
log input value to stdout
`js
const Gpio = require('../lib/index.js').Gpio;
var gs = Gpio.createReadStream(32, {throttle: 100});
gs.pipe(process.stdout);
process.on("SIGINT", function(){
gs.end();
process.exit(0);
});
`
##### createWriteStream(pin[, options])
Experimental. See the following example:
read from stdin and set value
`js
const Gpio = require('../lib/index.js').Gpio;
var gs = Gpio.createWriteStream(40, {
mode: Gpio.OUTPUT,
state: Gpio.HIGH
});
console.log('Please input value of P40.');
process.stdin.pipe(gs);
process.on("SIGINT", function(){
gs.end();
process.exit(0);
});
`
trace input/output pins
`js
const Gpio = require('../lib/index.js').Gpio;
var input = Gpio.createReadStream(32, {throttle: 100});
var output = Gpio.createWriteStream(40);
input.pipe(output);
process.on("SIGINT", function(){
input.end();
output.end();
process.exit(0);
});
`
##### group(pins[, activeLow])
Create GpioGroup instance. See [GpioGroup(pins[, activeLow]) - Constructor](#gpiogrouppins-activelow).
---
##### value:0|1
Get or set a velue to a GPIO synchronously.
##### mode:0|1
The pin direction, pass either Gpio.INPUT for read mode or Gpio.OUTPUT for write mode.
##### state:0|1
The pin state. For input pins, state can be either Gpio.POLL\_HIGHT or Gpio.POLL\_LOW. For output pins, it is equivalent to value that state can be either Gpio.HIGH or Gpio.LOW.
##### activeLow: boolean
Specifies whether the values read from or written to the GPIO should be inverted. The interrupt generating edge for the GPIO also follow this this setting. The valid values for activeLow are true and false. Setting activeLow to true inverts. The default value is false.
---
##### Gpio.init([options])
Initialise the bcm2835 library. This will be called automatically by .open()
using the default option values if not called explicitly. The default values
are:
`js`
var options = {
gpiomem: true, / Use /dev/gpiomem /
mapping: 'physical', / Use the P1-P40 numbering scheme /
}
##### gpiomem
There are two device nodes for GPIO access. The default is /dev/gpiomemgpio
which, when configured with group access, allows users in that group to
read/write directly to that device. This removes the need to run as root, but
is limited to GPIO functions.
For non-GPIO functions (i²c, PWM, SPI) the /dev/mem device is required for.init()
full access to the Broadcom peripheral address range and the program needs to
be executed as the root user (e.g. via sudo). If you do not explicitly call when using those functions, the library will do it for you withgpiomem: false.
You may also need to use gpiomem: false if you are running on an older Linuxgpiomem
kernel which does not support the module.
rpio will throw an exception if you try to use one of the non-GPIO functions
after already opening with /dev/gpiomem, as well as checking to see if you
have the necessary permissions.
Valid options:
* true: use /dev/gpiomem for non-root but GPIO-only accessfalse
* : use /dev/mem for full access but requires root
##### mapping
There are two naming schemes when referring to GPIO pins:
* By their physical header location: Pins 1 to 26 (A/B) or Pins 1 to 40 (A+/B+)
* Using the Broadcom hardware map: GPIO 0-25 (B rev1), GPIO 2-27 (A/B rev2, A+/B+)
Confusingly however, the Broadcom GPIO map changes between revisions, so for
example P3 maps to GPIO0 on Model B Revision 1 models, but maps to GPIO2 on all
later models.
This means the only sane default mapping is the physical layout, so that the
same code will work on all models regardless of the underlying GPIO mapping.
If you prefer to use the Broadcom GPIO scheme for whatever reason (e.g. to use
the P5 header pins on the Raspberry Pi 1 revision 2.0 model which aren't
currently mapped to the physical layout), you can set mapping to gpio to
switch to the GPIOxx naming.
Valid options:
* gpio: use the Broadcom GPIOxx namingphysical
* : use the physical P01-P40 header layout
##### Gpio.sleep(ms[,async])
Sleep for a few milliseconds. If async is true, it will return a promise.
---
If a pin is in direction of Gpio.DIR_IN. Three type of events can be fired when needed.
##### event:rising
When register listener to rising event the rising interrupt edges should be configured implictly and the GPIO will trigger the rising event.
`js`
gpio.on('rising', function(){
console.log('A rising signal detected!');
});
##### event:falling
When register listener to falling event the falling interrupt edges should be configured implictly and the GPIO will trigger the falling event.
##### event:change
When register listener to change event the both(rising and falling) interrupt edges should be configured implictly and the GPIO will trigger the change event(on both rising and falling edges).
##### Note:
Registering events to rising and falling will implictly change the interrupt edges as well as unregistering events:
`js`
let gpio = new Gpio(40);
gpio.open(Gpio.INPUT);
assertEqual(gpio.edge, Gpio.POLL_NONE);
gpio.on('rising', function(){...});
assertEqual(gpio.edge, Gpio.POLL_HIGH);
gpio.on('falling', function(){...});
assertEqual(gpio.edge, Gpio.POLL_BOTH);
gpio.removeListener('rising');
assertEqual(gpio.edge, Gpio.POLL_LOW);
gpio.removeListener('falling');
assertEqual(gpio.edge, Gpio.POLL_NONE);
---
##### GpioGroup(pins, activeLow)
Experimental. See the following example:
`js
var GpioGroup = require('./lib/index.js').GpioGroup;
var pins = [32, 40, 33]; //RGB
var group = new GpioGroup(pins, true);
group.open(GpioGroup.OUTPUT);
var color = {
yellow: 0b110,
red: 0b100,
green: 0b010
};
function turn(color){
return new Promise(function(resolve, reject) {
group.value = color;
resolve();
})
}
function wait(time){
return new Promise(function(resolve, reject) {
setTimeout(resolve,time);
})
}
void function (){
turn(color.green)
.then(wait.bind(null, 5000))
.then(turn.bind(null, color.yellow))
.then(wait.bind(null, 2000))
.then(turn.bind(null, color.red))
.then(wait.bind(null, 5000))
.then(arguments.callee)
}();
process.on("SIGINT", function(){
group.close();
console.log('shutdown!');
process.exit(0);
});
``
GPL