###### tags: `Tutorial` # 如何在QIoT Suite Lite中透過Modbus Gateway與Modbus裝置通訊(以透過Modbus Gateway+AQD-101 四合一空氣品質顯示板示範) 本教學教您如何在 QIoT Suite Lite 中透過 Modbus Gateway (使用 [WEP232 Modbus Gateway](http://www.artt.com.tw/ec99/rwd1146/product.asp?Xcategory_id=57&prodid=IDEC_DEMO3&category_id=61&cty_id=61) 示範)與 Modbus 裝置通訊,並以 [AQD-101 四合一空氣品質顯示板](http://www.artt.com.tw/ec99/rwd1146/product.asp?Xcategory_id=60&prodid=AQD101&category_id=62&cty_id=62)示範,安裝第三方 Node-red Plugin 支援 Modbus 協定裝置,使用的 Plugin 是 : [node-red-contrib-modbus](https://flows.nodered.org/node/node-red-contrib-modbus),請參考以下步驟安裝 Plugin : ## 安裝Node-red modbus plugin 1. 使用 SSH 登入 NAS 後,執行以下 Command ```bash system-docker exec -ti qiot-node-red bash ``` **SSH連線方式可參考此篇教學文 - [How to access QNAP NAS by SSH?](https://www.qnap.com/go/how-to/knowledge-base/article/how-to-access-qnap-nas-by-ssh/)** 2. 切換至指定目錄 ```bash cd /usr/local/lib/node_modules/node-red ``` 3. 安裝node-red-contrib-modbus ```bash npm install node-red-contrib-modbus ``` 4. 重啟所有 Node-red ```bash supervisorctl restart all ``` ## Node-red modbus plugin 使用方式(AQD-101 四合一空氣品質顯示板示範) ![](https://i.imgur.com/LdJs5k7.png) 1. 請先參考下圖建立 Thing-Resource (參考 **[How to show DHT11 data on a QIoT Dashboard?](https://www.qnap.com/en/how-to/tutorial/article/how-to-show-dht11-data-on-a-qiot-dashboard)** 步驟1~5) : ![](https://i.imgur.com/zoZHJLp.png) 2. 參考以下範例,將範例 JSON 流程匯入到 Rule (參見 : **[How to Backup QIoT Suite Lite](https://www.qnap.com/en/how-to/tutorial/article/how-to-backup-qiot-suite-lite)** 第一部分) : ```json= [ { "id": "c01d41a4.a2fbc", "type": "tab", "label": "Flow 1" }, { "id": "60f9272b.659558", "type": "modbus-client", "z": "", "name": "Air1", "clienttype": "tcp", "bufferCommands": true, "stateLogEnabled": false, "tcpHost": "192.168.110.100", "tcpPort": "502", "tcpType": "DEFAULT", "serialPort": "/dev/ttyUSB", "serialType": "RTU-BUFFERD", "serialBaudrate": "9600", "serialDatabits": "8", "serialStopbits": "1", "serialParity": "none", "serialConnectionDelay": "100", "unit_id": 1, "commandDelay": 1, "clientTimeout": 1000, "reconnectTimeout": 2000 }, { "id": "c37433a1.6ea7b", "type": "ui_base", "theme": { "name": "theme-light", "lightTheme": { "default": "#0094CE", "baseColor": "#0094CE", "baseFont": "-apple-system,BlinkMacSystemFont,Segoe UI,Roboto,Oxygen-Sans,Ubuntu,Cantarell,Helvetica Neue,sans-serif", "edited": true, "reset": false }, "darkTheme": { "default": "#097479", "baseColor": "#097479", "baseFont": "-apple-system,BlinkMacSystemFont,Segoe UI,Roboto,Oxygen-Sans,Ubuntu,Cantarell,Helvetica Neue,sans-serif", "edited": false }, "customTheme": { "name": "Untitled Theme 1", "default": "#4B7930", "baseColor": "#4B7930", "baseFont": "-apple-system,BlinkMacSystemFont,Segoe UI,Roboto,Oxygen-Sans,Ubuntu,Cantarell,Helvetica Neue,sans-serif" }, "themeState": { "base-color": { "default": "#0094CE", "value": "#0094CE", "edited": false }, "page-titlebar-backgroundColor": { "value": "#0094CE", "edited": false }, "page-backgroundColor": { "value": "#fafafa", "edited": false }, "page-sidebar-backgroundColor": { "value": "#ffffff", "edited": false }, "group-textColor": { "value": "#1bbfff", "edited": false }, "group-borderColor": { "value": "#ffffff", "edited": false }, "group-backgroundColor": { "value": "#ffffff", "edited": false }, "widget-textColor": { "value": "#111111", "edited": false }, "widget-backgroundColor": { "value": "#0094ce", "edited": false }, "widget-borderColor": { "value": "#ffffff", "edited": false }, "base-font": { "value": "-apple-system,BlinkMacSystemFont,Segoe UI,Roboto,Oxygen-Sans,Ubuntu,Cantarell,Helvetica Neue,sans-serif" } } }, "site": { "name": "Node-RED Dashboard", "hideToolbar": "false", "allowSwipe": "false", "dateFormat": "DD/MM/YYYY", "sizes": { "sx": 48, "sy": 48, "gx": 6, "gy": 6, "cx": 6, "cy": 6, "px": 0, "py": 0 } } }, { "id": "e1e0707f.b407c", "type": "modbus-client", "z": "", "name": "gw", "clienttype": "tcp", "bufferCommands": true, "stateLogEnabled": false, "tcpHost": "192.168.0.100", "tcpPort": "502", "tcpType": "DEFAULT", "serialPort": "/dev/ttyUSB", "serialType": "RTU-BUFFERD", "serialBaudrate": "9600", "serialDatabits": "8", "serialStopbits": "1", "serialParity": "none", "serialConnectionDelay": "100", "unit_id": 1, "commandDelay": 1, "clientTimeout": 1000, "reconnectTimeout": 2000 }, { "id": "dfc2de90.d0679", "type": "ui_group", "z": "", "name": "QNAP", "tab": "9195395f.dd7be8", "disp": true, "width": "20" }, { "id": "9195395f.dd7be8", "type": "ui_tab", "z": "", "name": "AQD101", "icon": "dashboard" }, { "id": "5476a4bd.1d5a0c", "type": "modbus-response", "z": "c01d41a4.a2fbc", "name": "", "registerShowMax": 20, "x": 610, "y": 100, "wires": [] }, { "id": "9c555352.193fd", "type": "debug", "z": "c01d41a4.a2fbc", "name": "", "active": true, "tosidebar": true, "console": false, "tostatus": false, "complete": "false", "x": 290, "y": 260, "wires": [] }, { "id": "1794adc0.56e812", "type": "qiotbroker out", "z": "c01d41a4.a2fbc", "name": "temp", "opt_customtopic": false, "topic": "", "qos": "0", "retain": "false", "thing": "1eb4888a-254f-44be-a6c6-7a2c8a2019c1", "qtopic": "qiot/things/admin/air/temp", "username": "admin", "ver": "1", "x": 890, "y": 140, "wires": [] }, { "id": "d17c98b5.a8cad8", "type": "qiotbroker out", "z": "c01d41a4.a2fbc", "name": "hum", "opt_customtopic": false, "topic": "", "qos": "0", "retain": "false", "thing": "1eb4888a-254f-44be-a6c6-7a2c8a2019c1", "qtopic": "qiot/things/admin/air/co2", "username": "admin", "ver": "1", "x": 890, "y": 200, "wires": [] }, { "id": "c9ff0a1e.64b998", "type": "qiotbroker out", "z": "c01d41a4.a2fbc", "name": "co2", "opt_customtopic": false, "topic": "", "qos": "0", "retain": "false", "thing": "1eb4888a-254f-44be-a6c6-7a2c8a2019c1", "qtopic": "qiot/things/admin/air/co2", "username": "admin", "ver": "1", "x": 890, "y": 80, "wires": [] }, { "id": "3cf2b649.a55e6a", "type": "function", "z": "c01d41a4.a2fbc", "name": "AQD101", "func": "var myCO2=msg.payload[5];\nvar myPM25=msg.payload[6];\nvar myTemp=msg.payload[7]/100;\nvar myHumd=msg.payload[8]/100;\n/*\n//if (myTemp > 40) myTemp = 29.5;\n//if (myTemp < 10) myTemp = 29.5;\nvar oldTemp = global.get('Temp')||29.5;\nif (Math.abs(myTemp - oldTemp) > 1) myTemp = oldTemp;\n\nglobal.set('Temp', myTemp);\n*/\n\n/*\nmsg.payload.CO2 = myCO2;\nmsg.payload.Temp = myTemp;\nmsg.payload.Humd = myHumd;\nmsg.payload.PM25 = myPM25;\n*/\n\nvar CO2 = { payload:{value:myCO2}};\nvar Temp = { payload:{value:myTemp}};\nvar Humd = { payload:{value:myHumd}};\nvar PM25 = { payload:{value:myPM25}};\n\nreturn [CO2, Temp, Humd, PM25];\n//return msg;", "outputs": 4, "noerr": 0, "x": 580, "y": 180, "wires": [ [ "c9ff0a1e.64b998" ], [ "1794adc0.56e812" ], [ "d17c98b5.a8cad8" ], [ "c08f0aaf.45a8d8" ] ], "outputLabels": [ "CO2", "Temp", "Humd", "PM25" ] }, { "id": "445c7656.277ba8", "type": "modbus-read", "z": "c01d41a4.a2fbc", "name": "", "topic": "", "showStatusActivities": false, "showErrors": false, "unitid": "1", "dataType": "InputRegister", "adr": "0", "quantity": "9", "rate": "1", "rateUnit": "s", "delayOnStart": false, "startDelayTime": "", "server": "60f9272b.659558", "x": 290, "y": 160, "wires": [ [ "5476a4bd.1d5a0c", "3cf2b649.a55e6a" ], [ "9c555352.193fd" ] ] }, { "id": "c08f0aaf.45a8d8", "type": "qiotbroker out", "z": "c01d41a4.a2fbc", "name": "pm2.5", "opt_customtopic": false, "topic": "", "qos": "0", "retain": "false", "thing": "1eb4888a-254f-44be-a6c6-7a2c8a2019c1", "qtopic": "qiot/things/admin/air/pm25", "username": "admin", "ver": "1", "x": 887, "y": 257, "wires": [] } ] ``` 3. 匯入完成後請先變更 QBroker Node,雙擊下圖紅色方框其中之一 ![](https://i.imgur.com/8kR8jFL.png) 4. 進入設定介面後參見下圖選擇相應的 Thing 跟 Resource (例:co2選co2、temp選temp)後點擊 **Done** ![](https://i.imgur.com/JcsxlHs.png) 5. 依序將其他的 QBroker Node 修改 6. 雙擊 **Modbus Read** 設定,下圖 :two: 的地方依據您的裝置操作手冊設定(AQD-101依據圖示設定), :three: 地方設定 Modbus Gateway IP 設定,點擊 :pencil2: ![](https://i.imgur.com/rxZU3TX.png) 7. 此處依據您的 Modbus Gateway IP、Port 進行設定,設定完畢後點擊 **Update**在點擊 **Done** ![](https://i.imgur.com/RCiMVGd.png) 9. **AQD101** Function node 可雙擊設定依據您裝置輸出的格式將程式碼進行調整 10. 點擊 **Save** 保存設定 11. 之後參閱 [**How to use the Dashboard after updating QIoT Suite Lite from v1.1.1 to v1.1.2?**](https://www.qnap.com/go/how-to/tutorial/article/how-to-use-the-dashboard-after-updating-qiot-suite-lite-from-v1-1-1-to-v1-1-2) 方法一進行設定,將**temp**、**hum**、**co2**、**pm25**加入到Dashboard中,完成後如下圖 ![](https://i.imgur.com/vbRG8yj.png)