RustのbtleplugというBLEライブラリを使ってM5Stickと通信してみました。
https://github.com/deviceplug/btleplug
環境: mac0S High Sierra
Arduino M5Stick-C
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 |
#include <M5StickC.h> #include <BLEDevice.h> #include <BLEUtils.h> #include <BLEServer.h> #include <BLE2902.h> #define SERVER_NAME "M5Stick01" #define SERVICE_UUID "9cfa4ac2-f433-41f7-b2aa-5a3df12d8678" #define CHARACTERISTIC_UUID "7c32da1c-4c3e-482f-a12a-dd19bd7d96fa" BLEServer* pServer = NULL; BLECharacteristic* pCharacteristic = NULL; bool deviceConnected = false; class MyServerCallbacks: public BLEServerCallbacks { void onConnect(BLEServer* pServer) { M5.Lcd.println("connect"); deviceConnected = true; }; void onDisconnect(BLEServer* pServer) { M5.Lcd.println("disconnect"); deviceConnected = false; } }; class MyCallbacks: public BLECharacteristicCallbacks { void onRead(BLECharacteristic *pCharacteristic) { ; } void onWrite(BLECharacteristic *pCharacteristic) { ; } }; void setup() { M5.begin(); M5.MPU6886.Init(); // MPU6886を初期設定する BLEDevice::init(SERVER_NAME); BLEServer *pServer = BLEDevice::createServer(); pServer->setCallbacks(new MyServerCallbacks()); BLEService *pService = pServer->createService(SERVICE_UUID); pCharacteristic = pService->createCharacteristic( CHARACTERISTIC_UUID, BLECharacteristic::PROPERTY_READ | BLECharacteristic::PROPERTY_WRITE | BLECharacteristic::PROPERTY_NOTIFY | BLECharacteristic::PROPERTY_INDICATE ); pCharacteristic->setCallbacks(new MyCallbacks()); pCharacteristic->addDescriptor(new BLE2902()); pService->start(); BLEAdvertising *pAdvertising = pServer->getAdvertising(); pAdvertising->start(); M5.Lcd.println("Advertising"); } float ax, ay, az; // 加速度センサを読み出す変数 float gx, gy, gz; // ジャイロセンサを読み出す変数 char data[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; void loop() { M5.MPU6886.getAccelData(&ax, &ay, &az); M5.MPU6886.getGyroData(&gx, &gy, &gz); if (deviceConnected) { if(pCharacteristic != NULL){ data[0] = ax * 1000; data[1] = ay * 1000; data[2] = az * 1000; data[3] = gx * 1000; data[4] = gy * 1000; data[5] = gz * 1000; pCharacteristic->setValue(data); } } delay(100); M5.update(); } |
Mac側は、btleplugのexampleにある、lights.rsを参考にm5stick.rsを同じフォルダに作成しました。
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 |
extern crate btleplug; extern crate rand; use btleplug::api::{bleuuid::uuid_from_u16, Central, Peripheral, WriteType}; #[cfg(target_os = "linux")] use btleplug::bluez::manager::Manager; #[cfg(target_os = "macos")] use btleplug::corebluetooth::manager::Manager; #[cfg(target_os = "windows")] use btleplug::winrtble::manager::Manager; use std::thread; use std::time::Duration; use uuid::Uuid; const TARGET_UUID: u128 = 0x7c32da1c_4c3e_482f_a12a_dd19bd7d96fa; const M5STICK_CHARACTERISTIC_UUID: Uuid = Uuid::from_u128(TARGET_UUID); pub fn main() { let manager = Manager::new().unwrap(); // get the first bluetooth adapter let central = manager .adapters() .expect("Unable to fetch adapter list.") .into_iter() .nth(0) .expect("Unable to find adapters."); central.start_scan().unwrap(); thread::sleep(Duration::from_secs(2)); // find the device we're interested in let stick = central .peripherals() .into_iter() .find(|p| { p.properties() .local_name .iter() .any(|name| name.contains("M5Stick01")) }) .expect("M5Stick not found"); // connect to the device stick.connect().unwrap(); // discover characteristics stick.discover_characteristics().unwrap(); // find the characteristic we want let chars = stick.characteristics(); for c in chars.iter() { println!("uuid: {:?}", c.uuid); } let cmd_char = chars .iter() .find(|c| c.uuid == M5STICK_CHARACTERISTIC_UUID) .expect("Unable to find characterics"); for _ in 0..50{ let data = stick.read(&cmd_char).unwrap(); println!("data: {:?}", data); thread::sleep(Duration::from_millis(200)); } } |
btleplugディレクトリで下記コマンド実行
cargo run –example m5stick
出力は、M5Stickのセンサ値を表示します。
適当に動かしてみました。
uuid: 7c32da1c-4c3e-482f-a12a-dd19bd7d96fa
data: [140, 195, 45, 76, 44, 130]
data: [138, 209, 39, 3, 239, 191]
data: [138, 206, 41, 198, 56, 81]
data: [151, 200, 26, 137, 167, 8]
data: [153, 198, 55, 41, 202, 107]
data: [152, 202, 52, 52, 129, 118]
data: [235, 52, 236, 122, 249, 196]
data: [192, 127, 94, 78, 239, 119]
data: [116, 242, 85, 125, 113, 247]
…..
Rustのビルドはいろんなパッケージを読み込んでいるで時間がかかりますね。
こういうの記録しておかないと次に何かやるとき思い出せないので、
備忘録でした。