LoginSignup
0

More than 1 year has passed since last update.

posted at

updated at

M5Stackを使ったBLEスキャナーを作る

はじめに

この記事はTDU CPSLab Advent Calendar 2021の5日目の記事です。
大遅刻してしまいました。ごめんなさい。orz

なにやるの?

M5Stack(esp32)でBLEを使ってRSSI取りたいと思います。
esp32でBluetoothを使う時には一般的にはESP32_BLE_Arduinoを使います。
このソースを読むのはとても勉強になるのでお勧めです。

どうしてやるの?

研究でBLEを使った接近判定をしてみたかったので・・・・

ソースコード

名前をもつBLEデバイスが周囲に存在しているときにシリアルでRSSIを出すソースがこちらになります。

main.cpp
#include <M5Stack.h>
#include <BLEDevice.h>

BLEScan* pBLEScan;

class testAdvertisedCallBacks: public BLEAdvertisedDeviceCallbacks {
  void onResult(BLEAdvertisedDevice advertisedDevice){
    int rssi = advertisedDevice.getRSSI();
    if(advertisedDevice.haveName()){
      Serial.println("Show DeviceName");
      Serial.println(advertisedDevice.getName().c_str());
      Serial.println(rssi);
      Serial.println();
    }
  };
};

void setup() {
  M5.begin();
  M5.Lcd.setTextSize(3);
  M5.Lcd.println("GW");
  BLEDevice::init("M5Stack-GW");
  pBLEScan = BLEDevice::getScan(); //scan object 
  pBLEScan->setAdvertisedDeviceCallbacks(new testAdvertisedCallBacks());
  pBLEScan->setActiveScan(true);
}

void loop() {
  BLEScanResults foundDevices = pBLEScan->start(10,false);
  Serial.println("Devices Founded!");
  Serial.println(foundDevices.getCount());
  Serial.println("scan done!");
  delay(2000);
}

ビーコンについて

BLEビーコンについてはこちらのサンプルを使わせていただきます・・・
デバイス名が"Long name works now"になります

動作結果

image.png

おわりに

まーじで記事を書く余裕がない・・・ 
あんまりM5を使ったBLEの記事は少なかったので書いてみました! よければ参考にしてみてください

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
What you can do with signing up
0