介绍
node-red-contrib-clearblade-mqtt
是一个基于 Node-RED
的 npm
包,支持使用 ClearBlade
平台的 MQTT
服务进行智能设备的数据收集和控制。
本文将详细介绍如何使用该 npm
包,并提供示例代码帮助读者更好地理解。
使用方法
在
Node-RED
中,安装node-red-contrib-clearblade-mqtt
。npm install node-red-contrib-clearblade-mqtt
设置
ClearBlade
平台的MQTT
连接信息。在Node-RED
的Settings.js
文件中,添加如下代码:-- -------------------- ---- ------- --- ------------------ - - ---------- ------------------------- ------------- --------------------------- -------------- ----- ------- ----- ---------- --------------- ------------- ------------------ ------ --------------- --------- ----------------- -- -------------------------------- - ---------------------- ------------------------------------ - ---------- - --- ------------ - ------------- ---- -- --- ---------- - --------------------------------- --- ----------- - - ------ ------------------------- --------- ---------------------------- ---------- ----------------------------- ------------- -------------------------------- -- -------------- - ---------- - ------- - ---- -------- -------- --- ------- ------- ---- - ------------- -------- --------------- ----- -- ---------------------------- -------------- -- ---------------------------------------
展开代码使用
ClearBlade
平台的MQTT
服务。示例代码:
[{"id":"9ef25a7f.96b1a8","type":"inject","z":"dd7caced.1a6b7","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"5","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":170,"y":120,"wires":[["8f94ba24.321de"]]},{"id":"8f94ba24.321de","type":"function","z":"dd7caced.1a6b7","name":"publish data","func":"msg.topic = \"clearblade/sensor/data\";\nmsg.payload = {\n \"temperature\": 26,\n \"humidity\": 40\n}\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":390,"y":120,"wires":[["1cc2b8f1.67e338"]]},{"id":"1cc2b8f1.67e338","type":"clearblade-mqtt out","z":"dd7caced.1a6b7","name":"","broker":"14f1160d.69a3a9","brokerConn":"8497160f.a1f2b","x":620,"y":120,"wires":[]},{"id":"14f1160d.69a3a9","type":"clearblade-mqtt-broker","z":"","broker":"messaging.{platform-url}","port":"8903","useSSL":true,"clientID":"node-red-384dcb08.89c2c","username":"{user-email}","password":"{user-password}"},{"id":"8497160f.a1f2b","type":"clearblade-mqtt-conn","z":"","clientID":"node-red-384dcb08.89c2c","clean":true}]
注意事项
在设置
ClearBlade
平台的MQTT
连接信息时,需要将{platform-url}
、{system-key}
、{system-secret}
、{user-email}
和{user-password}
替换成相应的信息。在示例代码中,使用了
inject
和function
两个Node-RED
节点分别产生数据和发布数据。在实际使用时,可以根据需要添加其它节点。clearblade-mqtt out
节点为node-red-contrib-clearblade-mqtt
包提供的节点,用于将数据发送到ClearBlade
平台。在使用该节点时,需要为该节点设置一个clearblade-mqtt-broker
节点和一个clearblade-mqtt-conn
节点。
结论
本文简单介绍了 node-red-contrib-clearblade-mqtt
包的使用方法,并通过示例代码详细说明了如何使用该包进行智能设备的数据收集和控制。希望本文能够对读者有所帮助,同时也希望读者能够深入了解该 npm
包,为智能设备的开发和应用做出更多的贡献。
来源:JavaScript中文网 ,转载请注明来源 https://www.javascriptcn.com/post/600671cf30d0927023822936