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
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.
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!
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).
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).
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.
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).
Here, the pin PA12 (pin 3) is chosen as the output.
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":[]}]
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.
- Install Home Assistant (HASS) on an Orange Pi running under Armbian
- IoT development based on Orange Pi, Arduino (Firmata), Nodejs, Blynk and Johnny-Five
- Blynk + Node.js + Johnny-Five: drive a Pan-Tilt PTZ SG90 kit on Orange Pi with an Arduino / Firmata
- Start programming with Node Js and Johnny-Five: IoT and robotics based on Arduino, Raspberry Pi, Orange Pi
- Orange Pi: test of the OPI.GPIO package for Node-Red (node-red-contrib-opi-gpio)
- Orange Pi (Armbian): replace the GPIO by an Arduino/Firmata, Node-RED and Johnny-Five