Gpio javascript raspberry pi

Saved searches

Use saved searches to filter your results more quickly

You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.

Control Raspberry Pi GPIO pins with node.js

License

JamesBarwell/rpi-gpio.js

This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?

Sign In Required

Please sign in to use Codespaces.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching Xcode

If nothing happens, download Xcode and try again.

Launching Visual Studio Code

Your codespace will open once ready.

There was a problem preparing your codespace, please try again.

Latest commit

Git stats

Files

Failed to load latest commit information.

README.md

Control Raspberry Pi GPIO pins with node.js

  • Raspberry Pi 1 Model A
  • Raspberry Pi 1 Model A+
  • Raspberry Pi 1 Model B
  • Raspberry Pi 1 Model B+
  • Raspberry Pi 2 Model B
  • Raspberry Pi 3 Model B
  • Raspberry Pi 4 Model B
  • Raspberry Pi Zero
  • Raspberry Pi Zero W

rpi-gpio 1.x is no longer supported. Please use 2.x unless you need to run with an old version of node.

node version rpi-gpio 1.x rpi-gpio 2.x +
0.10 Yes No
0.12 Yes No
4 Yes Yes
6 Yes Yes
8 Yes Yes
10 No Yes
12 No Yes

This module can then be installed with npm:

Please note that this module has a dependency on epoll and that currently it is only possible to build and develop the module on Linux systems.

If you are having trouble installing this module make sure you are running gcc/g++ -v 4.8 or higher. Here is an installation guide.

If you wish to use this module with Typescript, install the definitions from Definitely Typed:

npm install --save @types/rpi-gpio 

Please note that this is not a Typescript project and the definitions are independently maintained by the community. Thanks to Roaders for providing these.

Before you can read or write, you must use setup() to open a channel, and must specify whether it will be used for input or output. Having done this, you can then read in the state of the channel or write a value to it using read() or write() .

All of the functions relating to the pin state within this module are asynchronous, so where necessary — for example in reading the value of a channel — a callback must be provided. This module inherits the standard EventEmitter, so you may use its functions to listen to events.

Please be aware that there are multiple ways of referring to the pins on the Raspberry Pi. The simplest and default way to use the module is refer to them by physical position, using the diagrams on this page. So holding the Raspberry Pi such that the GPIO header runs down the upper-right side of the board, if you wished to address GPIO4 (which is in column 1 and row 4), you would setup pin 7. If you wish instead to refer to the pins by their GPIO names (known as BCM naming), you can use the setMode command described in the API documentation below.

This module will work without use of the sudo command, as long as the user running the node process belongs to the gpio group. You can check the current user’s groups by running the command groups , or groups for another user. If you are not already a member of the gpio group, you can add yourself or another user by running sudo adduser gpio .

The default API uses the node-style error-first callbacks to perform asynchronous functions. Most of these methods take a callback, and that callback should check for an error in its first argument. It is important to check for an error after each command, else your code will continue to run and will likely fail in hard to understand ways.

setup(channel [, direction, edge], callback)

Sets up a channel for read or write. Must be done before the channel can be used.

  • channel: Reference to the pin in the current mode’s schema.
  • direction: The pin direction, pass either DIR_IN for read mode or DIR_OUT for write mode. You can also pass DIR_LOW or DIR_HIGH to use the write mode and specify an initial state of ‘off’ or ‘on’ respectively. Defaults to DIR_OUT.
  • edge: Interrupt generating GPIO chip setting, pass in EDGE_NONE for no interrupts, EDGE_RISING for interrupts on rising values, EDGE_FALLING for interrupts on falling values or EDGE_BOTH for all interrupts. Defaults to EDGE_NONE.
  • callback: Provides Error as the first argument if an error occurred.

Reads the value of a channel.

  • channel: Reference to the pin in the current mode’s schema.
  • callback: Provides Error as the first argument if an error occured, otherwise the pin value boolean as the second argument.

write(channel, value [, callback])

Writes the value of a channel.

  • channel: Reference to the pin in the current mode’s schema.
  • value: Boolean value to specify whether the channel will turn on or off.
  • callback: Provides Error as the first argument if an error occured.

Sets the channel addressing schema.

  • mode: Specify either Raspberry Pi or SoC/BCM pin schemas, by passing MODE_RPI or MODE_BCM. Defaults to MODE_RPI.

Tears down any previously set up channels. Should be run when your program stops, or needs to reset the state of the pins.

Tears down the module state — used for testing.

See Node EventEmitter for documentation on listening to events.

Emitted when the value of a channel changed

