HOME
  Security
   Software
    Hardware
  
FPGA
  CPU
   Android
    Raspberry Pi
  
nLite
  Xcode
   etc.
    ALL
  
LINK
BACK
 

2017/08/11

Raspberry Pi 3に Nodejs blenoをインストールして Bluetooth BLEのペリフェラルを作成する Raspberry Pi 3に Nodejs blenoをインストールして Bluetooth BLEのペリフェラルを作成する

(ラズパイ3と Node.js blenoで Bluetooth BLEの周辺機器機能を実装する)

Tags: [Raspberry Pi], [電子工作]





● Raspberry Pi 3 Model Bを遂に購入

 Raspberry Pi3 Model B RPI2 RPI3

 大人気の CPUボードの Raspberry Piに WiFiと Bluetoothが搭載されたモデルが新発売となりました。
 以前から Raspberry Pi 2を買おうかどうか迷っていましたが、Raspberry Pi 3 Model Bの発売を機に購入を決意してラズベリアンになる事にしました。

 ※ ラズパイの OS Raspbianはバージョンが上がる毎に過去の版と OSの内部の作りが変わり、過去に書かれた製作記事(例えば Raspbian Wheezyの時代の記事)がそのままではエラーが出たりして動かない事が有ります。
 ※ 当方のホームページのラズパイ記事は全て Raspberry Pi 3 Model Bと Raspbian Jessieの組み合わせで動作確認をしております。
(ただし、将来的に新しい Raspbian OSが出た場合に、当方の Raspbian Jessieを基にした内容がそのままでは動かない可能性が有ります。)
 ※ 2017/08/16から Raspbian OSは Raspbian Jessieから Raspbian Stretchに変わりました。
 ※ 2019/06/20から Raspbian OSは Raspbian Stretchから Raspbian Busterに変わりました。

Download Raspbian for Raspberry Pi

ちなみに、歴代のバージョンと名称は
Debianコードネーム年月備考(参考)Ubuntuでの該当名称
Debian 11Bullseye2021/08/14~2021/11からラズパイにリリースFocal Fossa 20.04 LTS ?
Debian 10Buster2019/06/20~2019/06からラズパイ4対応Bionic 18.04 LTS
Debian 9Stretch2017/08/16~2018/03からラズパイ3B+対応Xenial 16.04 LTS
Debian 8Jessie2015~2016/02からラズパイ3対応Trusty 14.04 LTS
Debian 7Wheezy2013~2016
Debian 6.0Squeeze2011~2014
Debian GNU/Linux 5.0Lenny2009~2012


●ラズパイ3 + Node.js blenoで Bluetooth BLEのペリフェラルを作成する

 ラズパイ3の BLE + Node.jsで擬似環境を作成する必要性が出てきました。
 ここでは BLEや Node.jsに慣れる為にサンプルを動かして BLEと Node.jsに対して免疫をつけていきます。

 ラズパイで BLEをプログラムから自由に扱う手段としては Node.jsを使う方法が一般的な様です。
 ここでも Node.jsを使って Bluetooth BLEのペリフェラルを作成します。

 BLE = Bluetooth Low Energy
 bleno = BLE Peripheral(スレーブ、周辺機器側)
sandeepmistry/bleno - A Node.js module for implementing BLE (Bluetooth Low Energy) peripherals

 noble = BLE Central(マスター、ホスト機器側)
sandeepmistry/noble - A Node.js BLE (Bluetooth Low Energy) central module


●ラズパイ3に Node.jsをインストールする方法

・ uname -aで環境を確認する。
 (RASPBIAN STRETCH WITH DESKTOP Release date:2017-08-16)
$ uname -a
Linux raspberrypi 4.9.41-v7+ #1023 SMP Tue Aug 8 16:00:15 BST 2017 armv7l GNU/Linux

$ nodejs -v
v4.8.2

$ npm -v
-bash: npm: command not found

 2017/08版の Raspbian OSには Node.js v4.8.2がインストールされています。
 が、不要なので削除して Node.jsをインストールします。

● Node.js v7

 Node.js v7の場合は下記の通り。(BLE blenoライブラリの動作を確認)
# autoremove nodejsで既存の nodejsを削除する。
sudo apt-get -y autoremove nodejs

# setup Node.js v7
curl -sL https://deb.nodesource.com/setup_7.x | sudo -E bash -
# install Node.js v7
sudo apt-get -y install nodejs

# node -vでバージョンを確認する。
# $ node -v
# v7.10.1 (node-v51 ABI)

# npm -vでバージョンを確認する。
# $ npm -v
# 4.2.0

