SSブログ

M5Stack同士でシリアル通信 [Arduino&M5Stack]

今回は、M5Stack同しでシリアル通信をして、文字列を送信&受信してみました

参考
http://robooptions.blog.fc2.com/blog-entry-9.html

この場合はM5StickCでしたが、M5Stackでやってみました。今はまだシリアル通信でしかできないけど、そのうち、CardKB(M5StackのGroveキーボード)やディスプレイ表示でやりたいです。

スレーブのBluetooth用MACアドレスを調べる
スレーブのM5Stackにこのコードを記述
void setup(void) {
  Serial.begin(115200);
  Serial.println("-----------------");
  uint8_t macBT[6];
  esp_read_mac(macBT, ESP_MAC_BT);
  Serial.printf("%02X:%02X:%02X:%02X:%02X:%02X\r\n", macBT[0], macBT[1], macBT[2], macBT[3], macBT[4], macBT[5]);
}

void loop() {
  delay(1000);
}

するとシリアルモニタに「1A:2B:3C:4D:5E:6F」のようなアドレスが出るのでこれを使って…

マスター側
//This example code is in the Public Domain (or CC0 licensed, at your option.)
//By Victor Tchistiak - 2019
//
//This example demostrates master mode bluetooth connection and pin
//it creates a bridge between Serial and Classical Bluetooth (SPP)
//this is an extention of the SerialToSerialBT example by Evandro Copercini - 2018
//

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

BluetoothSerial SerialBT;

String MACadd = "1A:2B:3C:4D:5E:6F";//スレーブのアドレス
uint8_t address[6]  = {0x1A, 0x2B, 0x3C, 0x4D, 0x5E, 0x6F};//スレーブのアドレス
//uint8_t address[6]  = {0x00, 0x1D, 0xA5, 0x02, 0xC3, 0x22};
String name = "OBDII";
char *pin = "1234"; //<- standard pin would be provided by default
bool connected;

String History[10] = {"", "", "", "", "", "", "", "", "", ""};

void setup() {
  M5.begin();
  Serial.begin(115200);
  SerialBT.begin("ESP32test", true);
  Serial.println("The device started in master mode, make sure remote BT device is on!");
  M5.Lcd.setTextSize(3), M5.Lcd.setCursor(50, 80), M5.Lcd.print("Connected...");
  connected = SerialBT.connect(address);
  M5.Lcd.setTextSize(2);
  if (connected) {
    M5.Lcd.clear();
  } else {
    while (!SerialBT.connected(10000)) {
      M5.Lcd.clear();
      Serial.println("Failed to connect. Make sure remote device is available and in range, then restart app.");
      M5.Lcd.setTextColor(RED), M5.Lcd.setCursor(0, 80), M5.Lcd.print("Failed to connect");
    }
  }
  delay(3000);
  M5.Lcd.clear();
  M5.Lcd.setTextColor(WHITE), M5.Lcd.setTextSize(2), M5.Lcd.setCursor(0, 0);


  // disconnect() may take upto 10 secs max
  if (SerialBT.disconnect()) {
    Serial.println("Connected Succesfully!");
    M5.Lcd.setTextColor(GREEN), M5.Lcd.setCursor(0, 80), M5.Lcd.print("Connected Succesfully!");
  }
  //this would reconnect to the name(will use address, if resolved) or address used with connect(name / address).
  SerialBT.connect();
}

void loop() {
  if (Serial.available()) {
    String datastring = Serial.readStringUntil(';');
    Serial.print("SEND:" + datastring);
    add("SEND:" + datastring);
    SerialBT.print(datastring);
    //Serial.print("myself:");
    //Serial.print(Serial.readStringUntil(';'));

  }
  if (SerialBT.available()) {
    String datastring = SerialBT.readStringUntil(';');
    Serial.print("READ:" + datastring);
    add("READ:" + datastring);
  }
  delay(20);
}


void add(String addstring) {
  History[9] = History[8];
  History[8] = History[7];
  History[7] = History[6];
  History[6] = History[5];
  History[5] = History[4];
  History[4] = History[3];
  History[3] = History[2];
  History[2] = History[1];
  History[1] = History[0];
  History[0] = addstring;
  M5.Lcd.fillRect(0, 0, 320, 180, BLACK);
  M5.Lcd.setTextColor(WHITE), M5.Lcd.setTextSize(2), M5.Lcd.setCursor(0, 0);
  M5.Lcd.print(History[0]);
  M5.Lcd.print(History[1]);
  M5.Lcd.print(History[2]);
  M5.Lcd.print(History[3]);
  M5.Lcd.print(History[4]);
  M5.Lcd.print(History[5]);
  M5.Lcd.print(History[6]);
  M5.Lcd.print(History[7]);
  M5.Lcd.print(History[8]);
  M5.Lcd.print(History[9]);
  M5.Lcd.fillRect(0, 180, 100, 100, BLACK);
}


スレーブ側
//This example code is in the Public Domain (or CC0 licensed, at your option.)
//By Evandro Copercini - 2018
//
//This example creates a bridge between Serial and Classical Bluetooth (SPP)
//and also demonstrate that SerialBT have the same functionalities of a normal Serial

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

String History[10] = {"", "", "", "", "", "", "", "", "", ""};
String sendstring = "";

#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
#error Bluetooth is not enabled! Please run `make menuconfig` to and enable it
#endif

BluetoothSerial SerialBT;

void setup() {
  M5.begin();
  Serial.begin(115200);
  SerialBT.begin("ESP32test"); //Bluetooth device name
  Serial.println("The device started, now you can pair it with bluetooth!");
}

void loop() {
  if (Serial.available()) {
    String datastring = Serial.readStringUntil(';');
    Serial.print("SEND:" + datastring);
    add("SEND:" + datastring);
    SerialBT.print(datastring);
    //Serial.print("myself:");
    //Serial.print(Serial.readStringUntil(';'));

  }
  if (SerialBT.available()) {
    String datastring = SerialBT.readStringUntil(';');
    Serial.print("READ:" + datastring);
    add("READ:" + datastring);
  }
  delay(20);
}

void add(String addstring) {
  History[9] = History[8];
  History[8] = History[7];
  History[7] = History[6];
  History[6] = History[5];
  History[5] = History[4];
  History[4] = History[3];
  History[3] = History[2];
  History[2] = History[1];
  History[1] = History[0];
  History[0] = addstring;
  M5.Lcd.fillRect(0, 0, 320, 180, BLACK);
  M5.Lcd.setTextSize(2), M5.Lcd.setCursor(0, 0);

  for (byte i = 0; i = 9; i++) {
    if (History[i].charAt(0) == 'S'){
      M5.Lcd.setTextColor(WHITE);
    }else{
      M5.Lcd.setTextColor(YELLOW);
    }
    M5.Lcd.print(History[i]);
  }
}



動作すると、シリアルでの会話(SEND: READ:が出ます)や、M5Stackに過去の履歴の表示ができます。

バグ
・早く送信しまくるとバグる
・;(セミコロン)は送信不可(バグる)
・ディスプレイ表示▲




nice!(0)  コメント(0) 

この広告は前回の更新から一定期間経過したブログに表示されています。更新すると自動で解除されます。