最近对Node-RED比较感兴趣,因为在上OpenHarmony课程的时候,一直想找一个可以通过MQTT控制设备的低代码客户端解决方案。第一次指导Node-RED是在试用聆思开发板的时候,它的云端就是使用的Node-RED。
在安装Node-RED之前,请确保您的系统已安装Node.js(建议版本8.x LTS及以上)。如果没有NodeJS,可以参考NodeJS入门(一)---nodejs详细安装步骤_node.js-CSDN博客。
对于Node.js的包管理器(npm)用户,打开终端或命令提示符。运行以下命令以全局安装Node-RED:
npm install -g --unsafe-perm node-red
安装完成后,通过运行以下命令启动Node-RED:
node-red
程序执行会产生如下log:
C:\Users\Administrator>node-red
21 Aug 09:10:21 - [info]
Welcome to Node-RED
===================
21 Aug 09:10:21 - [info] Node-RED version: v4.0.2
21 Aug 09:10:21 - [info] Node.js version: v18.19.0
21 Aug 09:10:21 - [info] Windows_NT 10.0.17763 x64 LE
21 Aug 09:10:26 - [info] Loading palette nodes
21 Aug 09:10:35 - [info] Settings file : C:\Users\Administrator\.node-red\settings.js
21 Aug 09:10:35 - [info] Context store : 'default' [module=memory]
21 Aug 09:10:35 - [info] User directory : \Users\Administrator\.node-red
21 Aug 09:10:35 - [warn] Projects disabled : editorTheme.projects.enabled=false
21 Aug 09:10:35 - [info] Flows file : \Users\Administrator\.node-red\flows.json
21 Aug 09:10:35 - [info] Creating new flow file
21 Aug 09:10:35 - [warn]
---------------------------------------------------------------------
Your flow credentials file is encrypted using a system-generated key.
If the system-generated key is lost for any reason, your credentials
file will not be recoverable, you will have to delete it and re-enter
your credentials.
You should set your own key using the 'credentialSecret' option in
your settings file. Node-RED will then re-encrypt your credentials
file using your chosen key the next time you deploy a change.
---------------------------------------------------------------------
21 Aug 09:10:35 - [warn] Encrypted credentials not found
21 Aug 09:10:35 - [info] Starting flows
21 Aug 09:10:35 - [info] Started flows
21 Aug 09:10:35 - [info] Server now running at http://127.0.0.1:1880/
使用浏览器访问 http://127.0.0.1:1880/就可以看到Node-RED界面。在左侧的模块栏中可以找到mqtt、http等网络模块。后面我们就可以尝试用Node-RED去控制OpenHarmony的端侧设备。
设置好发布端的MQTT对象,后面就是