กลับไปหน้ารวมไฟล์
control-your-raspberry-pi-gpios-from-arduino-cloud-using-node-red-fc5464-en.md

This project shows how to interact with the Raspberry Pi GPIOs from a dashboard created using Arduino Cloud with an application programmed in Node-RED. It can serve as an example to create your own applications that require access to RPI GPIOs and that can be ultimately controlled using a dashboard.

The setup

In this project I have used a Raspberry Pi 5 connected to an LED and a push button, both inserted in a breadboard. This is the diagram

Raspberry Pi GPIO setup diagram

Note: This project should work with any Raspberry Pi version and actually with any Linux-based machine that supports libgpiod. Please, leave your comments if it does not work with your board in order to review it.

How to create your project

The process to use this project is very straighforward:

  1. Create the Device and Thing in the Arduino Cloud and get the Arduino Cloud API Key
  2. Install Node-RED and required nodes
  3. Develop the Node-RED application
  4. Create the Arduino Cloud dashboard
  5. Test everything

1. Create the Device and Thing in the Arduino Cloud

Note: Before getting started, make sure that you have an Arduino Cloud account

Create the Device

Go to the Devices section of the Arduino IoT Cloud and click on + DEVICE.

Select "Any Device" and follow the instructions on the wizard.

Note: Save your Device ID and Secret Key as they will be used in your python code.

Create the Thing

In your recently created device page, go to the Associated Thing section, click on "Create Thing" and rename it.

Note: You can also create the Thing from the Things list and associate it later.

Create the Variables

Add the variables by clicking on the ADD button. At the end of the process, your list of variables should look like this.

Arduino Cloud: Thing variables

Get an Arduino Cloud API key

The process to get an Arduino Cloud API key is very straightforward:

  1. Go to https://cloud.arduino.cc/home/api-keys.
  2. Click on CREATE API KEY and enter a name.
  3. Note down the Client ID and Client Secret or download the PDF. We will use these credentials in the Node-RED node later.

2. Install Node-RED and required nodes

How to install Node-RED

There are many ways to install Node-RED in a Raspberry Pi or any other machine. Typically, you can use: docker, snap, apt, native node.js installation.

The tricky point is that we want our Node-RED instance to have access to the GPIOs and host system. This can be achieved with all the installation methods but for most of them it is quite difficult to achieve and many conflicts and issues need to be solved. So, the easiest and recommended way in this project would be to do a native installation using NPM.

These are the instructions:

  1. Node-RED Installation

Install Node-RED with the following command

sudo npm install -g --unsafe-perm node-red
  1. Manage the service

There are many tools that can be used to manage the service, but I recommend using pm2 to manage the service and make it start on boot.

Install pm2

sudo npm install -g pm2

Edit the pm2.json file and set the full path to your gpio-basic folder

{
	 "apps": [
	   {
	     "name": "node-red",
	     "script": "<FULL_PATH_TO_YOUR FOLDER>/pm2-node-red-start.sh"
	   }
	 ]
}

Launch the service, save it and enable pm2 to start on boot:

sudo pm2 start pm2.json
sudo pm2 save
sudo env PATH=$PATH:/usr/bin $(which pm2) startup systemd

The service will be launched automatically on boot.

You can manually start or stop the service with sudo pm2 start node-red or sudo pm2 stop node-red.

Open Node-RED

Open a browser a introduce the URL: http://<IP_ADDRESS>:1880, replacing <IP_ADDRESS> with the IP Address of your Raspberry Pi.

Your Node-RED environment will be opened. If everything was properly configured, you should see the flow

Node-RED flow

Note: If you can't see the flow and there's just an empty screen, you can either review the configuration (maybe the folder is not set properly) or you could also import the flow.json pasting the content directly using Menu-->Import

Install the required nodes

The first time, you will probably get some messages indicating that there are nodes missing. It is because the application requires the following nodes:

  1. node-red-contrib-libgpiod: The nodes needed to interact with the GPIOs
  2. @arduino/node-red-contrib-arduino-iot-cloud: The nodes to communicate with Arduino Cloud
  3. node-red-contrib-cpu: I have used this node to generate an integer value that is sent to the Cloud. In this case I send the CPU load.

In order to install the nodes, you just have to click on Menu-->Manage Palette on the menu.

Then go to the Install tab, search for the modules listed above and install them.

3. Develop your Node-RED application

If everything worked correctly, you should see the picture shown above.

Now, it's time to configure the nodes.

Configure the Arduino Cloud nodes

First, let's configure the Arduino Cloud nodes. For each node, you have to do the following:

  1. Select the Connection

Note: The first time, you will have to set up the Connection and introduce the API secrets Client ID and Client Secret.

  1. Select the Thing from the dropdown menu
  2. Select the Property (the variable) from the dropdown menu
  3. Check the Send as device box
  4. Select the Device from the dropdown menu
Configure the Arduino Cloud node
Configure de Arduino Cloud connection

As a reference, you can see the full process in action for one node in the animated GIF in the github project.

Configure the GPIO nodes

First, let's configure the GPIO IN node. Double click on the node and set:

  1. Device --> Set your gpiochipX (gpiochip4 in the example)
  2. Type: Digital pin
  3. Pin --> Set your pin number