● Node.js v6

 Node.js v6の場合は下記の通り。(BLE blenoライブラリの動作を確認)
# autoremove nodejsで既存の nodejsを削除する。
sudo apt-get -y autoremove nodejs

# setup Node.js v6
curl -sL https://deb.nodesource.com/setup_6.x | sudo -E bash -
# install Node.js v6
sudo apt-get -y install nodejs

# node -vでバージョンを確認する。
# $ node -v
# v6.11.2 (node-v48 ABI)

# npm -vでバージョンを確認する。
# $ npm -v
# 3.10.10


●ラズパイ3に Node.jsの BLE blenoライブラリをインストールする方法

 npm install blenoで Node.jsの BLE blenoライブラリをインストールします。
npm install bleno

# インストールの途中で下記のエラーが出ますが、発生原因や対処方法等が不明なので放置します。
# node-pre-gyp ERR! Tried to download: https://github.com/tessel/node-usb/releases/download/1.2.0/usb_bindings-v1.2.0-node-v51-linux-arm.tar.gz
# node-pre-gyp ERR! Pre-built binaries not found for usb@1.2.0 and node@7.10.1 (node-v51 ABI) (falling back to source compile with node-gyp)

# /home/pi
# └─┬ bleno@0.4.2
#   ├─┬ bluetooth-hci-socket@0.5.1
#   │ └── nan@2.6.2
#   ├── bplist-parser@0.0.6
#   └─┬ debug@2.6.8
#       └── ms@2.0.0

# blenoインストール後は ./node_modules/ディレクトリが作成されています。
cd
ls -l ./node_modules/

# blenoのサンプルを gitからダウンロードします。
cd
git clone https://github.com/sandeepmistry/bleno

・ Bluetooth BLE blenoの battery-serviceのサンプルを動かす
cd
cd ./bleno/examples/battery-service

# sudo node main.jsでサンプルを動かす
sudo node main.js

# on -> stateChange: poweredOn
# on -> advertisingStart: success
# setServices: success

・ Bluetooth BLE blenoの echoのサンプルを動かす
cd
cd ./bleno/examples/echo

# sudo node main.jsでサンプルを動かす
sudo node main.js

# bleno - echo
# on -> stateChange: poweredOn
# on -> advertisingStart: success

・ pizzaのサンプルを動かす
cd
cd ./bleno/examples/pizza

# sudo node pizza.jsでサンプルを動かす
sudo node pizza.js

# 何がどうなるのか不明

・ blink1のサンプルを動かす
cd
cd ./bleno/examples/blink1

# sudo node main.jsでサンプルを動かす
sudo node main.js

# Error: Cannot find module 'node-blink1'


● Androidスマホに BLEセントラルのアプリをインストールする

BLE Scanner: Read、Write、Notify - Bluepixel Technology LLP
com.macdom.ble.blescanner
アップデート 2017年3月17日
現在のバージョン 3.12

 BLE Scannerアプリを Androidスマホにインストールします。

・Android BLEセントラルのアプリ BLE Scanner
Android BLEセントラルのアプリ BLE Scanner


・Android BLEセントラルのアプリ BLE Scanner
Android BLEセントラルのアプリ BLE Scanner


・Android BLEセントラルのアプリ BLE Scanner
Android BLEセントラルのアプリ BLE Scanner



 Asus Z200や LG nexus 5Xではラズパイ3の blenoに BLE接続(Connect)しますが、XiaoMi Mi4cはラズパイ3の blenoに BLE接続(Connect)に失敗します。

 XiaoMi Mi4cの BLEは TIの SensotTagには問題無く接続するので、ラズパイ3の BLEに相性が有るのか、blenoで作成するペリフェラル定義に何か不足が有るのか、何らかの相性が有ると思われます。


● blenoのサンプルを作成してみる

● Bluetooth BLE blenoの beacon.js iBeaconのサンプル

nano beacon.js
sudo node beacon.js
var bleno = require('bleno');

var uuid = 'e2c56db5dffb48d2b060d0f5a71096e0';
var major = 0x1234; // 0x0000 - 0xffff
var minor = 0xabcd; // 0x0000 - 0xffff
var measuredPower = -123; // -128 - 127

bleno.on('stateChange', function(state) {
  console.log('bleno.on -> stateChange: ' + state);
  if (state === 'poweredOn') {
    bleno.startAdvertisingIBeacon(uuid, major, minor, measuredPower);
  }
});

・Bluetooth BLE blenoの beacon.js iBeaconのサンプル
Bluetooth BLE blenoの beacon.js iBeaconのサンプル


