前回の投稿の続きになります。
MQTTでデータを送信する
Node-REDに送信するデータとMQTT用のフローを追加します。
全体
[{"id":"fcc27bf6cad6aa63","type":"BLE Beacon Scanner","z":"23af55b50b4cc44f","name":"","x":240,"y":200,"wires":[["1265703ed62aff71"]]},{"id":"6aca689b132472bc","type":"debug","z":"23af55b50b4cc44f","name":"Debug","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":730,"y":200,"wires":[]},{"id":"1265703ed62aff71","type":"switch","z":"23af55b50b4cc44f","name":"MacAddressでfilter","property":"payload.address","propertyType":"msg","rules":[{"t":"eq","v":"f4:46:ce:9a:a3:28","vt":"str"}],"checkall":"true","repair":false,"outputs":1,"x":510,"y":200,"wires":[["6aca689b132472bc","f05f425955799d2c"]]},{"id":"56bc5675787ebacc","type":"debug","z":"23af55b50b4cc44f","name":"Debug2","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":780,"y":260,"wires":[]},{"id":"f05f425955799d2c","type":"function","z":"23af55b50b4cc44f","name":"Object->Array","func":"// input変数にはInjectノードからのメッセージが含まれます\nvar inputObject = msg.payload.other; // input.payloadはオブジェクトと仮定\n\nif (typeof inputObject === 'object' && inputObject !== null) {\n // オブジェクトのキーを取得し、それを使ってArrayを生成\n var keys = Object.keys(inputObject);\n var outputArray = keys.map(key => inputObject[key]);\n\n return { payload: outputArray };\n}\n\n// オブジェクトが存在しない場合\nreturn msg; // または、適切な値に置き換えてください\n","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":580,"y":260,"wires":[["56bc5675787ebacc","c4393b6d65082064","2c8fd5e2b3174986","93ae16e87d19abdc","1a5c501b20164e0f","5ebd915b8d95917d","9ea7fb152690e368","6f7b3495d5700c7f","6bf989fbb9a1687c"]]},{"id":"c4393b6d65082064","type":"function","z":"23af55b50b4cc44f","name":"16進数文字列の作成","func":"const data = msg.payload;\nvar fixdata = '';\n\nfor (let i = 0; i < data.length; i++) {\n var hexValue = data[i].toString(16);\n\n // 1桁の場合、前に0を補完\n if (hexValue.length === 1) {\n hexValue = '0' + hexValue;\n }\n\n fixdata = fixdata + hexValue;\n}\n\nmsg.payload = fixdata;\nreturn msg;\n","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":660,"y":320,"wires":[["07adc2c8774ebdaf","b503e2b5aa6395cb","fef74c87faf34edc"]]},{"id":"07adc2c8774ebdaf","type":"debug","z":"23af55b50b4cc44f","name":"Debug3","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":860,"y":320,"wires":[]},{"id":"b503e2b5aa6395cb","type":"function","z":"23af55b50b4cc44f","name":"データ整理","func":"const data = msg.payload;\nconst jsonData = {\n CompanyID: data.substring(0, 4),\n CO2: data.substring(4, 8),\n PMdata: data.substring(8, 16),\n Temperature: data.substring(16,20),\n Humid: data.substr(20)\n};\nmsg.payload = jsonData;\nreturn msg;","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":710,"y":380,"wires":[["7ec7d7569e433f02","97dae78b1ac20fdd","1019f66b07b300ff","7f142e3129f5faae","fa171c843d32d64c","5ab7fbe8dc6c0ef0"]]},{"id":"7ec7d7569e433f02","type":"debug","z":"23af55b50b4cc44f","name":"Debug4","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":900,"y":380,"wires":[]},{"id":"12eb37daec728514","type":"function","z":"23af55b50b4cc44f","name":"Temperature計算②","func":"msg.temp = (msg.payload[9] * 255 + msg.payload[8]) /10;\nreturn msg;","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1110,"y":100,"wires":[["58a610e0e9fdcb04","d33056ec34e78e80","a60c9946e21c9433","b3dcab26f4badb2f"]]},{"id":"58a610e0e9fdcb04","type":"debug","z":"23af55b50b4cc44f","name":"DebugTemp","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"temp","targetType":"msg","statusVal":"","statusType":"auto","x":1390,"y":40,"wires":[]},{"id":"2c8fd5e2b3174986","type":"debug","z":"23af55b50b4cc44f","name":"DebugHumid","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload[10]","targetType":"msg","statusVal":"","statusType":"auto","x":1170,"y":160,"wires":[]},{"id":"6bf989fbb9a1687c","type":"function","z":"23af55b50b4cc44f","name":"CO2計算②","func":"msg.co2 = msg.payload[3] * 255 + msg.payload[2];\nreturn msg;","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":970,"y":40,"wires":[["12eb37daec728514","4b8541eec9a9a45d"]]},{"id":"4b8541eec9a9a45d","type":"debug","z":"23af55b50b4cc44f","name":"DebugCO2","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"co2","targetType":"msg","statusVal":"","statusType":"auto","x":1170,"y":40,"wires":[]},{"id":"fef74c87faf34edc","type":"ui_text","z":"23af55b50b4cc44f","group":"71c614f549b6503f","order":0,"width":0,"height":0,"name":"","label":"16進全体","format":"{{msg.payload}}","layout":"row-spread","className":"","style":false,"font":"","fontSize":16,"color":"#000000","x":1140,"y":440,"wires":[]},{"id":"97dae78b1ac20fdd","type":"ui_text","z":"23af55b50b4cc44f","group":"71c614f549b6503f","order":1,"width":0,"height":0,"name":"","label":"CompanyID","format":"{{msg.payload.CompanyID}}","layout":"row-spread","className":"","style":false,"font":"","fontSize":16,"color":"#000000","x":1150,"y":480,"wires":[]},{"id":"1019f66b07b300ff","type":"ui_text","z":"23af55b50b4cc44f","group":"71c614f549b6503f","order":2,"width":0,"height":0,"name":"","label":"CO2 level","format":"{{msg.payload.CO2}}","layout":"row-spread","className":"","style":false,"font":"","fontSize":16,"color":"#000000","x":1140,"y":520,"wires":[]},{"id":"fa171c843d32d64c","type":"ui_text","z":"23af55b50b4cc44f","group":"71c614f549b6503f","order":3,"width":0,"height":0,"name":"","label":"PM〇〇","format":"{{msg.payload.PMdata}}","layout":"row-spread","className":"","style":false,"font":"","fontSize":16,"color":"#000000","x":1140,"y":560,"wires":[]},{"id":"5ab7fbe8dc6c0ef0","type":"ui_text","z":"23af55b50b4cc44f","group":"71c614f549b6503f","order":4,"width":0,"height":0,"name":"","label":"Temperature","format":"{{msg.payload.Temperature}}","layout":"row-spread","className":"","style":false,"font":"","fontSize":16,"color":"#000000","x":1150,"y":600,"wires":[]},{"id":"7f142e3129f5faae","type":"ui_text","z":"23af55b50b4cc44f","group":"71c614f549b6503f","order":5,"width":0,"height":0,"name":"","label":"Humidity","format":"{{msg.payload.Humid}}","layout":"row-spread","className":"","style":false,"font":"","fontSize":16,"color":"#000000","x":1140,"y":640,"wires":[]},{"id":"a60c9946e21c9433","type":"ui_text","z":"23af55b50b4cc44f","group":"a67b478cee43f5f9","order":6,"width":0,"height":0,"name":"","label":"CO2 level","format":"{{msg.co2}} ppm","layout":"row-spread","className":"","style":false,"font":"","fontSize":16,"color":"#000000","x":1560,"y":60,"wires":[]},{"id":"d33056ec34e78e80","type":"ui_text","z":"23af55b50b4cc44f","group":"a67b478cee43f5f9","order":6,"width":0,"height":0,"name":"","label":"Temperature","format":"{{msg.temp}} ℃","layout":"row-spread","className":"","style":false,"font":"","fontSize":16,"color":"#000000","x":1570,"y":100,"wires":[]},{"id":"93ae16e87d19abdc","type":"ui_text","z":"23af55b50b4cc44f","group":"a67b478cee43f5f9","order":6,"width":0,"height":0,"name":"","label":"Humidity","format":"{{msg.payload[10]}} %RH","layout":"row-spread","className":"","style":false,"font":"","fontSize":16,"color":"#000000","x":1560,"y":140,"wires":[]},{"id":"1a5c501b20164e0f","type":"ui_text","z":"23af55b50b4cc44f","group":"9ed0a591c4a0eed9","order":6,"width":0,"height":0,"name":"","label":"PM1.0 質量濃度","format":"{{msg.payload[4]}} μg/m3","layout":"row-spread","className":"","style":false,"font":"","fontSize":16,"color":"#000000","x":1580,"y":200,"wires":[]},{"id":"5ebd915b8d95917d","type":"ui_text","z":"23af55b50b4cc44f","group":"9ed0a591c4a0eed9","order":6,"width":0,"height":0,"name":"","label":"PM2.5 質量濃度","format":"{{msg.payload[5]}} μg/m3","layout":"row-spread","className":"","style":false,"font":"","fontSize":16,"color":"#000000","x":1580,"y":240,"wires":[]},{"id":"9ea7fb152690e368","type":"ui_text","z":"23af55b50b4cc44f","group":"9ed0a591c4a0eed9","order":6,"width":0,"height":0,"name":"","label":"PM4.0 質量濃度","format":"{{msg.payload[6]}} μg/m3","layout":"row-spread","className":"","style":false,"font":"","fontSize":16,"color":"#000000","x":1580,"y":280,"wires":[]},{"id":"6f7b3495d5700c7f","type":"ui_text","z":"23af55b50b4cc44f","group":"9ed0a591c4a0eed9","order":6,"width":0,"height":0,"name":"","label":"PM10.0 質量濃度","format":"{{msg.payload[7]}} μg/m3","layout":"row-spread","className":"","style":false,"font":"","fontSize":16,"color":"#000000","x":1590,"y":320,"wires":[]},{"id":"0937c6c3c2140fc0","type":"comment","z":"23af55b50b4cc44f","name":"Rawdata","info":"","x":1080,"y":400,"wires":[]},{"id":"06531716839b2117","type":"debug","z":"23af55b50b4cc44f","name":"Debug","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1530,"y":380,"wires":[]},{"id":"b3dcab26f4badb2f","type":"function","z":"23af55b50b4cc44f","name":"データ整理","func":"const data = msg.payload;\nconst jsonData = {\n CompanyID: \"0b60\",\n CO2: msg.co2 + \"ppm\",\n PM1: msg.payload[4] + \"μg/m3\",\n PM2: msg.payload[5] + \"μg/m3\",\n PM3: msg.payload[6] + \"μg/m3\",\n PM4: msg.payload[7] + \"μg/m3\",\n Temperature: msg.temp + \"℃\",\n Humid: msg.payload[10] + \"%\"\n};\nmsg.payload = jsonData;\nreturn msg;","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1370,"y":380,"wires":[["06531716839b2117","4ac248ebe772431f"]]},{"id":"4ac248ebe772431f","type":"mqtt out","z":"23af55b50b4cc44f","name":"","topic":"ratoktest","qos":"","retain":"","respTopic":"","contentType":"","userProps":"","correl":"","expiry":"","broker":"cb1195384d5774a6","x":1540,"y":460,"wires":[]},{"id":"71c614f549b6503f","type":"ui_group","name":"Rawdata","tab":"4e6203f8b7533ede","order":1,"disp":true,"width":"6","collapse":false,"className":""},{"id":"a67b478cee43f5f9","type":"ui_group","name":"View","tab":"4e6203f8b7533ede","order":2,"disp":true,"width":"6","collapse":false,"className":""},{"id":"9ed0a591c4a0eed9","type":"ui_group","name":"PM data","tab":"4e6203f8b7533ede","order":3,"disp":true,"width":"6","collapse":false,"className":""},{"id":"cb1195384d5774a6","type":"mqtt-broker","name":"","broker":"mqtt://public:public@public.cloud.shiftr.io","port":"1883","clientid":"","autoConnect":true,"usetls":false,"protocolVersion":"4","keepalive":"60","cleansession":true,"autoUnsubscribe":true,"birthTopic":"","birthQos":"0","birthPayload":"","birthMsg":{},"closeTopic":"","closeQos":"0","closePayload":"","closeMsg":{},"willTopic":"","willQos":"0","willPayload":"","willMsg":{},"userProps":"","sessionExpiry":""},{"id":"4e6203f8b7533ede","type":"ui_tab","name":"ratok","icon":"dashboard","disabled":false,"hidden":false}]
追加部分のみ
[{"id":"06531716839b2117","type":"debug","z":"23af55b50b4cc44f","name":"Debug","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1530,"y":380,"wires":[]},{"id":"b3dcab26f4badb2f","type":"function","z":"23af55b50b4cc44f","name":"データ整理","func":"const data = msg.payload;\nconst jsonData = {\n CompanyID: \"0b60\",\n CO2: msg.co2 + \"ppm\",\n PM1: msg.payload[4] + \"μg/m3\",\n PM2: msg.payload[5] + \"μg/m3\",\n PM3: msg.payload[6] + \"μg/m3\",\n PM4: msg.payload[7] + \"μg/m3\",\n Temperature: msg.temp + \"℃\",\n Humid: msg.payload[10] + \"%\"\n};\nmsg.payload = jsonData;\nreturn msg;","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1370,"y":380,"wires":[["06531716839b2117","4ac248ebe772431f"]]},{"id":"4ac248ebe772431f","type":"mqtt out","z":"23af55b50b4cc44f","name":"","topic":"ratoktest","qos":"","retain":"","respTopic":"","contentType":"","userProps":"","correl":"","expiry":"","broker":"cb1195384d5774a6","x":1540,"y":460,"wires":[]},{"id":"cb1195384d5774a6","type":"mqtt-broker","name":"","broker":"mqtt://public:public@public.cloud.shiftr.io","port":"1883","clientid":"","autoConnect":true,"usetls":false,"protocolVersion":"4","keepalive":"60","cleansession":true,"autoUnsubscribe":true,"birthTopic":"","birthQos":"0","birthPayload":"","birthMsg":{},"closeTopic":"","closeQos":"0","closePayload":"","closeMsg":{},"willTopic":"","willQos":"0","willPayload":"","willMsg":{},"userProps":"","sessionExpiry":""}]
JSON形式のデータを作成します。
各データを個別に格納し、表記したい単位も同時に格納しています。
ComfortClickでデータを受信する
Comfort Click上でMQTTのデータを受信する方法は下記記事を参照してください。
ComfortClickから外部MQTTブローカーにPublishしてメッセージを送る方法
今回は、MQTTのトピックを “ratoctest”にしました。
作成したJSONデータを表示用テキストに落としていきます。
Token Nameにpayload配下のTemperatureやHumidを適用していきます。
UIの適用・可視化
作成した表示用オブジェクトをドラッグアンドドロップします。
使用中のComfort Clickのアドレスにアクセスして確認します。