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

Table of Contents
drapeau france
level advanced

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.

npm install node-red-contrib-opi-gpio
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
groups pi

If the group does not exist, you must create it and allow the user ft to access it

addgroup gpio
usermod -a -G gpio pi

Then add a rule to the Kernel

sudo nano /etc/udev/rules.d/99-com.rules

Paste the following rule and save the file with CTRL + X then Y.

KERNEL=="gpio*", RUN="/bin/sh -c 'chgrp -R gpio /sys/%p /sys/class/gpio && chmod -R g+w /sys/%p /sys/class/gpio'"

Restart the Orange Pi.

sudo reboot

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

var status = context.get('led') || false;  // Récupère l'état de la Led (false si inexistant) - Get led status (false by default) 
status = !status;                          // Inverse l'état - Reverse status
context.set('led',status);                 // Enregistre l'état - store new led status

msg.payload = status;                      // Prépare la sortie du node - prepare node output
return msg;

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

[{"id":"1188e21.b72161e","type":"opi_out","z":"802c6c20.fd84e","name":"Pin 3 - PA12 (Led)","set":false,"level":"1","opi":true,"pin2":"","pin1":"12","pin":"12","x":728.5,"y":310,"wires":[]},{"id":"2e1640da.19eb4","type":"opi_in","z":"802c6c20.fd84e","name":"Pin 5 - PA11 (Switch button)","opi":true,"enableInterrupt":true,"edge":"rising","debounce":"100","pin2":"","pin1":"11","pin":"11","x":262.5,"y":208,"wires":[["f760119e.40a43"]]},{"id":"f760119e.40a43","type":"function","z":"802c6c20.fd84e","name":"New Status","func":"var status = context.get('led') || false;\nstatus = !status;\nmsg.payload = status;\n\ncontext.set('led',status);\n\nreturn msg;","outputs":1,"noerr":0,"x":508.5,"y":265,"wires":[["1188e21.b72161e","db31a090.17315"]]},{"id":"db31a090.17315","type":"debug","z":"802c6c20.fd84e","name":"","active":true,"console":"false","complete":"false","x":720.5,"y":201,"wires":[]},{"id":"2f572255.f0f61e","type":"comment","z":"802c6c20.fd84e","name":"Orange Pi GPIO.OPI input/output","info":"","x":266.5,"y":146,"wires":[]}]

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.

Click to rate this post!
[Total: 1 Average: 5]

Thanks for your reading

Did you like this project ? Don't miss any more projects by subscribing to our weekly newsletter!

Are you having a problem with this topic?

Maybe someone has already found the solution, visit the forum before asking your question
1 Comment
  1. Hello!

    Thanks for the tutorial.
    I have followed the steps but no luck with node-red accessing the pin. It gives me the Error: EACCES: permission denied, open ‘/sys/class/gpio/gpio20/direction’ every time I start node-red.

    I have checked the group access for the gpio and is the following:
    -rwxrwx— 1 root gpio 4096 Mar 18 16:43 export
    drwxr-xr-x 3 root root 0 Mar 18 16:46 gpio20
    drwxr-xr-x 3 root root 0 Mar 18 16:46 gpiochip0
    drwxr-xr-x 3 root root 0 Mar 18 16:46 gpiochip352
    -rwxrwx— 1 root gpio 4096 Mar 18 16:31 unexport

    Is there anything else I can do?

Leave a Reply

Read more
DIY Projects
DIY Projects
%d bloggers like this: