3つ目の例に示すように、長さ0の非retainデータを送信すると、retainデータを削除できます。
+3つ目の例に示すように、長さ0のretainデータを送信すると、retainデータを削除できます。
+注釈
+サンプルでは文字列(ASCII)データを送受信していますが、値をbyte配列・バイナリで送受信することもできます。
+「Serial通信」では、開発用PCのシリアルコンソールをつかって、USB接続を介して、文字列を送受信しました。 +「Wifi接続」のTelnetサーバでは、TCP/IP通信上でのTelnet接続を介して、文字列を送受信しました。
+ここでは、Bluetoothを用いて、上記と同様、2台のデバイス間で文字列を送受信する例を示します。Bluetoth では、機器同士は同一の「プロファイル」という通信方式に対応している必要があります。Serial Protocol Profile は、Bluetooth無線通信で仮想シリアル接続を可能にするプロファイルです。
+注釈
+基本的に ESP32同士をBluetoothシリアルでつないでみる と同じです。
+リスト 24 を、1台目のM5StickCPlusで実行し、BluetoothのMACアドレスを調べます。
+1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +10 +11 +12 +13 +14 +15 +16 +17 +18 +19 +20 +21 +22 +23 +24 +25 +26 +27 +28 +29 +30 +31 +32 +33 +34 +35 +36 +37 +38 +39 +40 +41 +42 +43 +44 +45 +46 +47 +48 +49 +50 +51 +52 +53 +54 +55 +56 +57 +58 +59 +60 +61 | #include <M5StickCPlus.h>
+#include "BluetoothSerial.h"
+
+BluetoothSerial SerialBT;
+
+const char* dname = "BT_Serial_00"; // Slave device name
+
+void setup() {
+ M5.begin();
+ M5.Lcd.setRotation(3);
+ M5.Lcd.fillScreen(YELLOW);
+ M5.Lcd.setTextColor(BLACK, YELLOW);
+ M5.Lcd.setCursor(0, 0, 1);
+ M5.Lcd.setTextSize(2);
+
+ Serial.begin(115200);
+ SerialBT.begin(dname); //Bluetooth device name
+ Serial.println("The device started, now you can pair it with bluetooth!");
+
+ uint8_t macBT[6];
+ esp_read_mac(macBT, ESP_MAC_BT);
+ char mac[20];
+ sprintf(mac, "%02X:%02X:%02X:%02X:%02X:%02X", macBT[0], macBT[1], macBT[2], macBT[3], macBT[4], macBT[5]);
+ Serial.println(mac);
+ M5.Lcd.println(mac);
+
+}
+
+int line = 0;
+void loop() {
+ char c;
+ if (Serial.available()) {
+ SerialBT.write(c = Serial.read());
+ Serial.printf("read:%c\n", c);
+ }
+ if (SerialBT.available()) {
+ Serial.write(c = SerialBT.read());
+ SerialBT.write(c + 0);
+ M5.Lcd.print(c);
+ if (c == 0xa) line++;
+ if (line > 7) {
+ M5.Lcd.fillScreen(YELLOW); M5.Lcd.setCursor(0, 0, 1);
+ line = 0;
+ }
+ }
+
+ M5.update();
+ if (M5.BtnA.wasReleasefor(1000) ) {
+ SerialBT.println("[A] was Pressed longer than 1s");
+ } else if (M5.BtnA.wasReleased()) {
+ SerialBT.println("[A] was Pressed");
+ } else if (M5.BtnB.wasReleasefor(1000) ) {
+ SerialBT.println("[B] was Pressed longer than 1s");
+ } else if (M5.BtnB.wasReleased()) {
+ SerialBT.println("[B] was Pressed");
+ } else if (M5.Axp.GetBtnPress() == 2) {
+ SerialBT.println("[Power] was Pressed");
+ }
+
+ delay(20);
+}
+ |
リスト 25 を、2台目のM5StickCPlusで実行します。このとき、(1)で調べたMACアドレスを指定します。
+1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 + 11 + 12 + 13 + 14 + 15 + 16 + 17 + 18 + 19 + 20 + 21 + 22 + 23 + 24 + 25 + 26 + 27 + 28 + 29 + 30 + 31 + 32 + 33 + 34 + 35 + 36 + 37 + 38 + 39 + 40 + 41 + 42 + 43 + 44 + 45 + 46 + 47 + 48 + 49 + 50 + 51 + 52 + 53 + 54 + 55 + 56 + 57 + 58 + 59 + 60 + 61 + 62 + 63 + 64 + 65 + 66 + 67 + 68 + 69 + 70 + 71 + 72 + 73 + 74 + 75 + 76 + 77 + 78 + 79 + 80 + 81 + 82 + 83 + 84 + 85 + 86 + 87 + 88 + 89 + 90 + 91 + 92 + 93 + 94 + 95 + 96 + 97 + 98 + 99 +100 +101 +102 +103 +104 | #include <M5StickCPlus.h>
+#include <BluetoothSerial.h>
+
+BluetoothSerial SerialBT;
+
+bool willConnect = true;
+bool connected;
+
+const char* dname = "BT_Serial_00"; // Slave device name
+
+void setup() {
+ M5.begin();
+ M5.Lcd.setRotation(3);
+ M5.Lcd.fillScreen(BLUE);
+
+ M5.Lcd.setTextColor(BLACK, CYAN);
+ M5.Lcd.setCursor(0, 0, 1);
+ M5.Lcd.setTextSize(2);
+
+ Serial.begin(115200);
+ SerialBT.begin("BT_Serial_01", true);
+ Serial.println("The device started in master mode, make sure remote BT device is on!");
+
+ // connect(address) is fast (upto 10 secs max), connect(name) is slow (upto 30 secs max) as it needs
+ // to resolve name to address first, but it allows to connect to different devices with the same name.
+ // Set CoreDebugLevel to Info to view devices bluetooth address and device names
+ if (false) {
+ connected = SerialBT.connect(dname);
+ } else {
+ uint8_t address[6] = {0x24, 0xA1, 0x60, 0x47, 0x84, 0x4E}; // MACアドレスが 24:A1:60:47:84:4E のとき
+ connected = SerialBT.connect(address);
+ }
+ if (connected) {
+ Serial.println("Connected Succesfully!");
+ M5.Beep.tone(2000, 1000);
+ M5.Lcd.fillScreen(CYAN);
+ }
+ // disconnect() may take upto 10 secs max
+ // if (SerialBT.disconnect()) {
+ // connected = false;
+ // Serial.println("Disconnected Succesfully!");
+ // }
+ // this would reconnect to the name(will use address, if resolved) or address used with connect(name/address).
+ //SerialBT.connect();
+}
+
+void connectOrDisconnect(bool willCon) {
+ if (willCon && connected) return;
+ if (willCon) {
+ if (connected) {
+ Serial.println("Connected Succesfully!");
+ M5.Beep.tone(2000, 1000);
+ M5.Lcd.fillScreen(CYAN);
+ } else {
+ while (!SerialBT.connected(10000)) {
+ Serial.println("Failed to connect. Make sure remote device is available and in range, then restart app.");
+ M5.Lcd.fillScreen(PURPLE);
+ }
+ }
+ } else {
+ if ( SerialBT.disconnect() ) {
+ connected = false;
+ Serial.println("Disconnected Succesfully!");
+ M5.Lcd.fillScreen(BLUE);
+ }
+ }
+}
+int line = 0;
+void loop() {
+ connectOrDisconnect( willConnect );
+
+ char c;
+ if (Serial.available()) {
+ SerialBT.write(c = Serial.read());
+ Serial.printf("read:%c\n", c);
+ }
+ if (SerialBT.available()) {
+ Serial.write(c = SerialBT.read());
+ M5.Lcd.print(c);
+ if (c == 0xa) line++;
+ if (line > 7) {
+ M5.Lcd.fillScreen(CYAN); M5.Lcd.setCursor(0, 0, 1);
+ line = 0;
+ }
+ }
+
+ M5.update(); M5.Beep.update();
+ if (M5.BtnA.wasReleasefor(1000) ) {
+ SerialBT.println("[A] was Pressed longer than 1s");
+ } else if (M5.BtnA.wasReleased()) {
+ SerialBT.println("[A] was Pressed");
+ } else if (M5.BtnB.wasReleasefor(1000) ) {
+ SerialBT.println("[B] was Pressed longer than 1s");
+ if (SerialBT.disconnect()) {
+ connected = false;
+ M5.Lcd.fillScreen(PURPLE);
+ }
+ } else if (M5.BtnB.wasReleased()) {
+ SerialBT.println("[B] was Pressed");
+ } else if (M5.Axp.GetBtnPress() == 2) {
+ SerialBT.println("[Power] was Pressed");
+ }
+ delay(20);
+}
+ |