diff --git a/_build/html/.doctrees/environment.pickle b/_build/html/.doctrees/environment.pickle index 12e43fd..7930dad 100644 --- a/_build/html/.doctrees/environment.pickle +++ b/_build/html/.doctrees/environment.pickle Binary files differ diff --git a/_build/html/.doctrees/week1.doctree b/_build/html/.doctrees/week1.doctree index 54a22d9..8cd969f 100644 --- a/_build/html/.doctrees/week1.doctree +++ b/_build/html/.doctrees/week1.doctree Binary files differ diff --git a/_build/html/_sources/week1.rst.txt b/_build/html/_sources/week1.rst.txt index e5b6e0f..135f20f 100644 --- a/_build/html/_sources/week1.rst.txt +++ b/_build/html/_sources/week1.rst.txt @@ -468,13 +468,43 @@ #(サブスクライブ) mosquitto_sub -h mqtt.istlab.info -u ex1 -P PASS -t office/temp -3つ目の例に示すように、長さ0の非retainデータを送信すると、retainデータを削除できます。 +3つ目の例に示すように、長さ0のretainデータを送信すると、retainデータを削除できます。 - +.. note:: サンプルでは文字列(ASCII)データを送受信していますが、値をbyte配列・バイナリで送受信することもできます。 Bluetooth Serial Protocol Profile (SPP) -------------------------------------------- +「Serial通信」では、開発用PCのシリアルコンソールをつかって、USB接続を介して、文字列を送受信しました。 +「Wifi接続」のTelnetサーバでは、TCP/IP通信上でのTelnet接続を介して、文字列を送受信しました。 + +ここでは、Bluetoothを用いて、上記と同様、2台のデバイス間で文字列を送受信する例を示します。Bluetoth では、機器同士は同一の「プロファイル」という通信方式に対応している必要があります。Serial Protocol Profile は、Bluetooth無線通信で仮想シリアル接続を可能にするプロファイルです。 + +.. note:: 基本的に `ESP32同士をBluetoothシリアルでつないでみる `_ と同じです。 + +(1) スレーブ用デバイスのMACアドレスを調べる +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +:numref:`bts01_slave` を、1台目のM5StickCPlusで実行し、BluetoothのMACアドレスを調べます。 + +.. literalinclude:: src/bts01_slave.ino + :caption: + :name: bts01_slave + :language: arduino + :linenos: + +(2) マスタ用デバイスにサンプルを書き込む +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +:numref:`bts01_master` を、2台目のM5StickCPlusで実行します。このとき、(1)で調べたMACアドレスを指定します。 + +.. literalinclude:: src/bts01_master.ino + :caption: + :name: bts01_master + :language: arduino + :linenos: + + Bluetooth Low Energy -------------------------------------- @@ -489,6 +519,7 @@ ----------------------------------- + その他 ------------------------------------- - CO2 (CCS811) @@ -496,4 +527,3 @@ - JJYClock - RTC(BM8563) - OTA - diff --git a/_build/html/index.html b/_build/html/index.html index 9314baf..3f9dbfc 100644 --- a/_build/html/index.html +++ b/_build/html/index.html @@ -206,8 +206,8 @@
  • Bluetooth Serial Protocol Profile (SPP)
  • Bluetooth Low Energy
  • Preference
  • -
  • 電力制御
  • -
  • その他
  • +
  • 電力制御
  • +
  • その他
  • 2週目
  • -
  • Bluetooth Serial Protocol Profile (SPP)
  • +
  • Bluetooth Serial Protocol Profile (SPP) +
  • Bluetooth Low Energy
  • Preference
  • -
  • 電力制御
  • -
  • その他
  • +
  • 電力制御
  • +
  • その他
  • 2週目
  • @@ -2427,11 +2431,368 @@ mosquitto_sub -h mqtt.istlab.info -u ex1 -P PASS -t office/temp -

    3つ目の例に示すように、長さ0の非retainデータを送信すると、retainデータを削除できます。

    +

    3つ目の例に示すように、長さ0のretainデータを送信すると、retainデータを削除できます。

    +
    +

    注釈

    +

    サンプルでは文字列(ASCII)データを送受信していますが、値をbyte配列・バイナリで送受信することもできます。

    +

    Bluetooth Serial Protocol Profile (SPP)

    +

    「Serial通信」では、開発用PCのシリアルコンソールをつかって、USB接続を介して、文字列を送受信しました。 +「Wifi接続」のTelnetサーバでは、TCP/IP通信上でのTelnet接続を介して、文字列を送受信しました。

    +

    ここでは、Bluetoothを用いて、上記と同様、2台のデバイス間で文字列を送受信する例を示します。Bluetoth では、機器同士は同一の「プロファイル」という通信方式に対応している必要があります。Serial Protocol Profile は、Bluetooth無線通信で仮想シリアル接続を可能にするプロファイルです。

    +
    +

    注釈

    +

    基本的に ESP32同士をBluetoothシリアルでつないでみる と同じです。

    +
    +
    +

    (1) スレーブ用デバイスのMACアドレスを調べる

    +

    リスト 24 を、1台目のM5StickCPlusで実行し、BluetoothのMACアドレスを調べます。

    +
    +
    リスト 24 src/bts01_slave.ino
    +
     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);
    +}
    +
    +
    +
    +
    +
    +

    (2) マスタ用デバイスにサンプルを書き込む

    +

    リスト 25 を、2台目のM5StickCPlusで実行します。このとき、(1)で調べたMACアドレスを指定します。

    +
    +
    リスト 25 src/bts01_master.ino
    +
      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);
    +}
    +
    +
    +
    +

    Bluetooth Low Energy

    @@ -2440,11 +2801,11 @@

    Preference

    -
    -

    電力制御

    -
    -

    その他

    +

    電力制御

    +
    +
    +

    その他

    • CO2 (CCS811)
    • RCS-620S (Felica)
    • diff --git a/src/bts01_master.ino b/src/bts01_master.ino new file mode 100644 index 0000000..62cc8ac --- /dev/null +++ b/src/bts01_master.ino @@ -0,0 +1,104 @@ +#include +#include + +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); +} \ No newline at end of file diff --git a/src/bts01_showmac.ino b/src/bts01_showmac.ino new file mode 100644 index 0000000..dc91218 --- /dev/null +++ b/src/bts01_showmac.ino @@ -0,0 +1,11 @@ +void setup(void) { + Serial.begin(115200); +} + +void loop() { + 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]); + delay(5000); +} \ No newline at end of file diff --git a/src/bts01_slave.ino b/src/bts01_slave.ino new file mode 100644 index 0000000..01e38a8 --- /dev/null +++ b/src/bts01_slave.ino @@ -0,0 +1,61 @@ +#include +#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); +} \ No newline at end of file diff --git a/week1.rst b/week1.rst index e5b6e0f..135f20f 100755 --- a/week1.rst +++ b/week1.rst @@ -468,13 +468,43 @@ #(サブスクライブ) mosquitto_sub -h mqtt.istlab.info -u ex1 -P PASS -t office/temp -3つ目の例に示すように、長さ0の非retainデータを送信すると、retainデータを削除できます。 +3つ目の例に示すように、長さ0のretainデータを送信すると、retainデータを削除できます。 - +.. note:: サンプルでは文字列(ASCII)データを送受信していますが、値をbyte配列・バイナリで送受信することもできます。 Bluetooth Serial Protocol Profile (SPP) -------------------------------------------- +「Serial通信」では、開発用PCのシリアルコンソールをつかって、USB接続を介して、文字列を送受信しました。 +「Wifi接続」のTelnetサーバでは、TCP/IP通信上でのTelnet接続を介して、文字列を送受信しました。 + +ここでは、Bluetoothを用いて、上記と同様、2台のデバイス間で文字列を送受信する例を示します。Bluetoth では、機器同士は同一の「プロファイル」という通信方式に対応している必要があります。Serial Protocol Profile は、Bluetooth無線通信で仮想シリアル接続を可能にするプロファイルです。 + +.. note:: 基本的に `ESP32同士をBluetoothシリアルでつないでみる `_ と同じです。 + +(1) スレーブ用デバイスのMACアドレスを調べる +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +:numref:`bts01_slave` を、1台目のM5StickCPlusで実行し、BluetoothのMACアドレスを調べます。 + +.. literalinclude:: src/bts01_slave.ino + :caption: + :name: bts01_slave + :language: arduino + :linenos: + +(2) マスタ用デバイスにサンプルを書き込む +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +:numref:`bts01_master` を、2台目のM5StickCPlusで実行します。このとき、(1)で調べたMACアドレスを指定します。 + +.. literalinclude:: src/bts01_master.ino + :caption: + :name: bts01_master + :language: arduino + :linenos: + + Bluetooth Low Energy -------------------------------------- @@ -489,6 +519,7 @@ ----------------------------------- + その他 ------------------------------------- - CO2 (CCS811) @@ -496,4 +527,3 @@ - JJYClock - RTC(BM8563) - OTA -