modalsoul’s blog

これは“失敗”と呼べるかもしれないが、ぼくは“学習体験”と呼びたい

M5StickCPlusとBluetooth接続でシリアル通信

M5StickCPlusでシリアル通信した際のメモその2


前回はUSBを使った有線接続を使ったが、今回はBluetoothを使った無線接続でシリアル通信をする

modalsoul.hatenablog.com

ライブラリ

BluetoothSerial

github.com

コード

#include <M5StickCPlus.h>
#include "BluetoothSerial.h"

BluetoothSerial SerialBT;

void setup() {
  M5.begin();
  M5.Lcd.setTextFont(1);
  M5.Lcd.setCursor(0, 0, 2); 
  M5.Lcd.println("Serial sample.");
  SerialBT.begin("M5StickCPlusBT");
}

void loop() {
  if(SerialBT.available()) {
    String msg = SerialBT.readString();
    M5.Lcd.print("Received:" + msg);
    SerialBT.print(msg);
  }
}

SerialBluetoothSerialはほぼ同じIFなので、前回とコードはほぼ同じ

動作確認

  • BluetoothSerial.beginで指定したデバイスとペアリング
  • ArduinoIDEの「ツール」->「シリアル」から、接続したM5StickCPlusを選択
  • シリアルモニタを起動

あとは前回のシリアル通信と同じ手順でOK