A node.js-based GPIO library for Raspberry Pi.
This was tested with Raspberry Pi Model B+, but should work with older versions as well.
When instantiating a GPIO, the library requires you to provide the physical pin number as the GPIO number will be coordinated internally.
An example app, 'app.js', is provided.
Install node.js on the Raspberry Pi:
sudo apt-get install nodejs npm
For root access to the pins, this library uses gpio-admin.
Here are the steps to install it, but for additional information, please visit that link.
git clone git://github.com/quick2wire/quick2wire-gpio-admin.git
cd quick2wire-gpio-admin
make
sudo make install
sudo adduser $USER gpio (May not be needed if your using the 'pi' user as it's already a member of that group. Log out and log back in once added.)
Exports the pin (associated GPIO) and optionally sets the direction
and pull
(enables the Pull Up and Pull Down Resistors). TODO : set the edge
pinNumber
: The pin number to export.options
: (Optional) An object that supportsdirection
('in' or 'out') andpull
('up' or 'down'). Direction defaults to 'out' and 'pull' is not set by default.callback
: (Optional) Called when the pin is ready. Following standard node callback structure, the first argument is the error.
Exports the GPIO. This is done automatically during instantiation.
callback
: (Optional) Called when the pin is exported. Following standard node callback structure, the first argument is the error.
Unexports the GPIO.
callback
: (Optional) Called when the pin is unexported. Following standard node callback structure, the first argument is the error.
Changes the direction to in
or out
.
direction
:in
orout
.callback
: (Optional) Called when the pin direction is set. Following standard node callback structure, the first argument is the error.
Gets the direction of the pin.
callback
: (Optional) Called when the pin direction is read. Following standard node callback structure, the first argument is the error.
Reads the current value of the pin.
callback
: (Optional) Called when the pin is read. Following standard node callback structure, the first argument is the error. The second argument, the value, will be the number0
or1
.
Example:
var GPIO = require('./RasPi_GPIO'),
async = require('async'),
pin7;
async.series({
init: function(callback){
pin7 = new GPIO(7, { direction:'in' }, function(err) {
callback(err);
});
},
read: function(callback){
pin7.read(function(err, value) {
callback(err, value);
});
}
}, function(err, result){
console.log(result.read);
});
Writes value
to pinNumber
.
value
: Supports most types of boolean values, for example, 0, 1, 'true', false.callback
: (Optional) Called when the pin value is written. Following standard node callback structure, the first argument is the error.
Example:
var GPIO = require('./RasPi_GPIO'),
async = require('async'),
pin7;
async.series([
function(callback){
pin7 = new GPIO(7, function(err) {
callback(err);
});
},
function(callback){
pin7.write(0, function(err) {
callback(err);
});
}
], function(err){
console.log(err);
});
- The library is maintained using gulp/mocha/instanbul for tests and coverage. To run tests and coverage, with gulp installed, run
gulp
.
- Edge support.
- Add synchronous read/write calls.
- More testing.
(The MIT License)
Copyright (c) 2014 James Booth [email protected]
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.