LoginSignup
3
3

More than 5 years have passed since last update.

BLESerial2を使って、Arduinoからセンサーデータを読みだしてみる

Last updated at Posted at 2016-07-17

はじめに

Arduinoでデータを読みだすのは簡単ですが、それをどうやって別の端末に転送するか、という問題がありますよね。
もちろんUSB経由のSerialでも、距離的に繋がるときはいいんですけど、そうでない場合も多いわけで。
そんな時に浅草ギ研様のBLESerial2を見つけたので、とりあえず使ってみようと思いその記録です。
http://www.robotsfx.com/robot/BLESerial2.html

念のため書いておきますと、BLEとか無線に対する基本的な知識はないです。

Arduinoとの接続

ここはいちいち書きません。
浅草ギ研様のページに書いてありますので、そちらを参照してください。
http://www.robotsfx.com/robot/img/radio/BLESerial/BLESerial2_how2.html

コード

まずはArduinoのスケッチです。
センサとかつなぐの面倒だったので、定期的にメッセージを送るだけです。
まぁ、その部分をセンサ入力に変えればいいだけなので、そこは読み替えてください。

void setup() {
  Serial.begin(9600);
}

void loop() {
  Serial.write("hello");
  delay(1000);
}

次に受け手側のスケッチです。

浅草ギ研様のサイトにはiPhoneとかの接続例とコードはあるんですが、最終的にはRaspiとかと繋ぎたいのでこのコード例ではそのまま使えません。
正直言語はなんでもいいんですが、はやりのnodejsを使うことにします。

ちなみに今回は、ちょっと家にいないので、手元のMacbook Proから接続してテストしています。

node.js
var noble = require("noble");
noble.on("stateChange", function(state) {
  console.log("on -> stateChange" + state);
  if (state == "poweredOn") {
    noble.startScanning([],false, function (err) { console.log(err); });
  }
  else {
    noble.stopScanning();
  }
});

noble.on("scanStart", function() {
  console.log("on -> scanStart");
});

noble.on("scanStop", function() {
  console.log("on -> scanStop");
});

noble.on("discover", function(peripheral) {
  console.log("on -> discover: " + peripheral);
  noble.stopScanning();
  peripheral.on("connect", function () {
    console.log("on -> connect");
    this.discoverServices();
  });
  peripheral.on("disconnect", function () {
    console.log("on -> disconnect");
  });

  peripheral.on("servicesDiscover", function(services) {
    console.log("on -> services discover " + services);
    for (var i = 0; i < services.length; i ++) {
      services[i].on("includedServicesDiscover", function (includedServiceUUIDs) {
        console.log("on -> service included services discovered " + includedServiceUUIDs);
        this.discoverCharacteristics();
      });
      services[i].on("characteristicsDiscover", function (characteristics) {
        console.log("on -> characteristics discover " + characteristics);
        for (var i = 0; i < characteristics.length; i ++) {
            characteristics[i].on("data", function (data, isNotification) {
              console.log("data: " + data);
            });
            characteristics[i].subscribe();
        }
      });
      services[i].discoverIncludedServices();
    }
  });
  peripheral.connect();
});

BLESerial2の仕様

こう書いてしまうと何でも簡単にデータが送れそうに見えちゃいますが、それなりに制限もあって一度に15byteしか送れません。
15byteを超えた文字(例えば先程のSerial.writeの中身を1234567890abcdef)を送ろうとすると下記のようになります。

スクリーンショット 2016-07-17 18.31.49.png

最初数回はオーバーする箇所(f)が次の回に回ったり、手前の数文字が次の回に回ってたりしますが、安定してくると、f以降は切られるみたいですね。

15byteを超える場合は20ms以降開けてから送信する必要があるみたいですので、delayで適切に開けて送信すればよいですね。

最後に

とりあえず通信はできました。簡単でした。
ちなみに、一番大変だったのはハンダ付けで、10年ぶりにハンダ付けをしたものが、この小さな基盤だったので案の定ミスりました。

3
3
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
3