1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

M5StackでBLE鯖にしてスマホからメッセージ送ってみる

Last updated at Posted at 2022-08-01

前々からやろうやろう詐欺を自分自身にやりつつ、その重い腰を上げてやってみた時のを備忘録がわりにまた書いてきます。

使用機材

  • M5Stack Gray
  • Android端末
    • BLE Scanner(アプリ)
  • MacBookAir
    • ArduinoIDE(M5Stack関連は設定済みを前提とします。)

BLEって何さ?

BLEはBluetoothのセンサー等での用途で使われるもので、通常のBluetoothよりも消費電力が低いです...と講義のように書いててもアレなので詳しくはググってください..

今回作るもの

M5StackをBLEのセントラル(鯖のイメージ)とし、スマホ(Android端末:BLE Scanner使用)をペリフェラルとして、スマホ側からメッセージを入れて送信すると、M5側で受信したメッセージを表示させるところまでを仕上げていきたいと思います。

M5Stack側のコード

ble_2022.ino
/*
    Based on Neil Kolban example for IDF: https://github.com/nkolban/esp32-snippets/blob/master/cpp_utils/tests/BLE%20Tests/SampleServer.cpp
    Ported to Arduino ESP32 by Evandro Copercini
    updates by chegewara
*/
#include <BLEDevice.h>
#include <BLEUtils.h>
#include <BLEServer.h>
#include <BLEScan.h>
#include <M5Stack.h>

// See the following for generating UUIDs:
// https://www.uuidgenerator.net/

#define SERVICE_UUID        "4fafc201-1fb5-459e-8fcc-c5c9c331914b"
#define CHARACTERISTIC_UUID "beb5483e-36e1-4688-b7f5-ea07361b26a8"

BLEServer* pServer = NULL;
BLECharacteristic* pCharacteristic = NULL;

void setup() {
  Serial.begin(115200);
  M5.begin();
  M5.Lcd.setTextSize(2);
  M5.Lcd.println("BLE start");
  Serial.println("Starting BLE work!");
  // サーバーセット
  BLEDevice::init("m5-york");
  BLEServer *pServer = BLEDevice::createServer();
  BLEService *pService = pServer->createService(SERVICE_UUID);
  pCharacteristic = pService->createCharacteristic(
                                         CHARACTERISTIC_UUID,
                                         BLECharacteristic::PROPERTY_READ |
                                         BLECharacteristic::PROPERTY_WRITE
                                       );
  // サーバー回し始め
  pService->start();
  // BLEAdvertising *pAdvertising = pServer->getAdvertising();  // this still is working for backward compatibility
  BLEAdvertising *pAdvertising = BLEDevice::getAdvertising(); 
  pAdvertising->addServiceUUID(SERVICE_UUID);
  pAdvertising->setScanResponse(true);
  pAdvertising->setMinPreferred(0x06);  // functions that help with iPhone connections issue
  pAdvertising->setMinPreferred(0x12);
  BLEDevice::startAdvertising(); //アドバタイズする
  pCharacteristic->setValue("Hello World!"); //value初期定義
  Serial.println("set BLE server & start advertis!");
  M5.Lcd.println("set BLEperif & advertising");
}

void loop() {
    std::string value = pCharacteristic->getValue();
    M5.Lcd.println(value.c_str()); //M5Stack画面に出力
    Serial.println(value.c_str());
    if(M5.BtnB.wasPressed()){ // 真ん中ボタンでM5Stack内画面消去
      M5.Lcd.setCursor(0, 0);
      M5.Lcd.fillScreen(TFT_BLACK);
    }
  delay(2000);
}

BLEサーバーのサンプルスケッチ(ESP32 BLE Arduino→BLE_server)から少し書き換えました。サンプルのままだとサーバーなどのものがloopの中で使えないので外に出してあげたり、M5Stackへの画面出力等を足してあげました。

出力結果

まず、こちらがAndroid/BLE Scanner側のスクショ。画面内のマルで"W"と書かれたところをタップすると、右の画面となり、OKでM5Stack側へ送信となります。

スマホ側からデータを送ると、M5Stack側では以下のように出力されます。シリアルのとM5Stack画面内のものを載せておきます。

スクリーンショット 2022-08-01 17.15.26.png
DSC_2738.JPG

自分の経験が少ないせいもありますが、M5Stack内画面で、ある程度出力したら画面をリセットする動作も実装したかったのですが、少々うまくいかず放置して代わりにボタン操作で消せるようにしました。

まあ今回、自分の持ってるM5StackにはBLEが入ってること自体は前々から知っていたものの、中々実際に動かすことまでする気力が出なかったので、いい経験になったのと今後何かを作る際のハードルひとつ程度、消せたと思われる..?

参考

1
0
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
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?