GithubHelp home page GithubHelp logo

raspi-gpio's Introduction

Raspi GPIO

Gitter

Raspi GPIO is part of the Raspi.js suite of libraries that provides access to the hardware GPIO pins on a Raspberry Pi.

If you have a bug report, feature request, or wish to contribute code, please be sure to check out the Raspi IO Contributing Guide.

System Requirements

  • Raspberry Pi Model B Rev 1 or newer (sorry Model A users)
  • Raspbian Jessie or newer
  • Node 6.4.0 or newer

Detailed instructions for getting a Raspberry Pi ready for NodeBots, including how to install Node.js, can be found in the wiki

Installation

First, be sure that you have installed Raspi.js.

Install with npm:

npm install raspi-gpio

Note: this project is written in TypeScript and includes type definitions in the package.json file. This means that if you want to use it from TypeScript, you don't need to install a separate @types module.

Example Usage

In TypeScript/ES6:

import { init } from 'raspi';
import { DigitalInput, DigitalOutput, PULL_UP } from 'raspi-gpio';

init(() => {
  const input = new DigitalInput({
    pin: 'P1-3',
    pullResistor: PULL_UP
  });

  const output = new DigitalOutput('P1-5');

  output.write(input.read());
});

In JavaScript:

const raspi = require('raspi');
const gpio = require('raspi-gpio');

raspi.init(() => {
  const input = new gpio.DigitalInput({
    pin: 'P1-3',
    pullResistor: gpio.PULL_UP
  });

  const output = new gpio.DigitalOutput('P1-5');

  output.write(input.read());
});

Pin Naming

The pins on the Raspberry Pi are a little complicated. There are multiple headers on some Raspberry Pis with extra pins, and the pin numbers are not consistent between Raspberry Pi board versions.

To help make it easier, you can specify pins in three ways. The first is to specify the pin by function, e.g. 'GPIO18'. The second way is to specify by pin number, which is specified in the form "P[header]-[pin]", e.g. 'P1-7'. The final way is specify the Wiring Pi virtual pin number, e.g. 7. If you specify a number instead of a string, it is assumed to be a Wiring Pi number.

Be sure to read the full list of pins on the supported models of the Raspberry Pi.

API

Module Constants

Constant Description
LOW A logic low value, one of the two possible return values from digital reads and arguments to digital writes
HIGH A logic high value, one of the two possible return values from digital reads and arguments to digital writes
PULL_NONE Do not use a pull up or pull down resistor with a pin, one of the three possible values for the pullResistor in the pin configuration object.
PULL_DOWN Use the internal pull down resistor for a pin, one of the three possible values for the pullResistor in the pin configuration object.
PULL_UP Use the internal pull up resistor for a pin, one of the three possible values for the pullResistor in the pin configuration object.
module An easily consumable object for indirectly passing this module around. Intended specifically for use by Core IO (details coming soon)
Property Type Description
PULL_NONE Constant Alias of PULL_NONE module export.
PULL_DOWN Constant Alias of PULL_DOWN module export.
PULL_UP Constant Alias of PULL_UP module export.
createDigitaInput(config) String | Number | Object See the DigitalInput constructor for details
createDigitaOutput(config) String | Number | Object See the DigitalOutput constructor for details

new DigitalInput(config)

Instantiates a new GPIO input instance.

Arguments:

Argument Type Description
config Number | String | Object The configuration for the GPIO pin. If the config is a number or string, it is assumed to be the pin number for the peripheral. If it is an object, the following properties are supported:
Property Type Description
pin Number | String The pin number or descriptor for the peripheral
pullResistor (optional) PULL_NONE | PULL_DOWN | PULL_UP Which internal pull resistor to enable, if any. Defaults to PULL_NONE

DigitalInput Instance Properties

value (read-only)

The current value on the pin.

Type: LOW | HIGH

DigitalInput Instance Methods

read()

Reads the current value on the pin.

Arguments: None

Returns: LOW or HIGH

DigitalInput Instance Events

on('change', function(value))

Fired whenever the value of the GPIO pin changes.

Callback Arguments:

Argument Type Description
value LOW | HIGH The current value of the GPIO pin.

new DigitalOutput(config)

Instantiates a new GPIO output instance.

Arguments:

Argument Type Description
config Number | String | Object The configuration for the GPIO pin. If the config is a number or string, it is assumed to be the pin number for the peripheral. If it is an object, the following properties are supported:
Property Type Description
pin Number | String The pin number for the peripheral
pullResistor PULL_NONE | PULL_DOWN | PULL_UP Which internal pull resistor to enable, if any. Defaults to PULL_NONE

