LoginSignup
3
1

More than 5 years have passed since last update.

Node.jsでParrotのミニドローンのBLE情報を探す

Last updated at Posted at 2017-11-04

RESASハッカソン 岩手で記事書いてます。 #RESAS

ドローンをNode.jsから制御する記事を過去に書きましたが、コードが古くなってきたので書き直します。

ノンプログラマでもコピペでOK!JavaScriptを使ってDroneを飛ばそう

現行のParrotのミニドローンMamboを探します。

筆者の環境

  • Node.js: v9.0.0
  • Mac: 10.12.6 Sierra

準備

npm init -y
npm i --save noble

コード

scan.js
'use strict';

const noble = require('noble');
const MINIDRONES_IDENTIFER = ['Maclan_','Mambo_','RS_']; //対応機種によって増えていく
let counter = 0;

//ParrotのMinidroneを見つける
const findParrotDrone = (localName) => {
    if(localName === undefined)return false;
    for(let i = 0, len = MINIDRONES_IDENTIFER.length; i < 10; i++){
        if(localName.indexOf(MINIDRONES_IDENTIFER[i]) === 0){
            return true;
        }
    }
    return false;
}

//周辺のBLE機器を検索
const startScan = () => {
    noble.startScanning();
    noble.on('discover', (peripheral) => {
        counter++;
        let mes = '';
        if (!('localName' in peripheral.advertisement)) return; //localNameがあるか判断
        if(findParrotDrone(peripheral.advertisement.localName)) mes += `発見→`; //localNameでParrotのデバイスを検索
        mes += `${counter}台目: ${peripheral.advertisement.localName} | ${peripheral.uuid} | RSSI ${peripheral.rssi}`;
        console.log(mes);
    });
}

if (noble.state === 'poweredOn') {
    startScan();
} else {
    noble.on('stateChange', startScan);
}

実行

Mamboの電源を付けておきましょう。

$ node scan.js

1台目: undefined | xxxxxxxxxxxxxxxxxxxxxxxxxx | RSSI -62
2台目: undefined | xxxxxxxxxxxxxxxxxxxxxxxxxx | RSSI -63
発見→3台目: Mambo_XXXXX | zzzzzzzzzzzzzzzzzzzzzz | RSSI -62
4台目: undefined | xxxxxxxxxxxxxxxxxxxxxxxxxx | RSSI -62

zzzzzzzzzzzzzzzzzzzzzzと書いているところがドローンのUUIDです。

3
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
1