目录
网络节点
1.1 mqtt in节点
连接到MQTT代理并订阅来自指定主题的消息。如下简单的使用:
function函数:
msg.payload = msg;
return msg;
配置该节点:服务端需要配置的信息较多,稍后再讲。
订阅主题的方式:单个主题和动态主题,使用动态主题时,没有主题名称选项。
服务端: 对应提供服务的IP地址 和 端口号,我使用的为本地的MQTT服务器 EMQX 部署在本地。
这里的账号密码对应MQTT服务器的热衷账号和密码。
如下所示,我本地部署的MQTT服务器的账号密码。
1.2 mqtt out节点
连接到MQTT代理并发布消息。发布到MQTT服务器后,可以被其他客户端订阅。如1.1所示订阅该数据。
function函数:
# 发布的话题名称
msg.topic = '/data';
# 服务质量
msg.qos = 0;
return msg;
如下所示连接:
使用动态话题则可把除服务端以外全部留白。 如下:
如下所示发布订阅和订阅在同一系统,在实际使用中,可以把MQTT的客户端挂载在不同系统上面。例如,使用本地电脑连接PLC采集数据,也可使用单片机采集数据,使用MQTT协议发布到MQTT云服务器上,再使用手机,或者小程序订阅该话题,即可数据上云,这就是简单的物联网。
[
{
"id": "920f86e7462b0e2e",
"type": "mqtt in",
"z": "e09ceb5a7ea8cc48",
"name": "",
"topic": "/data",
"qos": "2",
"datatype": "auto-detect",
"broker": "11b9c434447860ee",
"nl": false,
"rap": true,
"rh": 0,
"inputs": 0,
"x": 230,
"y": 220,
"wires": [
[
"c7928900464576ae"
]
]
},
{
"id": "1a03a208e4ac506b",
"type": "mqtt out",
"z": "e09ceb5a7ea8cc48",
"name": "",
"topic": "",
"qos": "",
"retain": "",
"respTopic": "",
"contentType": "",
"userProps": "",
"correl": "",
"expiry": "",
"broker": "11b9c434447860ee",
"x": 570,
"y": 320,
"wires": []
},
{
"id": "fe39c21ed1d5f214",
"type": "function",
"z": "e09ceb5a7ea8cc48",
"name": "function 1",
"func": "msg.topic = '/data';\nmsg.qos = 0;\n\nreturn msg;",
"outputs": 1,
"timeout": 0,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 420,
"y": 320,
"wires": [
[
"1a03a208e4ac506b"
]
]
},
{
"id": "f868f911fb5f2629",
"type": "debug",
"z": "e09ceb5a7ea8cc48",
"name": "debug 1",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "false",
"statusVal": "",
"statusType": "auto",
"x": 540,
"y": 220,
"wires": []
},
{
"id": "07612def1387f1b5",
"type": "inject",
"z": "e09ceb5a7ea8cc48",
"name": "",
"props": [
{
"p": "payload"
},
{
"p": "topic",
"vt": "str"
}
],
"repeat": "1",
"crontab": "",
"once": true,
"onceDelay": "1",
"topic": "",
"payload": "",
"payloadType": "date",
"x": 240,
"y": 320,
"wires": [
[
"fe39c21ed1d5f214"
]
]
},
{
"id": "c7928900464576ae",
"type": "function",
"z": "e09ceb5a7ea8cc48",
"name": "function 2",
"func": "msg.payload = msg;\nreturn msg;",
"outputs": 1,
"timeout": 0,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 380,
"y": 220,
"wires": [
[
"f868f911fb5f2629"
]
]
},
{
"id": "11b9c434447860ee",
"type": "mqtt-broker",
"name": "",
"broker": "127.0.0.1",
"port": "1883",
"clientid": "",
"autoConnect": true,
"usetls": false,
"protocolVersion": "4",
"keepalive": "60",
"cleansession": true,
"autoUnsubscribe": true,
"birthTopic": "",
"birthQos": "0",
"birthRetain": "false",
"birthPayload": "",
"birthMsg": {},
"closeTopic": "",
"closeQos": "0",
"closeRetain": "false",
"closePayload": "",
"closeMsg": {},
"willTopic": "",
"willQos": "0",
"willRetain": "false",
"willPayload": "",
"willMsg": {},
"userProps": "",
"sessionExpiry": ""
}
]
1.3 http in 节点
创建用于创建Web服务的HTTP端点,类似于后端编程的Controller层。可以使用浏览器访问该 URL 则会被该节点监听触发该节点。
请求方式:POST 提交数据、GET 获取数据,其他比较少使用。
URL 地址:需要加上 node-red 的前缀,如下的访问URL地址为:127.0.0.1:1880/test
http in 需要配合和 http response 使用,然后往 msg.paylaod 写入HTML代码、json数据,即可用来写后端服务。
1.4 http request 节点
发送HTTP请求并返回响应。
function:
# 请求的url
msg.url='http://127.0.0.1:1880/test';
# 请求的方法
msg.method='GET';
return msg;
http request节点:可以全部空白,数据使用前方的 function 函数来传递。
下方是简单的应用,也可以部署本地的网站服务。
由 注入 触发 http 请求 ,再由 get 捕获请求, 模板 注入数据 最后 响应数据 返回到 http 请求 ,debug 打印刚刚请求回来数据。
[
{
"id": "455eb66e93290c9e",
"type": "http in",
"z": "e09ceb5a7ea8cc48",
"name": "",
"url": "/test",
"method": "get",
"upload": false,
"swaggerDoc": "",
"x": 160,
"y": 260,
"wires": [
[
"3d309eb93435dbd2"
]
]
},
{
"id": "0e62aa47af98351c",
"type": "http response",
"z": "e09ceb5a7ea8cc48",
"name": "",
"statusCode": "",
"headers": {},
"x": 470,
"y": 260,
"wires": []
},
{
"id": "3d309eb93435dbd2",
"type": "template",
"z": "e09ceb5a7ea8cc48",
"name": "",
"field": "payload",
"fieldType": "msg",
"format": "html",
"syntax": "mustache",
"template": "<!DOCTYPE html>\n<html>\n\n<head>\n <meta charset=\"utf-8\">\n <title>Title</title>\n</head>\n\n<body>\n\n <h1>我的第一个标题</h1>\n\n <p>我的第一个段落。</p>\n\n</body>\n\n</html>",
"output": "str",
"x": 330,
"y": 260,
"wires": [
[
"0e62aa47af98351c"
]
]
},
{
"id": "5bf0f58ddf762f24",
"type": "http request",
"z": "e09ceb5a7ea8cc48",
"name": "",
"method": "GET",
"ret": "txt",
"paytoqs": "ignore",
"url": "",
"tls": "",
"persist": false,
"proxy": "",
"insecureHTTPParser": false,
"authType": "",
"senderr": false,
"headers": [],
"x": 400,
"y": 360,
"wires": [
[
"f79d9389ae6d74cf"
]
]
},
{
"id": "1968c8810e73554a",
"type": "function",
"z": "e09ceb5a7ea8cc48",
"name": "function 1",
"func": "msg.url='http://127.0.0.1:1880/test';\nmsg.method='GET';\nreturn msg;",
"outputs": 1,
"timeout": 0,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 250,
"y": 360,
"wires": [
[
"5bf0f58ddf762f24"
]
]
},
{
"id": "6513ee4634128a61",
"type": "inject",
"z": "e09ceb5a7ea8cc48",
"name": "",
"props": [
{
"p": "payload"
},
{
"p": "topic",
"vt": "str"
}
],
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"topic": "",
"payload": "",
"payloadType": "num",
"x": 100,
"y": 360,
"wires": [
[
"1968c8810e73554a"
]
]
},
{
"id": "f79d9389ae6d74cf",
"type": "debug",
"z": "e09ceb5a7ea8cc48",
"name": "debug 1",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "false",
"statusVal": "",
"statusType": "auto",
"x": 560,
"y": 360,
"wires": []
}
]