Digital IN configuration

Next, let's configure the GPIO OUT node. Double click on the node and set:

  1. Device --> Set your gpiochipX (gpiochip4 in the example)
  2. Type: Digital (0/1)
  3. Pin --> Set your pin number
Digital OUT configuration

Deploy

Deploy the flow by clicking on the Deploy button on the top-right corner.

4. Create the Arduino Cloud dashboard

The dashboard that we are going to build will look like this

GPIO Basic Dashboard

There are 2 ways to create the dashboard:

  1. Create it manually. Replicate the one shown above following the instructions in this guide.
    1. The LED widgets should be linked to the variable led
    2. The Value widgets should be linked to the variable test_value
    3. The Button widgets should be linked to the variable button
  2. Clone the one provided by this tutorial following the instructions in this Annex of the github project.

5. Test everything

You should now see the following:

  1. Every time push the physical button, the button widget should be updated
  2. Every time you change the LED widget in the dashboard, the physical LED should switch to ON or OFF
  3. The value of test_value should reflect the Raspberry PI CPU load updated every 5s, and you can see the evolution on the dashboard

Enjoy!

System Interoperability: Arduino Cloud to RasPi

The Arduino IoT Cloud is designed to manage ESP32s and MKR boards natively. Raspberry Pi is a completely different Linux-based ecosystem. The Arduino Cloud Node-RED Bridge project teaches advanced "broker architecture". Instead of forcing the two systems to fight, you build a Node-RED translation layer, allowing a beautiful Arduino Cloud graphical dashboard to instantly actuate the physical GPIO pins of a Linux microcomputer.

The Node-RED Canvas Architecture

You do not write C++ or Python to solve this; you use Visual Flow-Based Programming.

  1. The Raspberry Pi boots up its Linux OS. You install Node-RED (a powerful, IBM-developed server software environment).
  2. You open the Node-RED canvas in your desktop browser. It looks like a giant flowchart.
  3. You install the specialized node-red-contrib-arduino-iot-cloud plugin nodes.
  4. The Translation Flow:
    • You drag the Arduino Property block onto the canvas. You authenticate it with your Arduino Cloud API key. It actively listens to your cloud dashboard dashboard button ("Turn on Pump").
    • You drag standard Raspberry Pi GPIO Out block onto the canvas, mapping it specifically to Physical Pin 12 on the Pi.
    • You drag a physical connection line between the two blocks!

Bypassing the Microcontroller

This project represents the ultimate abstraction of hardware.

  • You have completely eliminated the need for an actual Arduino board!
  • When you tap the button on the Arduino IoT Cloud app while sitting in a coffee shop in Tokyo...
  • The command hits the Arduino Azure servers. The Node-RED software running inside your house in London catches the API webhook instantly. Node-RED translates the payload through your flowchart and executes a low-level Python command natively inside the Raspberry Pi's processor, slamming Pin 12 to 3.3V, firing a relay to start your coffee machine!

Cloud Infrastructure Setup

  • Raspberry Pi (3 or 4) running Raspberry Pi OS.
  • Node-RED Server Package installed on the Pi.
  • Arduino IoT Cloud Account and generated API credentials.
  • Relay Modules or LEDs connected to the Pi's GPIO pins to visualize the outputs.

ข้อมูล Frontmatter ดั้งเดิม

apps:
  - "1x Arduino IoT Cloud"
author: "dbeamonte_arduino"
category: "Internet of Things, BT & Wireless"
components:
  - "1x Push Button"
  - "1x Diode Led Red 5mm"
  - "1x Breadboard 100x160"
  - "1x Raspberry Pi (Any Model)"
description: "This project shows how to interact with the Raspberry Pi GPIOs from a dashboard created using Arduino Cloud with an application programmed in Node-RED. It can serve as an example to create your own applications that require access to RPI GPIOs and that can be ultimately controlled using a dashboard."
difficulty: "Intermediate"
documentationLinks: []
downloadableFiles:
  - "https://github.com/dbduino-prjs/rpi-arduino-cloud/tree/master/node-red/gpio-basic"
encryptedPayload: "U2FsdGVkX18WO4JWKZ6lJpJ1q4QUH8bVteqDg6fXKunG59jqPzv1RO32KCCeYBcyDuo+S29fMzxvfOYnShCxmhQOj/7JK2PkoBTOsberlW0="
heroImage: "https://cdn.jsdelivr.net/gh/bigboxthailand/arduino-assets@main/images/projects/control-your-raspberry-pi-gpios-from-arduino-cloud-using-node-red-fc5464_cover.png"
lang: "en"
likes: 0
passwordHash: "595e5908dc4becf987377717e28842264b82d5b3f0c639121b463fc8eb79c3ca"
price: 79
seoDescription: "Control Raspberry Pi GPIOs from an Arduino Cloud Dashboard using Node-RED. Ideal for IoT projects and remote Raspberry Pi management."
tags:
  - "Internet Of Things"
  - "Remote Control"
  - "Monitoring"
  - "Data Collection"
title: "Control your Raspberry Pi GPIOs from Arduino Cloud using Node-RED"
tools: []
videoLinks: []
views: 1332