DigitalOutput Instance Properties

value (read-only)

The current value on the pin, equal to the previous written value.

Type: LOW | HIGH

DigitalOutput Instance Methods

write(value)

Writes the given value to the pin.

Arguments:

Argument Type Description
value LOW | HIGH The value to write to the pin

Returns: None

DigitalOutput Instance Events

on('change', function(value))

Fired whenever the value of the GPIO pin changes.

Callback Arguments:

Argument Type Description
value LOW | HIGH The current value of the GPIO pin.

License

The MIT License (MIT)

Copyright (c) 2014-2017 Bryan Hughes [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.

raspi-gpio's People

Contributors

nebrius avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

raspi-gpio's Issues

Some pins are inaccesible by pinNumber

var ledGreen  = new gpio.DigitalOutput('P1-11')    // this works!
var ledGreen  = new gpio.DigitalOutput(17)         // this works!

var ledGreen  = new gpio.DigitalOutput('P1-13')    // this works!
var ledGreen  = new gpio.DigitalOutput(27)         // this doesnt X_X
ledGreen.write(gpio.HIGH)

It doesn't throw any errors, but the led doesn't turn on using pinNumber 27

I've notices some pins work, others don't... that's unusual. Any ideas why?

PD: testing on Raspberry Pi B+

trouble installing.

Hi Bryan, I've been trying to install raspi-io along with johnny-five. When I run

sudo npm install -g raspi-io 

it works away for a while and then fails at the raspi-gpio installation step. Running

sudo npm install -g raspi-gpio 

also fails.
Here's the terminal output. Can you offer any advice? This is on a fresh installation of raspbian, along with node version 0.10.28

# npm install -g raspi-gpio
npm http GET https://registry.npmjs.org/raspi-gpio
npm http 304 https://registry.npmjs.org/raspi-gpio
npm http GET https://registry.npmjs.org/nan
npm http GET https://registry.npmjs.org/raspi-peripheral
npm http 304 https://registry.npmjs.org/raspi-peripheral
npm http 304 https://registry.npmjs.org/nan
npm http GET https://registry.npmjs.org/raspi-board
npm http 304 https://registry.npmjs.org/raspi-board

> [email protected] install /usr/local/lib/node_modules/raspi-gpio
> node-gyp rebuild

gyp WARN EACCES user "root" does not have permission to access the dev dir "/root/.node-gyp/0.10.28"
gyp WARN EACCES attempting to reinstall using temporary dev dir "/usr/local/lib/node_modules/raspi-gpio/.node-gyp"
gyp http GET http://nodejs.org/dist/v0.10.28/node-v0.10.28.tar.gz
gyp http 200 http://nodejs.org/dist/v0.10.28/node-v0.10.28.tar.gz
gyp http GET http://nodejs.org/dist/v0.10.28/SHASUMS.txt
gyp http 200 http://nodejs.org/dist/v0.10.28/SHASUMS.txt
make: Entering directory '/usr/local/lib/node_modules/raspi-gpio/build'
  CXX(target) Release/obj.target/addon/src/addon.o
../src/addon.cc: In function ‘void InitAll(v8::Handle<v8::Object>)’:
../src/addon.cc:40:35: error: no matching function for call to ‘NanNew(<unresolved overloaded function type>)’
../src/addon.cc:40:35: note: candidates are:
../node_modules/nan/nan_implementation_pre_12_inl.h:210:1: note: template<class T> v8::Local<T> NanNew(v8::Handle<T>)
../node_modules/nan/nan_implementation_pre_12_inl.h:216:1: note: template<class T> v8::Local<T> NanNew(const v8::Persistent<S>&)
../node_modules/nan/nan_new.h:177:1: note: template<class T> typename NanIntern::Factory::return_t NanNew()
../node_modules/nan/nan_new.h:183:1: note: template<class T, class A0> typename NanIntern::Factory<T>::return_t NanNew(A0)
../node_modules/nan/nan_new.h:189:1: note: template<class T, class A0, class A1> typename NanIntern::Factory<T>::return_t NanNew(A0, A1)
../node_modules/nan/nan_new.h:195:1: note: template<class T, class A0, class A1, class A2> typename NanIntern::Factory<T>::return_t NanNew(A0, A1, A2)
../node_modules/nan/nan_new.h:201:1: note: template<class T, class A0, class A1, class A2, class A3> typename NanIntern::Factory<T>::return_t NanNew(A0, A1, A2, A3)
../src/addon.cc:42:34: error: no matching function for call to ‘NanNew(<unresolved overloaded function type>)’
../src/addon.cc:42:34: note: candidates are:
../node_modules/nan/nan_implementation_pre_12_inl.h:210:1: note: template<class T> v8::Local<T> NanNew(v8::Handle<T>)
../node_modules/nan/nan_implementation_pre_12_inl.h:216:1: note: template<class T> v8::Local<T> NanNew(const v8::Persistent<S>&)
../node_modules/nan/nan_new.h:177:1: note: template<class T> typename NanIntern::Factory::return_t NanNew()
../node_modules/nan/nan_new.h:183:1: note: template<class T, class A0> typename NanIntern::Factory<T>::return_t NanNew(A0)
../node_modules/nan/nan_new.h:189:1: note: template<class T, class A0, class A1> typename NanIntern::Factory<T>::return_t NanNew(A0, A1)
../node_modules/nan/nan_new.h:195:1: note: template<class T, class A0, class A1, class A2> typename NanIntern::Factory<T>::return_t NanNew(A0, A1, A2)
../node_modules/nan/nan_new.h:201:1: note: template<class T, class A0, class A1, class A2, class A3> typename NanIntern::Factory<T>::return_t NanNew(A0, A1, A2, A3)
addon.target.mk:85: recipe for target 'Release/obj.target/addon/src/addon.o' failed
make: *** [Release/obj.target/addon/src/addon.o] Error 1
make: Leaving directory '/usr/local/lib/node_modules/raspi-gpio/build'
gyp ERR! build error 
gyp ERR! stack Error: `make` failed with exit code: 2
gyp ERR! stack     at ChildProcess.onExit (/usr/local/lib/node_modules/npm/node_modules/node-gyp/lib/build.js:267:23)
gyp ERR! stack     at ChildProcess.EventEmitter.emit (events.js:98:17)
gyp ERR! stack     at Process.ChildProcess._handle.onexit (child_process.js:807:12)
gyp ERR! System Linux 3.12.35+
gyp ERR! command "node" "/usr/local/lib/node_modules/npm/node_modules/node-gyp/bin/node-gyp.js" "rebuild"
gyp ERR! cwd /usr/local/lib/node_modules/raspi-gpio
gyp ERR! node -v v0.10.28
gyp ERR! node-gyp -v v0.13.0
gyp ERR! not ok 
npm ERR! [email protected] install: `node-gyp rebuild`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the [email protected] install script.
npm ERR! This is most likely a problem with the raspi-gpio package,
npm ERR! not with npm itself.
npm ERR! Tell the author that this fails on your system:
npm ERR!     node-gyp rebuild
npm ERR! You can get their info via:
npm ERR!     npm owner ls raspi-gpio
npm ERR! There is likely additional logging output above.

npm ERR! System Linux 3.12.35+
npm ERR! command "/usr/local/bin/node" "/usr/local/bin/npm" "install" "-g" "raspi-gpio"
npm ERR! cwd /
npm ERR! node -v v0.10.28
npm ERR! npm -v 1.4.9
npm ERR! code ELIFECYCLE
npm ERR! 
npm ERR! Additional logging details can be found in:
npm ERR!     /npm-debug.log
npm ERR! not ok code 0

Many thanks, Andy

Platform ARM - unable to install the library on other platform

Hey @nebrius! Thanks for the library, it works great on RPI.

I actually want to start a discussion about whether it is a good idea to specify a platform in package.json in this library.

Basically, it disallows to install this library on any other platform - mac, linux, etc. As I understand why - it makes it a little bit difficult to work with because:

  1. You can't run your app locally before deploying to raspberry PI, this can be an issue if the library is only a small part of the whole app.

  2. You can't build your app bundle on a mac/linux and ship it to raspberry pi where you only run npm rebuild as a part of the deployment process.

npm install and bundling the app with webpack can be very, very slow on RPI, for instance I have to wait around ~10mins for a complete deployment. Combing 1) and 2) you can imagine how frustrating the process can be...

