跳转至

Node Red 输入和输出节点说明

输入顾名思义就是Node Red自己提供的触发节点,一般我们写的流程基本都会以这个为开始的节点,常用的有以下这些:

  • inject
  • catch, 暂时基本没用过,等到了用了的场景再说
  • status
  • link
  • mqtt
  • http
  • websocket 暂时没有用到,后面再补
  • tcp 暂时没有用到,后面再补
  • udp 暂时没有用到,后面再补

inject

可用周期性的执行触发事件,事件内容包括一下:文字列,数字,布尔,json,二进制流,时间戳,还有node red本身提供存储的内容flow,global和系统的环境变量 env

看看下面的例子, 点击图片中指定的位置,看看输出信息有啥变化 node_red_inject_1

节点信息如下:

[{"id":"4bdb7253.4af98c","type":"tab","label":"流程2","disabled":false,"info":""},{"id":"a8a07df6.c6ce6","type":"inject","z":"4bdb7253.4af98c","name":"","topic":"","payload":"文字测试","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":259,"y":114,"wires":[["52b6c491.da1b9c"]]},{"id":"52b6c491.da1b9c","type":"debug","z":"4bdb7253.4af98c","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","x":515,"y":115,"wires":[]},{"id":"ba641760.6e1ec8","type":"inject","z":"4bdb7253.4af98c","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":240,"y":205,"wires":[["52b6c491.da1b9c"]]},{"id":"561287e4.017108","type":"inject","z":"4bdb7253.4af98c","name":"json测试","topic":"","payload":"{\"a\":\"b\"}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":241,"y":286,"wires":[["52b6c491.da1b9c"]]}]

这个节点是我们经常使用的,多数用于执行指定时间, 周期性执行. 指定时间段周期性支持 就是这两个综合起来执行

  • 每隔5s执行一下
[{"id":"f48c9672.fce908","type":"inject","z":"68360605.862db8","name":"","topic":"","payload":"","payloadType":"date","repeat":"5","crontab":"","once":false,"onceDelay":0.1,"x":176,"y":119,"wires":[["8508d435.f65108"]]},{"id":"8508d435.f65108","type":"debug","z":"68360605.862db8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","x":408,"y":117,"wires":[]}]
  • 指定13:00执行
[{"id":"f48c9672.fce908","type":"inject","z":"68360605.862db8","name":"指定13:00执行","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"00 13 * * *","once":false,"onceDelay":0.1,"x":196,"y":119,"wires":[["8508d435.f65108"]]},{"id":"8508d435.f65108","type":"debug","z":"68360605.862db8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","x":408,"y":117,"wires":[]}]

status

这个用来报告一些节点的状态,他配合一些节点带状态的,例如可以监听mqtt连接还是断开,节点信息如下:

[{"id":"68360605.862db8","type":"tab","label":"测试流程1","disabled":false,"info":""},{"id":"8508d435.f65108","type":"debug","z":"68360605.862db8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","x":408,"y":117,"wires":[]},{"id":"cbad2571.df8c38","type":"status","z":"68360605.862db8","name":"","scope":["329516d4.f0c4aa"],"x":185,"y":128,"wires":[["8508d435.f65108"]]},{"id":"329516d4.f0c4aa","type":"mqtt in","z":"68360605.862db8","name":"","topic":"test/test","qos":"2","broker":"1550bab3.2a8365","x":165,"y":210,"wires":[[]]},{"id":"1550bab3.2a8365","type":"mqtt-broker","z":"","name":"home","broker":"xxxxx.net","port":"2883","clientid":"","usetls":false,"compatmode":true,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""}]

这个是用来拆分流程使用的,例如有时候我不想把连接线拖的那么长,这时候这个就有用了. link 分为 outin,in接收out发过来的数据.看一下流程的gif:

node_red_inject_3

[{"id":"68360605.862db8","type":"tab","label":"测试流程1","disabled":false,"info":""},{"id":"ced4c65d.341768","type":"inject","z":"68360605.862db8","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":89,"y":70,"wires":[["736b3003.534f5"]]},{"id":"736b3003.534f5","type":"link out","z":"68360605.862db8","name":"1","links":["ca00656e.ae14a8"],"x":248,"y":69,"wires":[]},{"id":"ca00656e.ae14a8","type":"link in","z":"68360605.862db8","name":"2","links":["736b3003.534f5"],"x":92,"y":240,"wires":[["9a26b0f7.aa39e","b9003c01.8be29"]]},{"id":"9a26b0f7.aa39e","type":"debug","z":"68360605.862db8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":274,"y":220,"wires":[]},{"id":"b9003c01.8be29","type":"debug","z":"68360605.862db8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":257,"y":298,"wires":[]}]

mqtt

MQTT(Message Queuing Telemetry Transport,消息队列遥测传输)是IBM开发的一个即时通讯协议,有可能成为物联网的重要组成部分。该协议支持所有平台,几乎可以把所有联网物品和外部连接起来,被用来当做传感器和制动器(比如通过Twitter让房屋联网)的通信协议。主要说明一个qos:

  • 0 代表,“至多一次”,消息发布完全依赖底层 TCP/IP 网络。会发生消息丢失或重复。这一级别可用于如下情况,环境传感器数据,丢失一次读记录无所谓,因为不久后还会有第二次发送。
  • 1 代表,“至少一次”,确保消息到达,但消息重复可能会发生。
  • 2 代表,“只有一次”,确保消息到达一次。这一级别可用于如下情况,在计费系统中,消息重复或丢失会导致不正确的结果。

客户端工具可以使用这个mqttfx

node_red_inject_4

[{"id":"68360605.862db8","type":"tab","label":"测试流程1","disabled":false,"info":""},{"id":"361df5ad.6ce26a","type":"mqtt in","z":"68360605.862db8","name":"","topic":"node-red/xxxx","qos":"2","broker":"fc5a0d23.831d3","x":106,"y":165,"wires":[["12e936d1.f97ab9"]]},{"id":"12e936d1.f97ab9","type":"debug","z":"68360605.862db8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":324,"y":159,"wires":[]},{"id":"fc5a0d23.831d3","type":"mqtt-broker","z":"","name":"xxxx","broker":"192.168.3.10","port":"1883","clientid":"","usetls":false,"compatmode":true,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""}]

http

这个用来新建一个http server 服务,当我们的第三方服务可以在状态改变的时候发送http 请求的时候就特别有用了.这个需要配合输出 http response功能的function使用,一个负责响应http的状态码和Header,一个负责内容

下面例子你可以访问一下你的浏览器http://ip:1880/node-red, 看看返回的内容

[{"id":"68360605.862db8","type":"tab","label":"测试流程1","disabled":false,"info":""},{"id":"5b98a08f.9123e","type":"http in","z":"68360605.862db8","name":"","url":"/node-red","method":"get","upload":false,"swaggerDoc":"","x":200,"y":154,"wires":[["ef137a6.e6d3088"]]},{"id":"e5a43d9c.63d0a","type":"http response","z":"68360605.862db8","name":"","statusCode":"200","headers":{},"x":644,"y":160,"wires":[]},{"id":"ef137a6.e6d3088","type":"template","z":"68360605.862db8","name":"","field":"payload","fieldType":"msg","format":"json","syntax":"plain","template":"{\"status\":\"ok\"}","output":"str","x":457,"y":158,"wires":[["e5a43d9c.63d0a"]]}]