Node-RED is an open-source flow-based programming tool designed for wiring together hardware devices, APIs, and online services. It provides a visual programming interface that allows users to create and deploy event-driven applications easily. In Linux, Node-RED is a popular choice for IoT (Internet of Things) projects, automation, and rapid prototyping.
With Node-RED, users can create flows by connecting pre-built nodes together using a web-based editor. Each node represents a specific functionality or service, and flows represent the connections and data transformations between them. The visual nature of Node-RED makes it accessible to both experienced developers and those new to programming.
Node-RED offers a vast ecosystem of contributed nodes that extend its functionality, making it highly customizable and adaptable to various use cases. These nodes enable integration with databases, cloud services, hardware devices, messaging protocols, and more.
Running on Linux, Node-RED can be easily installed using package managers like apt or npm, and it runs on a wide range of Linux distributions. It provides a flexible and extensible platform for building IoT applications, automation workflows, and data processing pipelines in Linux environments.
Author: Pradeep Nandigama, Software Developer, OKdo
Open a terminal and type the command below, enter password for sudo, if asked:
sudo apt update
The command “sudo apt update” is used in Linux systems, particularly those using Debian-based distributions like Ubuntu, to update the package lists or repositories.
Here’s a breakdown of the command:
The process of running “sudo apt update” involves the following steps:
After running “sudo apt update,” you will see the output showing the progress of the update process, including the repositories being accessed and the packages being refreshed. Once completed, your package management system will be up to date and ready for package installation or upgrade using commands like “sudo apt install” or “sudo apt upgrade.”
It is a good practice to run “sudo apt update” regularly to keep your system updated with the latest software packages, security patches, and bug fixes provided by the distribution’s package maintainers.
Open a terminal and type the command below, enter password for sudo, if asked:
sudo apt install npm -y
The command “sudo apt install npm -y” is used in Linux systems, particularly those using Debian-based distributions like Ubuntu, to install the npm (Node Package Manager) tool.
Here’s a breakdown of the command:
When you run the command “sudo apt install npm -y”, it installs the npm tool on your system. The package manager retrieves the necessary files from the software repositories, resolves dependencies, and handles the installation process.
After the installation is complete, you can use npm to install and manage various Node.js packages and libraries, which are essential for developing JavaScript-based applications, server-side scripts, or building tools and utilities.
By including the “-y” flag, the command ensures that the installation proceeds without requiring manual confirmation for each step, allowing for automated installation scripts or unattended installations.
Installing npm using the “sudo apt install npm -y” command is a common approach on Debian-based Linux systems, as it provides a straightforward way to set up npm and start working with Node.js packages.
Enter the command as shown below:
sudo npm install -g --unsafe-perm node-red
The command “npm install -g –unsafe-perm node-red ” is used to install Node-RED, which is a flow-based programming tool and runtime for wiring together hardware devices, APIs, and online services.
Let’s break down the command ‘sudo npm install -g –unsafe-perm node-red’:
When you run the command ‘sudo npm install -g –unsafe-perm node-red’, it installs the Node-RED package globally on your system. The npm tool retrieves the necessary files from the npm registry, resolves dependencies, and handles the installation process.
Installing Node-RED globally allows you to run the ‘node-red’ command from any directory in the terminal, making it easier to access and use Node-RED for creating flows and building applications.
Note: The use of the –unsafe-perm flag is sometimes necessary when installing global npm packages with sudo on certain systems, as it ensures that the installation process doesn’t encounter permission-related errors. However, it is recommended to exercise caution and only use –unsafe-perm if needed, as it bypasses some security measures.
Open terminal window and type the following command:
node-red
Open chromium browser and type
http://127.0.0.1:1880/
Node red provides a visual programming environment for creating and managing flows, connecting nodes, and configuring their behavior.
Here are some key aspects of the Node-RED frontend:
The Node-RED frontend provides an intuitive and visual interface for creating flows, configuring nodes, and managing the behavior of applications. Its user-friendly design makes it accessible to both beginners and experienced developers, enabling rapid prototyping, automation, and integration of various services and devices
Please follow the instruction in the guide named “AN-00001_MRRA_INSTALLATION_IN_ROCK_4SE_C+_V1_18042023.docx”
Use ‘groupadd’ command to create a group called ‘gpio’
Use ‘groupadd’ command to create a group called ‘gpio’
sudo nano /etc/udev/rules.d/10-gpio-permissions.rules
SUBSYSTEM=="gpio", PROGRAM="/bin/sh -c '
chown -R root:gpio /sys/class/gpio && chmod -R 770 /sys/class/gpio;
chown -R root:gpio /sys/devices/platform/soc/*.gpio/gpio && chmod -R 770 /sys/devices/platform/soc/*.gpio/gpio;
chown -R root:gpio /sys$devpath && chmod -R 770 /sys$devpath
'"
Press ‘ctrl+o’ and ‘ctrl+x’ to save.
sudo service udev restart
sudo usermod -aG gpio <username>
Replace <username> with the actual username of the user running the script. In ROCK boards, it will be either ‘rock’ or ‘radxa’ by default.
Log out and log back in to apply the group membership changes.
After following these steps, you should be able to run the Python code that uses libmraa without requiring sudo privileges. The user running the script will have the necessary permissions to access the GPIO pins.
ROCK 4 series has a 40-pin expansion header. Each pin is distinguished by color. For ROCK 4C+, the following pin-out is applicable for version 1.2 and later.
Notes about 40-pin Header
SPI
UART
For I2C-2 and I2C-7
IO Voltage
RK3399 have three IO voltages, 1.8V/3.0V/3.3V. For ROCK Pi 4, below is the default voltage:
Connect a LED to pin 16 GPIO to an LED via a 1k resistor as show in the image.
Create a new directory to implement modules to support ROCK gpio.
sudo mkdir /usr/local/lib/node_modules/node-red/node_modules/ node-red-contrib-rock-4c-gpio
In this directory, 3 files are created.
Here’s a brief explanation of the roles of rock-4c-gpio.js, rock-4c-gpio.html, and package.json:
In summary, rock-4c-gpio.js provides the backend logic for GPIO pin control, rock-4c-gpio.html provides the frontend interface for user interaction, and package.json manages the project’s dependencies and defines various project-related configurations and scripts. Together, these files enable the interaction between the hardware (GPIO pins) and the user interface for GPIO control on the ROCK 4C+ board.
Open a terminal window and enter the following command to create package.json:
sudo nano /usr/local/lib/node_modules/node-red/node_modules/ node-red-contrib-rock-4c-gpio/package.json
Add the following configuration:
{
"name": "node-red-contrib-rock-4c-gpio",
"version": "1.0.0",
"description": "Node-RED custom node for Rock 4C+ GPIO",
"dependencies": {
"mraa": "^2.2.1"
},
"node-red": {
"nodes": {
"rock-4c-gpio": "rock-4c-gpio.js"
}
},
"keywords": [
"node-red",
"gpio",
"rock-4c-gpio"
],
"author": "Your Name",
"license": "MIT"
}
Press ‘ctrl+o’ and ‘ctrl+x’ to save.
The package.json file is an essential part of any Node.js project. It helps in managing dependencies, providing metadata about the package, and defining various configurations and scripts related to the project. It ensures that the necessary dependencies are installed correctly and provides important information for developers and users who interact with your package.
sudo nano /usr/local/lib/node_modules/node-red/node_modules/ node-red-contrib-rock-4c-gpio/rock-4c-gpio.js
module.exports = function (RED) {
function Rock4CGPIONode(config) {
RED.nodes.createNode(this, config);
var node = this;
var spawn = require('child_process').spawn;
// Retrieve configuration from the node's properties
this.gpioPin = config.gpioPin;
this.direction = config.direction;
this.value = config.value;
// Handle incoming messages
this.on('input', function (msg) {
console.log( "input", msg );
var payload = msg.payload;
// Update configuration based on the incoming message
if (payload.hasOwnProperty('gpioPin')) {
node.gpioPin = payload.gpioPin;
}
if (payload.hasOwnProperty('direction')) {
node.direction = payload.direction;
}
if (payload.hasOwnProperty('value')) {
node.value = payload.value;
}
else {
node.value = msg.payload;
}
if (node.direction == 'out') {
mraaGpio = spawn('mraa-gpio', ['set', node.gpioPin.toString(), node.value.toString()]);
}
else {
mraaGpio = spawn('mraa-gpio', ['get', node.gpioPin.toString()]);
}
// Handle command output
mraaGpio.stdout.on('data', (data) => {
// Process the command output as needed
node.send({ payload: data.toString() });
});
// Handle command errors
mraaGpio.stderr.on('data', (data) => {
// Process the command error as needed
node.error(data.toString());
});
// Send the updated configuration as the output message
msg.payload = {
gpioPin: node.gpioPin,
direction: node.direction,
value: node.value
};
node.send(msg);
});
}
RED.nodes.registerType('rock-4c-gpio', Rock4CGPIONode);
};
Let’s explain the key parts of the code:
In summary, this code defines a custom Node-RED node for Rock 4C+ GPIO control. It handles incoming messages, updates the GPIO pin configuration, controls the pin direction and value using the mraa-gpio command, and sends updated configurations and command outputs as messages to the next nodes in the flow.
sudo nano /usr/local/lib/node_modules/node-red/node_modules/ node-red-contrib-rock-4c-gpio/rock-4c-gpio.html
<script type="text/javascript">
RED.nodes.registerType('rock-4c-gpio', {
category: 'function',
color: '#a6bbcf',
defaults: {
name: { value: "" },
gpioPin: { value: "0", required: true },
direction: { value: "out", required: true },
value: { value: "0" }
},
inputs: 1,
outputs: 1,
icon: "node-red-node-rpi-gpio.png",
label: function () {
return this.name || "rock-4c-gpio";
},
oneditprepare: function () {
var node = this;
// Get the GPIO pin dropdown element
var gpioPinDropdown = document.getElementById("node-input-gpioPin");
// Populate the GPIO pin dropdown with options
for (var i = 0; i < 40; i++) {
var option = document.createElement("option");
option.text = i.toString();
option.value = i.toString();
gpioPinDropdown.appendChild(option);
}
// Set the selected value in the GPIO pin dropdown based on the node configuration
gpioPinDropdown.value = node.gpioPin || "0";
// Handle changes in the GPIO pin dropdown
gpioPinDropdown.addEventListener("change", function () {
node.changed = true;
});
// Get the direction dropdown element
var directionDropdown = document.getElementById("node-input-direction");
// Set the selected value in the direction dropdown based on the node configuration
directionDropdown.value = node.direction || "out";
// Handle changes in the direction dropdown
directionDropdown.addEventListener("change", function () {
node.changed = true;
});
// Get the value input element
var valueInput = document.getElementById("node-input-value");
// Set the value input based on the node configuration
valueInput.value = node.value || "0";
// Handle changes in the value input
valueInput.addEventListener("change", function () {
node.changed = true;
});
},
oneditsave: function () {
var node = this;
// Update node configuration properties if changed
if (node.changed) {
node.changed = false;
node.gpioPin = document.getElementById("node-input-gpioPin").value;
node.direction = document.getElementById("node-input-direction").value;
node.value = document.getElementById("node-input-value").value;
}
}
});
</script>
<script type="text/html" data-template-name="rock-4c-gpio">
<div class="form-row">
<label for="node-input-gpioPin"><i class="fa fa-map-pin"></i> GPIO Pin</label>
<select id="node-input-gpioPin">
<!-- GPIO pin options are dynamically populated via JavaScript -->
</select>
</div>
<div class="form-row">
<label for="node-input-direction"><i class="fa fa-long-arrow-up"></i> Direction</label>
<select id="node-input-direction">
<option value="out">Output</option>
<option value="in">Input</option>
</select>
</div>
<div class="form-row">
<label for="node-input-value"><i class="fa fa-toggle-on"></i> Value</label>
<input type="text" id="node-input-value">
</div>
</script>
Let’s break it down section by section:
In summary, the code defines a custom Node-RED node called “rock-4c-gpio”. It specifies its appearance, default properties, and behavior within the Node-RED editor. The oneditprepare function sets up the HTML elements and event listeners for the node’s configuration options. The oneditsave function updates the node’s configuration based on the user’s input. The HTML template defines the structure of the node properties panel, including input fields for GPIO pin, direction, and value.
Relaunch node-red as shown in steps 4, 5 and 8
The “rock-4c-gpio” function is available at the bottom
The flow uses the following functions:
Double click on the first inject function and set payload to 1 and name to ‘on’
Click on done.
Double click on the first inject function and set payload to 0 and name to ‘off’
Click on done.
Double click on the rock-4c-gpio function and set pin to the one that need to be controlled the direction to ‘Input’ or ‘Output’. In this case, pin 16 and Output are selected.
Click on done.
After any change the flow needs to be deployed. Click on the deploy on the top right corner.
A blue circle is shown on the component that is modified and needs deployment and the deploy button on the top right turns red.
Click on that and the flow is deployed.
Click on ‘ON’ and ‘OFF’ functions and the LED connected on PIN 16 should toggle. The flow works for read operations as well when the direction is set to ‘Input’.
If you have followed the steps above, you should have successfully installed, executed and controlled the GPIO pins from a node-red flow.
What’s your challenge? From augmented reality to machine learning and automation, send us your questions, problems or ideas… We have the solution to help you design the world. Get in touch today.
Von einem schnellen Antippen bis zum Zerschlagen des Liebesknopfs und zeigen Sie, wie sehr Ihnen dieses Projekt gefallen hat.