Bluetooth環境センサーRS-BTEVS1からデータをNode-REDで可視化 の記事と類似した内容です。
使用機器
Bluetoothワットチェッカー RS-BTWATTCH2(ラトックシステム株式会社)
繋いだコンセントのON/OFF切り替えや、電圧・電流・電力を測定できるデバイスです。
BLEのレシーバーを使用して受信したデータをNode-REDで可視化していきます。
機器の接続
コンセントの間に入るよう接続します。再度のON/OFFボタンで給電の切り替えができます。
今回はPCの充電器を接続しています。
MACアドレスはON/OFFボタンの反対側に記載されています。
計測データの仕様をpdfから引用してきました。
ノードの作成
[{"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-&gt;Array","func":"// input変数にはInjectノードからのメッセージが含まれます\nvar inputObject = msg.payload.other; // input.payloadはオブジェクトと仮定\n\nif (typeof inputObject === 'object' &amp;&amp; inputObject !== null) {\n // オブジェクトのキーを取得し、それを使ってArrayを生成\n var keys = Object.keys(inputObject);\n var outputArray = keys.map(key =&gt; 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 &lt; 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":"cf6fb9f48fc8a071","type":"ui_group","name":"ratoc_btwattch2","tab":"4e6203f8b7533ede","order":4,"disp":true,"width":"6","collapse":false,"className":""},{"id":"4e6203f8b7533ede","type":"ui_tab","name":"ratok","icon":"dashboard","disabled":false,"hidden":false}]
まずは、BLE Beacon ScannarからRS-BTWATTCH2のMACアドレスでフィルターをかけ、データを絞り込みます。
本来は不要ですが、ここではRS-BTWATTCH2の仕様書通りに16進数の文字列等(生データ)を作成しています。
残りのノードでは、実際に各データを仕様書通りに読み分け、計算の必要な個所ではfunctionノードで処理をしています。
それぞれダッシュボード上に表示させています。
ダッシュボードでの表示
各データがこのように可視化されます。