M5StickCPlusでシリアル通信した際のメモその2
前回はUSBを使った有線接続を使ったが、今回はBluetoothを使った無線接続でシリアル通信をする
ライブラリ
BluetoothSerial
コード
#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); } }
Serial
とBluetoothSerial
はほぼ同じIFなので、前回とコードはほぼ同じ
動作確認
BluetoothSerial.begin
で指定したデバイスとペアリング- ArduinoIDEの「ツール」->「シリアル」から、接続したM5StickCPlusを選択
- シリアルモニタを起動
あとは前回のシリアル通信と同じ手順でOK