This API exposes a Promises interface to the module. All of the same functions are available, but do not take callbacks and instead return a Promise.

The Promises interface is available in the promise namespace, e.g.:

var gpiop = require('rpi-gpio').promise; gpiop.setup(7, gpiop.DIR_OUT) .then(() =>  return gpiop.write(7, true) >) .catch((err) =>  console.log('Error: ', err.toString()) >)

See the examples directory included in this project.

Please note that all examples are intended to be directly runnable from the code repository, so they always require the module in at the top using var gpio = require(../rpi-gpio) . In reality, you will want to include the module using var gpio = require(‘rpi-gpio’)

Contributions are always appreciated, whether that’s in the form of bug reports, pull requests or helping to diagnose bugs and help other users on the issues page.

Due to the nature of this project it can be quite time-consuming to test against real hardware, so the automated test suite is all the more important. I will not accept any pull requests that cause the build to fail, and probably will not accept any that do not have corresponding test coverage.

You can run the tests with npm:

and create a coverage report with:

There is also an integration test that you can run on Raspberry Pi hardware, having connected two GPIO pins across a resistor. The command to run the test will provide further instructions on how to set up the hardware:

The tests use mochajs as the test framework, and Sinon.JS to stub and mock out file system calls.

About

Control Raspberry Pi GPIO pins with node.js

Источник

Node.js Raspberry Pi — GPIO Introduction

The Raspberry Pi has two rows of GPIO pins, which are connections between the Raspberry Pi, and the real world.

Output pins are like switches that the Raspberry Pi can turn on or off (like turning on/off a LED light). But it can also send a signal to another device.

Input pins are like switches that you can turn on or off from the outside world (like a on/off light switch). But it can also be a data from a sensor, or a signal from another device.

That means that you can interact with the real world, and control devices and electronics using the Raspberry PI and its GPIO pins!

Taking a Closer Look at the GPIO Pins

Raspberry Pi 3 with GPIO

This is an illustration of the Raspberry Pi 3.

The GPIO pins are the small red squares in two rows on the right side of the Raspberry Pi, on the actual Raspberry Pi they are small metal pins.

The Raspberry Pi 3 has 26 GPIO pins, the rest of the pins are power, ground or «other».

The pin placements correspond with the table below.

Raspberry Pi B+, 2, 3 & Zero

3V3 1 2 5V
GPIO 2 3 4 5V
GPIO 3 5 6 GND
GPIO 4 7 8 GPIO 14
GND 9 10 GPIO 15
GPIO 17 11 12 GPIO 18
GPIO 27 13 14 GND
GPIO 22 15 16 GPIO 23
3V3 17 18 GPIO 24
GPIO 10 19 20 GND
GPIO 9 21 22 GPIO 25
GPIO 11 23 24 GPIO 8
GND 25 26 GPIO 7
DNC 27 28 DNC
GPIO 5 29 30 GND
GPIO 6 31 32 GPIO 12
GPIO 13 33 34 GND
GPIO 19 35 36 GPIO 16
GPIO 26 37 38 GPIO 20
GND 39 40 GPIO 21

Legend

Taking a Closer Look at the Breadboard

A breadboard is used for prototyping electronics, it allows you to create circuits without soldering. It is basically a plastic board, with a grid of tie-points (holes). Inside the board there are metal strips connecting the different tie-points in specific ways.

In the illustration below we have highlighted some of the sections with different colors. This is to show you how the grid is connected.

Breadboard with connections highlighted

The different sections of the breadboard:

  • On the left, and right, side there are 2 columns of tie-points. All the tie points in each of these columns are connected.
  • The Power Bus — The columns highlighted with red. There are usually used to connect power to the Breadboard. Since the entire column is connected, you can connect power to any of the tie-points in the column.
  • The Ground Bus — The columns highlighted with blue. There are usually used to connect Ground to the Breadboard. Since the entire column is connected, you can connect ground to any of the tie-points in the column.
  • Rows of connected Tie-Points — The rows highlighted with green. The tie-points of each of these rows are connected, but not the entire row! The left side tie-points are connected (A-B-C-D-E), and the right side tie-points are connected (F-G-H-I-J).
  • In the center of the Breadboard there is a Trench, this separates the left and right rows. The width of the trench is designed so that many Integrated Circuits fit across it.

Install the onoff Module

To interface with the GPIO on the Raspberry Pi using Node.js, we will use a Module called «onoff».

Install the onoff module using npm:

Now onoff should be installed and we can interact with the GPIO of the Raspberry Pi.

Источник

Читайте также:  Отличие пакета io от nio java
Оцените статью