As a solution, I would suggest to remove the arm platform from package.json and add a platform check in the runtime of the library that would show a proper warning if not run on ARM. What do you think?

I might find some free time to implement it and create a PR.

Reading inital pin state switches it to high

Hello,

I'm trying to make a project crash proof if node fails. When the project starts I want to read the state of pins to see if they were set low, however the default init script I've found online defaults all the pins being read to HIGH. Is there a bug somewheres?

Reading on app start. This sets all the pins to HIGH even if one of them is LOW.

for(var i = 0; i < 4; i++)
{

  var input = new gpio.DigitalInput({
    pin : 'P1-' + pins[i]
  });
  //_state[i] =
  console.log(TAG + input.read());

}

This works great but I can't

  for(var i = 0; i < 4; i++)
  {
  
    var output = new gpio.DigitalOutput('P1-' + pins[i]);
  
    if(state[i])
      output.write(gpio.HIGH);
    else 
      output.write(gpio.LOW);
  
  }

pins stay open indefinitely after instantiation

Example which illustrates the problem:

const {DigitalInput} = require('raspi-gpio');
const input = new DigitalInput('GPIO4');
setInterval(() => {
  require('fs').writeSync(1, '.');
}, 1000).unref();

The following code simply exits:

const {DigitalInput} = require('raspi-gpio');
// const input = new DigitalInput('GPIO4');
setInterval(() => {
  require('fs').writeSync(1, '.');
}, 1000).unref();

