在树莓派上运行

先决条件

如果您使用的是Raspberry Pi操作系统,Bullseye是当前支持的版本。

安装与升级Node-RED

我们提供了一个脚本,用于在树莓派上安装Node.js、npm和Node-RED。当有新版本发布时,该脚本也可用于升级现有安装。

运行以下命令将下载并执行脚本。如果您想先查看脚本内容,可以在Github上查看。

bash <(curl -sL https://raw.githubusercontent.com/node-red/linux-installers/master/deb/update-nodejs-and-nodered)

您可以为脚本传递额外的参数。在上述命令末尾添加 --help 即可查看这些参数。

This script will work on any Debian-based operating system, including Ubuntu and Diet-Pi. You may need to run sudo apt install build-essential git curl first to ensure npm is able to fetch and build any binary modules it needs to install.

该脚本将:

  • 如果存在现有版本的Node-RED,请将其移除。
  • 如果检测到已安装Node.js,它将确保版本至少为v14。如果低于v14,它将停止并让用户决定是继续使用Node-RED 1.x版本,还是将Nodejs升级到更新的LTS版本。如果未检测到安装,它将使用NodeSource软件包安装Node.js 16 LTS版本。
  • 使用npm安装最新版本的Node-RED。
  • 可选安装一组针对树莓派的有用节点集合。
  • 将Node-RED设置为服务运行,并提供一组用于操作该服务的命令。
Node-RED has also been packaged for the Raspberry Pi OS repositories and appears in their list of 'Recommended Software'. This allows it to be installed using apt-get install nodered and includes the Raspberry Pi OS-packaged version of Node.js, but does not include npm.

注意: 目前RaspiOS Bullseye系统默认包含的node.js版本仍为v12。这意味着可安装的最新Node-RED版本仅限于2.x分支。虽然使用这些软件包最初很方便,但我们强烈建议改用上述安装脚本。

本地运行

本地运行Node-RED类似,您可以使用 node-red命令在终端中运行Node-RED。然后可以通过按下 Ctrl-C或关闭终端窗口来停止它。

由于树莓派的内存有限,您需要以额外参数启动Node-RED,告知底层的Node.js进程比默认情况更早释放未使用的内存。

要实现这一点,您应该使用替代的node-red-pi命令并传入max-old-space-size参数。

node-red-pi --max-old-space-size=256

作为服务运行

Pi的安装脚本还将其设置为以服务形式运行。这意味着它可以在后台运行,并配置为在启动时自动启动。

以下命令可用于操作该服务:

  • node-red-start - 启动Node-RED服务并显示其日志输出。按下Ctrl-C或关闭窗口不会停止服务;服务会继续在后台运行
  • node-red-stop - 停止Node-RED服务
  • node-red-restart - 这会停止并重新启动Node-RED服务
  • node-red-log - 显示服务的日志输出

你也可以在树莓派操作系统桌面上通过选择菜单 -> 编程 -> Node-RED菜单选项来启动Node-RED服务。

开机自动启动

如果您希望Node-RED在树莓派开机或重启时自动运行,可以通过执行以下命令启用服务自动启动:

sudo systemctl enable nodered.service

要禁用该服务,请运行以下命令:

sudo systemctl disable nodered.service

打开编辑器

Node-RED运行后,您可以在浏览器中访问编辑器。

如果您在树莓派桌面上使用浏览器,可以打开地址:http://localhost:1880

We recommend using a browser outside of the PI and pointing it at Node-RED running on the Pi. However you can use the built in browser and if so we recommend Chromium or Firefox-ESR and not Epiphany

.

从其他机器浏览时,您应该使用树莓派的主机名或IP地址:http://:1880。您可以通过在树莓派上运行hostname -I命令来查找IP地址。

下一步