・Bluetooth BLE blenoの beacon.js iBeaconのサンプル
Bluetooth BLE blenoの beacon.js iBeaconのサンプル


・Bluetooth BLE blenoの beacon.js iBeaconのサンプル
Bluetooth BLE blenoの beacon.js iBeaconのサンプル



● Bluetooth BLE blenoの battery.js バッテリー残量のサンプル

nano battery.js
sudo node battery.js
var bleno = require('bleno');

var name = 'pi_bleno_battery';
var serviceUUID = '180F';

bleno.on('stateChange', function(state) {
  console.log('bleno.on -> stateChange: ' + state);
  if (state === 'poweredOn') {
    bleno.startAdvertising(name, [serviceUUID]);
  } else {
    bleno.stopAdvertising();
  }
});

bleno.on('advertisingStart', function(error) {
  console.log('bleno.on -> advertisingStart: ' + error);
  if (!error) {
    bleno.setServices([
        new bleno.PrimaryService({
          uuid : serviceUUID,
          characteristics: [
            new bleno.Characteristic({
              uuid: '2A19',
              properties: ['read'],
              value: new Buffer([78]) // [78] = 78%
              // value: new Buffer([100]) // [100] = 100%
            })
          ]
        })
      ]
    );
  } else {
    console.error(error);
  }
});

・Bluetooth BLE blenoの battery.js バッテリー残量のサンプル
Bluetooth BLE blenoの battery.js バッテリー残量のサンプル


・Bluetooth BLE blenoの battery.js バッテリー残量のサンプル
Bluetooth BLE blenoの battery.js バッテリー残量のサンプル



● Bluetooth BLE blenoの notify.js Notifyのサンプル

nano notify.js
sudo node notify.js
var bleno = require('bleno');

var name = 'pi_bleno_notify';
var serviceUUID = 'fff0';

bleno.on('stateChange', function(state) {
  console.log('bleno.on -> stateChange: ' + state);
  if (state === 'poweredOn') {
    bleno.startAdvertising(name, [serviceUUID]);
  } else {
    bleno.stopAdvertising();
  }
});

bleno.on('advertisingStart', function(error) {
  console.log('bleno.on -> advertisingStart: ' + error);
  if (!error) {
    bleno.setServices([
        new bleno.PrimaryService({
          uuid : serviceUUID,
          characteristics: [
            new bleno.Characteristic({
              uuid: 'fff1',
              properties: ['notify', 'write'],
              descriptors: [
                new bleno.Descriptor({
                  uuid: '2901',
                  value: 'notify value message'
                })
              ]
            })
          ]
        })
      ]
    );
  } else {
    console.error(error);
  }
});

・Bluetooth BLE blenoの notify.js Notifyのサンプル
Bluetooth BLE blenoの notify.js Notifyのサンプル


・Bluetooth BLE blenoの notify.js Notifyのサンプル
Bluetooth BLE blenoの notify.js Notifyのサンプル


・Bluetooth BLE blenoの notify.js Notifyのサンプル
Bluetooth BLE blenoの notify.js Notifyのサンプル


・Bluetooth BLE blenoの notify.js Notifyのサンプル
Bluetooth BLE blenoの notify.js Notifyのサンプル




● npm install blenoでインストールされるモジュール郡

 npm install blenoでインストールされるモジュール郡
$ ls -l ./node_modules/
total 24
drwxr-xr-x 5 pi pi 4096 Aug  7 10:07 bleno
drwxr-xr-x 6 pi pi 4096 Aug  7 10:08 bluetooth-hci-socket
drwxr-xr-x 3 pi pi 4096 Aug  7 10:07 bplist-parser
drwxr-xr-x 3 pi pi 4096 Aug  7 10:07 debug
drwxr-xr-x 2 pi pi 4096 Aug  7 10:07 ms
drwxr-xr-x 4 pi pi 4096 Aug  7 10:07 nan

# git clone https://github.com/sandeepmistry/bleno
$ cd
$ git clone https://github.com/sandeepmistry/bleno
$ cd ./bleno/examples
$ ls -l
total 16
drwxr-xr-x 2 pi pi 4096 Aug  7 10:10 battery-service
drwxr-xr-x 2 pi pi 4096 Aug  7 10:10 blink1
drwxr-xr-x 2 pi pi 4096 Aug  7 10:10 echo
drwxr-xr-x 2 pi pi 4096 Aug  7 10:10 pizza



Tags: [Raspberry Pi], [電子工作]

●関連するコンテンツ(この記事を読んだ人は、次の記事も読んでいます)

