Node-REDとWireSharkでBLEセンサーのMACアドレスを調べる方法

IoTのアプリケーションを作る場合、顧客から指定をうけたBLEセンサーのデータを受信するケースがありますが、BLEセンサーのMACアドレスを確認することができないケースがあります。

Node-REDでBLEを受信する場合は、BLE BeaconScannerを使っていますが、事務所空間には数多くのBLEデータが飛び交っているので、その中から該当のBLEセンサーデータを見つけるのはけっこう大変です。

そこで、BLEデータのデータ長でフィルタリングをかけMACアドレスの一覧を作成することができれば、あとはWireSharkを使ってManufactureIDを確認するという作業も大幅に楽になります。

この記事ではそのやり方を紹介いたします。

Node-REDのフロー

Image from Gyazo

[{"id":"341745b1b2703890","type":"BLE Beacon Scanner","z":"451463cf5af94d7d","name":"","x":220,"y":160,"wires":[["ef2acfc9d267773c"]]},{"id":"ef2acfc9d267773c","type":"function","z":"451463cf5af94d7d","name":"ChkDataLength","func":"if (msg.payload.hasOwnProperty(\"other\")){\n    const data = msg.payload.other;\n\n    const len = data.length;\n\n    msg.len = len;\n\n    return msg;\n}\n\n\n\n\n","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":340,"y":240,"wires":[["0e66ccbad23a4138"]]},{"id":"0e66ccbad23a4138","type":"switch","z":"451463cf5af94d7d","name":"長さ20以上","property":"len","propertyType":"msg","rules":[{"t":"gte","v":"20","vt":"num"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":450,"y":320,"wires":[["06178776e5aa9e3e"],[]]},{"id":"06178776e5aa9e3e","type":"switch","z":"451463cf5af94d7d","name":"長さ27以下","property":"len","propertyType":"msg","rules":[{"t":"lt","v":"27","vt":"num"}],"checkall":"true","repair":false,"outputs":1,"x":650,"y":320,"wires":[["d0719c0b2f99e7dc","222257f3b7d8ca66"]]},{"id":"222257f3b7d8ca66","type":"debug","z":"451463cf5af94d7d","name":"Debug","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":830,"y":320,"wires":[]},{"id":"d0719c0b2f99e7dc","type":"function","z":"451463cf5af94d7d","name":"重複除去","func":"let ids = flow.get(\"uniqueIds\") || [];\n\nlet payload = msg.payload;\nlet id = payload.id;\n\nif (ids.includes(id)) {\n    // 重複がある場合、メッセージを破棄\n    return null;\n} else {\n    // 重複がない場合、idを記録してメッセージを次のノードに渡す\n    ids.push(id);\n    flow.set(\"uniqueIds\", ids);\n    return msg;\n}\n","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":820,"y":400,"wires":[[]]},{"id":"67519b2a349ad0ae","type":"function","z":"451463cf5af94d7d","name":"ID初期化","func":"var uniqueIds =\"\";\n\nflow.set(\"uniqueIds\", uniqueIds);\n","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1000,"y":80,"wires":[[]]},{"id":"7ddd107bbd81fa93","type":"inject","z":"451463cf5af94d7d","name":"ID初期化","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":840,"y":80,"wires":[["67519b2a349ad0ae"]]},{"id":"d519606b0e37ef15","type":"inject","z":"451463cf5af94d7d","name":"MAC出力","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":840,"y":160,"wires":[["b94f765d093dfbb3"]]},{"id":"b94f765d093dfbb3","type":"function","z":"451463cf5af94d7d","name":"uniqueIdsを取り出す","func":"let ids = flow.get(\"uniqueIds\");\n\nmsg.payload = ids;\n\nreturn msg;\n\n","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1000,"y":220,"wires":[["2edddcde89038874"]]},{"id":"208ff81d5f52b657","type":"debug","z":"451463cf5af94d7d","name":"debug 23","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1260,"y":360,"wires":[]},{"id":"2edddcde89038874","type":"function","z":"451463cf5af94d7d","name":"MACアドレス一覧出力","func":"let macList = msg.payload;\nlet formattedMacList = macList.map(mac => {\n    return mac.match(/.{1,2}/g).join(\":\");\n});\nmsg.payload = formattedMacList;\nreturn msg;\n","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1140,"y":280,"wires":[["208ff81d5f52b657"]]}]

Image from Gyazo

まずは、こちらのフローでBLE信号を受信、データの長さをLENというデータに格納。

MACアドレスを調べたいBLEセンサーのデータの長さを、おおよそ絞り込み、該当するMACアドレスの重複を削除した一覧をuniqueIdsとしてflowに保存します。

Image from Gyazo

ID初期化はflowのuniqueIdsを初期化。

MAC出力はuniqueIdsの一覧を出力します。

WireSharkで確認

先に取得したMACアドレス一覧を、WireSharkのadvertising addressでフィルタリングします。

btle.advertising_address == c5:a2:65:80:b7:56

WireShark側の設定は下記記事を参照。

ABLIC社のバッテリレス漏水センサCLEAN-BoostのデータをNode-REDで受信する方法 – スマートライト株式会社

表示されたデータのManufactureIDを確認し、探しているBLEセンサーであれば成功。

もし違う場合は、別のMACアドレスで同じことをしてManufactureIDを確認します。