2025年5月26日月曜日

Ble5 using Arduino Uno R4 WiFI receiver(sender not yet)

 

Raspberry Pi Pico 2 W (ピコ・ツー・ダブル) は、無線機能(Wi-FiとBluetooth)を備えたマイコンボードです。2.4GHzのWi-Fi (IEEE 802.11n) とBluetooth 5.2を搭載しており、BLE (Bluetooth Low Energy) を利用できます。picow,microbit はBluetooth4(BLE)である
BLE(Bluetooth Low Energy)において、SubscribeReadは、データの受信方法に関する重要な違いです。Subscribeは、デバイスが特定の特性の値が変更されたときに自動的に通知を受け取る方法であり、Readは、必要に応じて手動でデバイスからデータを読み取る方法です。
より詳しく説明すると:
  • Subscribe (通知/Indication/Notification):
    • デバイスが特定の特性の値が変更されたときに、自動的に通知を受け取ります。
    • セントラルデバイスがサブスクライブすると、ペリフェラルデバイスは、その特性の値が変更されるたびに通知を送ります。
    • 通知の種類として、NotificationIndicationがあり、Indicationは受信確認(ACK)を要求するのに対し、Notificationは要求しません。
    • 受信確認を要求するIndicationは、データの送信完了を保証するために使用されます。
  • Read (読み取り):
    • セントラルデバイスが、必要に応じてデバイスからデータを読み取ります。
    • Readは、特定の特性の値を取得するために、デバイスに読み取り要求を送信します。
    • Readは、データが変更されたかどうかに関わらず、必要な時にデータの状態を読み取ることができます。
BLE(Bluetooth Low Energy)において、「Write」と「Notify」は、デバイス間でのデータのやり取り方法の二つの種類です。
「Write」は、セントラルデバイス(例:スマホ)からペリフェラルデバイス(例:BLEセンサー)に対して、データを書き込む(送信する)方法です。一方、「Notify」は、ペリフェラルデバイスからセントラルデバイスに対して、イベントや変更があった場合に通知を送る方法です
Write (書き込み):
  • セントラルがペリフェラルに直接データを送る。
  • データの読み込みや変更をセントラル側で行う際に利用される。
  • ペリフェラル側で何か設定を変更したり、データを記録したりする場合に便利。
Notify (通知):
  • ペリフェラルがセントラルにイベントやデータの変更を知らせる。
  • 例えば、温度センサーが温度が変わった場合に、セントラルに通知を送るなど。
  • セントラル側でリアルタイムなデータの監視や、イベントに応じた処理を行う場合に便利。
違いを簡単にまとめると:
  • Writeはセントラルからペリフェラルへの一方的なデータ送信。
  • Notifyはペリフェラルからセントラルへのイベントや変更の通知。
補足:
  1. Notifyには、レスポンスが必要な「Indicate」とレスポンスが不要な「Notify」があります。
  2. BLEでは、これらの「Write」や「Notify」を組み合わせることで、様々なアプリケーションを実現できます。
  3. IndicateとNotifyの違い
  4. Indicate:
  5. データの送信後、セントラルデバイスが受信確認を返信し、受信確認が返信されるまで次のデータ送信に進まない。
    1. Notify:
      データの送信後、セントラルデバイスからの受信確認を待たずに次のデータ送信に進む。
    Indicateは、データの正確な転送が重要となるアプリケーションにおいて、Notifyよりも適しています。例えば、スマートホームデバイスのステータス情報や、センサーデータの通知など、データの損失が許されない場合にIndicateが用いられます。
    Indicateは、CCCD (Client Characteristic Configuration Descriptor)という特別なDescriptorを使い、セントラルデバイスがIndicateを購読しているかどうかを管理します。
  6. ^^^^^^^^^^^^^^^^^^^^^^
  7. esp32もそうだし、micropythn-picowもそうだとおもうがbluetoot serial terminal
、serial bluetooth monitor ではセントラルが拒否してまう ble4対応のためか

以下は本サイトの原文 ble5対応のlight blueでOK
ただしlight blueでも入力はhexで0,1でないとうまくいかなかった

https://docs.sunfounder.com/projects/elite-explorer-kit/ja/latest/new_feature_projects/02_bluetooth.html

#include <ArduinoBLE.h>
// Bluetooth® Low Energy LED Service サービス命名
BLEService ledService("19B10000-E8F2-537E-4F6C-D104768A1214");

// Bluetooth® Low Energy LED Switch Characteristic -
// custom 128-bit UUID, read and writable by central
// サービス特性
BLEByteCharacteristic switchCharacteristic("19B10001-E8F2-537E-4F6C-\
D104768A1214", BLERead | BLEWrite);

const int ledPin = LED_BUILTIN; // pin to use for the LED

void setup() {
Serial.begin(9600);
while (!Serial);
// set LED pin to output mode
pinMode(ledPin, OUTPUT);

// begin initialization
if (!BLE.begin()) {
Serial.println("starting Bluetooth® Low Energy module failed!");
while (1);
}
// 以下のadvertised local name と上記で宣言されたservice UUID が紐付けられた:
BLE.setLocalName("UNO R4 LED");
BLE.setAdvertisedService(ledService);//ledServiceをアドバタイズする!
// add the characteristic to the service
ledService.addCharacteristic(switchCharacteristic);
//ledServiceはswitchCharacteristicをもつ!

// add service
BLE.addService(ledService);
// set the initial value for the characeristic:
switchCharacteristic.writeValue(0);
// start advertising
BLE.advertise();

Serial.println("BLE LED Peripheral");
}

void loop() {
// listen for Bluetooth® Low Energy peripherals to connect:
BLEDevice central = BLE.central(); // central is light blue app

// if a central is connected to peripheral:
if (central) {
Serial.print("Connected to central: ");
// print the central's MAC address:
Serial.println(central.address()); // mac address!

// while the central is still connected to peripheral:
while (central.connected()) {
// if the remote device wrote to the characteristic,
// use the value to control the LED:
if (switchCharacteristic.written()) {
if (switchCharacteristic.value()) { // any value other than 0
Serial.println("LED on");
digitalWrite(ledPin, HIGH); // will turn the LED on
} else { // a 0 value
Serial.println(F("LED off"));
digitalWrite(ledPin, LOW); // will turn the LED off
}
}
}

// when the central disconnects, print it out:
Serial.print(F("Disconnected from central: "));
Serial.println(central.address());
}
}

0 件のコメント:

コメントを投稿