TIの 8051マイコン内蔵の BLE CC2540/CC2541を使ってみる
TIの 8051マイコン内蔵の BLE CC2540/CC2541を使ってみる

  Bluetooth Low Energyの SensorTagや iBeacon、CC Debuggerの使い方など

Raspberry Pi 3の Bluetooth BLEで TI SensorTagと接続してセンサーの測定値を読み取る方法
Raspberry Pi 3の Bluetooth BLEで TI SensorTagと接続してセンサーの測定値を読み取る方法

  ラズパイ3の BLEと Node.jsで、TIの SensorTag CC2541DKに接続してセンサーの状態を取得

Raspberry Pi 3の gatttoolのコマンドラインで TIの SensorTagを直接操作する方法
Raspberry Pi 3の gatttoolのコマンドラインで TIの SensorTagを直接操作する方法

  ラズパイ3と gatttoolのコマンドラインで TIの SensorTagに直接接続して gatttoolの使い方を覚える

Raspberry Pi 3の Nodejs blenoで BLE接続して GPIOで Lチカ制御やボタン状態を読み取る
Raspberry Pi 3の Nodejs blenoで BLE接続して GPIOで Lチカ制御やボタン状態を読み取る

  ラズパイ3と Node.js blenoで BLEの周辺機器機能を実装する GPIO編

Raspberry Pi 3の Python BLE pygattlibライブラリで TIの SensorTagに接続して制御する方法
Raspberry Pi 3の Python BLE pygattlibライブラリで TIの SensorTagに接続して制御する方法

  ラズパイ3と Python BLE pygattlibで TIの SensorTagのセンサーの値を取得とボタン状態の通知を受信する

Raspberry Pi 3に Bluetooth BlueZ Version 5.42 BLE
Raspberry Pi 3に Bluetooth BlueZ Version 5.42 BLE

  ラズパイで Bluetooth 4.0の BLE gatt通信を行なう TIの SensorTagや iBeacon実験など

Amazon Dash Buttonをハックして IoTボタンとして使う方法を解説
Amazon Dash Buttonをハックして IoTボタンとして使う方法を解説

  Amazon Dash Buttonをハック。ARP要求を監視、DNSハック、IPアドレスハック等。

Raspberry Piで Amazon Dash Buttonを自在にハックする方法。node-dash-button方法
Raspberry Piで Amazon Dash Buttonを自在にハックする方法。node-dash-button方法

  ラズパイ + Node.jsで Amazon Dash Buttonをハックして IoTボタンとして使う方法を解説

Raspberry Piで Amazon Dash Buttonを自在にハックする方法。tcpdump libpcap方法
Raspberry Piで Amazon Dash Buttonを自在にハックする方法。tcpdump libpcap方法

  ラズパイ + libpcap Packet Capture libraryで Amazon Dash Buttonをハックして IoTする

Raspberry Piで Amazon Dash Buttonを自在にハックする方法。Python Scapyライブラリ方法
Raspberry Piで Amazon Dash Buttonを自在にハックする方法。Python Scapyライブラリ方法

  ラズパイ + Python Scapyライブラリで ARPパケット検出からの Amazon Dash Buttonをハック

Raspberry Piで Amazon Dash Buttonを自在にハックする方法。Node.js Dasher方法
Raspberry Piで Amazon Dash Buttonを自在にハックする方法。Node.js Dasher方法

  ラズパイ + Node.js Dasherライブラリで ARPパケット検出からの Amazon Dash Buttonをハック

Raspberry Pi 3で安定して使える相性の無い最適な microSDカードの種類のまとめ
Raspberry Pi 3で安定して使える相性の無い最適な microSDカードの種類のまとめ

  ラズパイ3で安定して使える microSDカードを購入する Teamと SanDiskは絶対に買わない

Raspberry Pi 3の Linuxコンソール上で使用する各種コマンドまとめ
Raspberry Pi 3の Linuxコンソール上で使用する各種コマンドまとめ

  ラズパイの Raspbian OSのコマンドラインで使用する便利コマンド、負荷試験や CPUシリアル番号の確認方法等も

Raspberry Pi 3で GPIO端子の I2C機能を有効化する方法
Raspberry Pi 3で GPIO端子の I2C機能を有効化する方法

  ラズパイ3の GPIO端子の I2C機能を有効にして各種センサーを繋げる方法まとめ




[HOME] | [BACK]
リンクフリー(連絡不要、ただしトップページ以外は Web構成の変更で移動する場合があります)
Copyright (c) 2017 FREE WING,Y.Sakamoto
Powered by 猫屋敷工房 & HTML Generator

http://www.neko.ne.jp/~freewing/raspberry_pi/raspberry_pi_nodejs_ble_bleno_peripheral/