Home | Difficulty level | Advanced | Orange Pi: test of the OPI.GPIO package for Node-Red (node-red-contrib-opi-gpio)

Orange Pi: test of the OPI.GPIO package for Node-Red (node-red-contrib-opi-gpio)

The libraries that support the Orange Pi GPIO are growing smoothly. Today, I suggest you try the package node-red-contrib-opi-gpio for Node-RED developed by gprandst. The developer tested the package on an Orange Pi PC. For this article, I tested the package successfully on an Orange Pi Lite (WiFi, 512MB). Follow this tutorial to install Node-RED on Orange Pi (Armbian).

Installing the package in Node-RED

The installation is Node-RED is quite simple. First, close NodeRED and then start the installation of the package using the npm command. This is preferable because the installation requires the compilation of sources (WiringPi in particular), which does not work very well from the pallet manager.

For the writing of this article, the package did not install completely the 1st time. I had to restart the package installation one second directly from the palette manager.

Then, it is necessary to add a group gpio. The following steps are taken from the npm package documentation (available here). Begin by checking whether a gpio group exists.

Replace pi user with your username
If the group does not exist, you must create it and allow the user ft to access it
Then add a rule to the Kernel
Paste the following rule and save the file with CTRL + X then Y.
Restart the Orange Pi.

Using the Orange Pi GPIO from Node-RED

The package adds 2 tools in the Orange Pi palette:

Output pin: allows to control the outputs
Input pin: inputs, logic!

orange pi opi node-red gpio input output

A small example to conclude

To test the package, a switch will be connected to the GPIO 11 (PA11 in the OPI nomenclature) which will trigger the lighting of a LED connected to the GPIO12 (PA12) via a 220Ω resistor. Pressing the button again will turn off the LED. The state of the led will be stored in a context variable. As a reminder, here is the identification of the pins of Orange Pi (all models except Orange Pi Zero).

orange pi gpio pinout

Node input

Start by adding a node input on the flow. Open the editing window. By default, the Use GPIO pinout box is selected. By unchecking this box, it is possible to specify the spindle manually. We will let the package this time especially as the pins respect the names of OPI. Choose PA11 (Pin 5).

opi gpio orange pi node-red input

To detect the push of a button, gprandst added a handy interrupt management. It detects a support (or some other event) on a rising edge, a falling edge, or both. Here, rising (rising front) will do.

opi gpio orange pi node-red input edge detection

Keeping the led state in a Node-RED context

We will use the Node-RED context to store the state of the led. Each time a button is pressed, the function is called up. The previous state is retrieved with the context.get (‘led’) function. Then, it will be enough to invert (status =! Status) and finally store it for the next time context.set (‘led’, status). Paste this function into a function node

Node output

Now paste to node Output. It is possible to specify its own spindle number (as for input). The pin can also be initialized in a given state (high – high, low – low).

opi gpio orange pi node-red output

Here, the pin PA12 (pin 3) is chosen as the output.

opi gpio orange pi node-red output pin

Flow code

To go faster, here is the flow code you just need to import

opi gpio orange pi node-red flow test

We are still far from being able to take advantage of all the possibilities of the Orange Pi GPIO but it is already an excellent start especially for beginners looking for a solution (yet) more economical or more powerful than the Raspberry Pi.