前回の投稿の続きになります。
また、Bluetooth環境センサーRS-BTEVS1からデータをComfortClick上に可視化と類似した記事です。
MQTTでデータを送信する
Node-REDに送信するデータとMQTT用のフローを追加します。
全体
[{"id":"33d70e22c9e88081","type":"BLE Beacon Scanner","z":"9892657eac51d18d","name":"","x":120,"y":380,"wires":[["5a047c8194dbc24b"]]},{"id":"5a047c8194dbc24b","type":"switch","z":"9892657eac51d18d","name":"MacAddressでfilter","property":"payload.address","propertyType":"msg","rules":[{"t":"eq","v":"fa:ce:09:c4:70:1d","vt":"str"}],"checkall":"true","repair":false,"outputs":1,"x":350,"y":380,"wires":[["d9d5119b52de41ca","c16133fd194d6236","3c45542dd9e57d7b"]]},{"id":"d9d5119b52de41ca","type":"debug","z":"9892657eac51d18d","name":"Debug","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":530,"y":380,"wires":[]},{"id":"c16133fd194d6236","type":"function","z":"9892657eac51d18d","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":420,"y":440,"wires":[["c63a4178e013945a","9808b62ad6efd084"]]},{"id":"9808b62ad6efd084","type":"function","z":"9892657eac51d18d","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":500,"y":500,"wires":[["214c46e17afd8593","89862c16ce1bf864","cc9ff6ef8fea56f6"]]},{"id":"89862c16ce1bf864","type":"function","z":"9892657eac51d18d","name":"データ整理","func":"const data = msg.payload;\nconst jsonData = {\n CompanyID: data.substring(0, 4),\n Relay: data.substring(4, 6),\n Voltage: data.substring(6, 10),\n Current: data.substring(10,14),\n Power: data.substr(14)\n};\nmsg.payload = jsonData;\nreturn msg;","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":550,"y":560,"wires":[["83d1419df816a62d"]]},{"id":"c63a4178e013945a","type":"debug","z":"9892657eac51d18d","name":"Debug2","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":620,"y":440,"wires":[]},{"id":"214c46e17afd8593","type":"debug","z":"9892657eac51d18d","name":"Debug3","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":700,"y":500,"wires":[]},{"id":"83d1419df816a62d","type":"debug","z":"9892657eac51d18d","name":"Debug4","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":740,"y":560,"wires":[]},{"id":"3c45542dd9e57d7b","type":"change","z":"9892657eac51d18d","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"payload.other","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":420,"y":300,"wires":[["b2bfbfa05011dfdc","689b9264a475faa0"]]},{"id":"b2bfbfa05011dfdc","type":"debug","z":"9892657eac51d18d","name":"Debug5","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":620,"y":300,"wires":[]},{"id":"689b9264a475faa0","type":"switch","z":"9892657eac51d18d","name":"リレーの状態","property":"payload[2]","propertyType":"msg","rules":[{"t":"eq","v":"0x01","vt":"num"},{"t":"eq","v":"0x00","vt":"num"}],"checkall":"true","repair":false,"outputs":2,"x":240,"y":140,"wires":[["5306cee084a59eb3"],["b59bc918e109da0c"]]},{"id":"5306cee084a59eb3","type":"change","z":"9892657eac51d18d","name":"","rules":[{"t":"set","p":"relay","pt":"msg","to":"ON","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":450,"y":120,"wires":[["b7d0c77263ccc701","4c7d4c659763cd1a"]]},{"id":"b59bc918e109da0c","type":"change","z":"9892657eac51d18d","name":"","rules":[{"t":"set","p":"relay","pt":"msg","to":"OFF","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":450,"y":160,"wires":[["b7d0c77263ccc701","4c7d4c659763cd1a"]]},{"id":"b7d0c77263ccc701","type":"ui_text","z":"9892657eac51d18d","group":"cf6fb9f48fc8a071","order":6,"width":0,"height":0,"name":"","label":"リレーの状態","format":"{{msg.relay}}","layout":"row-spread","className":"","style":false,"font":"","fontSize":16,"color":"#000000","x":920,"y":140,"wires":[]},{"id":"4c7d4c659763cd1a","type":"function","z":"9892657eac51d18d","name":"電圧の計算","func":"const data = (msg.payload[4]<em>255 + msg.payload[3])/10;\n\nmsg.voltage = data;\nreturn msg;","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":650,"y":220,"wires":[["2cb4cff67669c961","25f2f36e31340b55","9bb07dc2debd9ff5"]]},{"id":"2cb4cff67669c961","type":"debug","z":"9892657eac51d18d","name":"debug 26","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"voltage","targetType":"msg","statusVal":"","statusType":"auto","x":840,"y":200,"wires":[]},{"id":"25f2f36e31340b55","type":"ui_text","z":"9892657eac51d18d","group":"cf6fb9f48fc8a071","order":6,"width":0,"height":0,"name":"","label":"電圧","format":"{{msg.voltage}} V","layout":"row-spread","className":"","style":false,"font":"","fontSize":16,"color":"#000000","x":830,"y":240,"wires":[]},{"id":"9bb07dc2debd9ff5","type":"function","z":"9892657eac51d18d","name":"電流の計算","func":"const data = (msg.payload[6]</em>255 + msg.payload[5])/10;\n\nmsg.current = data;\nreturn msg;","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":810,"y":300,"wires":[["d721a16e5e24606d","071be9c46632f286","3470c16d46fde983"]]},{"id":"d721a16e5e24606d","type":"debug","z":"9892657eac51d18d","name":"debug 27","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"current","targetType":"msg","statusVal":"","statusType":"auto","x":1000,"y":280,"wires":[]},{"id":"071be9c46632f286","type":"ui_text","z":"9892657eac51d18d","group":"cf6fb9f48fc8a071","order":6,"width":0,"height":0,"name":"","label":"電流","format":"{{msg.current}} mA","layout":"row-spread","className":"","style":false,"font":"","fontSize":16,"color":"#000000","x":990,"y":320,"wires":[]},{"id":"3470c16d46fde983","type":"function","z":"9892657eac51d18d","name":"電力の計算","func":"const data = (msg.payload[9] * 255 * 16 + msg.payload[8]*255 + msg.payload[7])/1000;\n\nmsg.power = data;\nreturn msg;","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":970,"y":380,"wires":[["df908ffde1aac32d","6d803af0d65ef315","5423597d4b564df5"]]},{"id":"df908ffde1aac32d","type":"debug","z":"9892657eac51d18d","name":"debug 28","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"power","targetType":"msg","statusVal":"","statusType":"auto","x":1140,"y":360,"wires":[]},{"id":"6d803af0d65ef315","type":"ui_text","z":"9892657eac51d18d","group":"cf6fb9f48fc8a071","order":6,"width":0,"height":0,"name":"","label":"電力","format":"{{msg.power}} W","layout":"row-spread","className":"","style":false,"font":"","fontSize":16,"color":"#000000","x":1130,"y":400,"wires":[]},{"id":"cc9ff6ef8fea56f6","type":"ui_text","z":"9892657eac51d18d","group":"cf6fb9f48fc8a071","order":6,"width":0,"height":0,"name":"","label":"Rawdata","format":"{{msg.payload}}","layout":"row-spread","className":"","style":false,"font":"","fontSize":16,"color":"#000000","x":920,"y":540,"wires":[]},{"id":"a128745f29ced351","type":"mqtt out","z":"9892657eac51d18d","name":"","topic":"ratoc2","qos":"","retain":"","respTopic":"","contentType":"","userProps":"","correl":"","expiry":"","broker":"cb1195384d5774a6","x":1250,"y":500,"wires":[]},{"id":"5423597d4b564df5","type":"function","z":"9892657eac51d18d","name":"データ整理","func":"const data = msg.payload;\nconst jsonData = {\n CompanyID: \"0b60\",\n Relay: msg.relay,\n Voltage: msg.voltage + \"V\",\n Current: msg.current + \"mA\",\n Power: msg.power + \"W\"\n};\nmsg.payload = jsonData;\nreturn msg;","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1090,"y":460,"wires":[["a128745f29ced351","bc3d534e373d1cf6"]]},{"id":"bc3d534e373d1cf6","type":"debug","z":"9892657eac51d18d","name":"debug 29","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1260,"y":460,"wires":[]},{"id":"cf6fb9f48fc8a071","type":"ui_group","name":"ratoc_btwattch2","tab":"4e6203f8b7533ede","order":4,"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":"a128745f29ced351","type":"mqtt out","z":"9892657eac51d18d","name":"","topic":"ratoc2","qos":"","retain":"","respTopic":"","contentType":"","userProps":"","correl":"","expiry":"","broker":"cb1195384d5774a6","x":1250,"y":500,"wires":[]},{"id":"5423597d4b564df5","type":"function","z":"9892657eac51d18d","name":"データ整理","func":"const data = msg.payload;\nconst jsonData = {\n CompanyID: \"0b60\",\n Relay: msg.relay,\n Voltage: msg.voltage + \"V\",\n Current: msg.current + \"mA\",\n Power: msg.power + \"W\"\n};\nmsg.payload = jsonData;\nreturn msg;","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1090,"y":460,"wires":[["a128745f29ced351","bc3d534e373d1cf6"]]},{"id":"bc3d534e373d1cf6","type":"debug","z":"9892657eac51d18d","name":"debug 29","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1260,"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のトピックを “ratoc2”にしました。
作成したJSONデータを表示用テキストに落としていきます。
Token Nameにpayload配下のVoltageやCurrentを適用していきます。
UIの適用・可視化
作成した表示用オブジェクトをドラッグアンドドロップします。
使用中のComfort Clickのアドレスにアクセスして確認します。