I'm not sure when this started happening, but tested it with Node 6 and Node 8 already.

The issue is due to pigpio's enableInterrupt() called without a corresponding disableInterrupt(). This likely goes for DigitalOutput as well.

Suggested solutions:

  1. Document that users need to call input.input.disableInterrupt() once they are finished with the DigitalInput object.
  2. Provide a destroy() method which calls the above method for a user, and document that instead. 😄
  3. Don't call enableInterrupt() in the constructor, and instead change the API to something like:
    const input = new DigitalInput('GPIO4');
    const offChange = input.onChange(myChangeHandler);
    // some time later
    offChange();
    Where DigitalInput.prototype.onChange would call enableInterrupt(), and return a function (offChange) which would call disableInterrupt().
  4. Leverage some sort of RxJS-like lib with Observables to do number 3 instead (this is a pretty strong use case for Observables!)

I'm happy to help with a PR, but would appreciate some direction. Until then, I can work around the issue using the method in solution 1.

Compilation terminated

Trying to install johnny-five and raspi-io on this hypriot image. npm install fails, looking at the output I see the following:

Entering directory user/src/app/node_modules/raspi-gpio/build
CXX(target) Release/obj.target/addon/src/addon.o
CXX(target) Release/obj.target/addon/src/init.o
../src/init.cc:27:22 fatal error: wiringPi.h: No such file or directory
#include <wiringPi.h>

compilation terminated.
addon.target.mk:91: recipe for target 'Release/obj.target/addon/src/init.o' failed
make: Leaving directory 'usr/src/app/node_modules/raspi-gpio/build'
make: ***[Release/obj.target/addon/src/init.o] Error 1
ERR! build error
ERR! stack Error: `make` failed with exit code: 2
stack at ChildProcess...
ERR! cwd /usr/src/app/node_modules/raspi-gpio
gyp ERR! node -v v5.5.0
gyp ERR! node-gyp -v v3.0.3
ERR! not ok

Deps:

"dependencies": {
    "johnny-five": "^0.9.19",
    "raspi-io": "^5.2.0"
  }

BADPLATFORM

Hi,

I get the following error during instal:

npm ERR! Darwin 15.6.0
npm ERR! argv "/usr/local/bin/node" "/usr/local/bin/npm" "install" "raspi" "raspi-gpio" "--save"
npm ERR! node v6.7.0
npm ERR! npm  v3.10.3
npm ERR! code EBADPLATFORM

npm ERR! notsup Not compatible with your operating system or architecture: [email protected]
npm ERR! notsup Valid OS:    any
npm ERR! notsup Valid Arch:  arm
npm ERR! notsup Actual OS:   darwin
npm ERR! notsup Actual Arch: x64

Environment:

PRETTY_NAME="Raspbian GNU/Linux 9 (stretch)"
NAME="Raspbian GNU/Linux"
VERSION_ID="9"
VERSION="9 (stretch)"
ID=raspbian
ID_LIKE=debian
HOME_URL="http://www.raspbian.org/"
SUPPORT_URL="http://www.raspbian.org/RaspbianForums"
BUG_REPORT_URL="http://www.raspbian.org/RaspbianBugs"

node v6.7.0

npm 3.10.3

Security enhancement

Hi,

I'm using raspi-gpio with node-red and node-red-contrib-gpio. I start node-red as root because wiringPiSetup require privileges. Is there a way to avoid starting node-red as root ? For example, by using sudo

Thank you :)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.