前提・実現したいこと
ESP32を3台使って通信をしたいです。3台のうち2台にセンサーをつけ残りの一台とつなぎ、センサーが反応したらデータを送り、送られてきたデータがどちらのESP32か判断して違う動きをしたい。
1対1での通信はできましたが1対2ができません。プログラムを思いつきで書いてみたのですがコンパイルは通るものの接続してくれません。1対1のプログラムをCHARACTERISTIC_UUIDを2つにしてみたのですがうまくいきません
該当のソースコード
↓受信側(1対2)
arduino
1//Based on BLE client example 2//サーバーが停止するとエラーで回復できない 3#include "BLEDevice.h" 4 5#define SERVICE_UUID "81d78b05-4e0a-4644-b364-b79312e4c307" 6#define CHARACTERISTIC_UUID "0989bf07-e886-443e-82db-7108726bb650" 7#define CHARACTERISTIC_UUID2"a67ad422-92aa-4fb1-982c-1a17ec351784" 8#define SERVER_NAME "esp32devc" 9 10 11static BLEUUID serviceUUID(SERVICE_UUID); 12static BLEUUID charUUID(CHARACTERISTIC_UUID); 13static BLEUUID charUUID2(CHARACTERISTIC_UUID2); 14 15 16static BLEAddress *pServerAddress; 17static boolean doConnect = false; 18static boolean connected = false; 19static BLERemoteCharacteristic* pRemoteCharacteristic; 20static BLERemoteCharacteristic* pRemoteCharacteristic2; 21 22 23static void notifyCallback( 24 BLERemoteCharacteristic* pBLERemoteCharacteristic, 25 uint8_t* pData, 26 size_t length, 27 bool isNotify) { 28 Serial.print("Notify callback for characteristic "); 29 Serial.print(pBLERemoteCharacteristic->getUUID().toString().c_str()); 30 Serial.print(" of data length "); 31 Serial.println(length); 32} 33 34bool connectToServer(BLEAddress pAddress) { 35 Serial.print("Forming a connection to "); 36 Serial.println(pAddress.toString().c_str()); 37 BLEClient* pClient = BLEDevice::createClient(); 38 pClient->connect(pAddress); 39 BLERemoteService* pRemoteService = pClient->getService(serviceUUID); 40 Serial.print(" - Connected to server :"); 41 Serial.println(serviceUUID.toString().c_str()); 42 if (pRemoteService == nullptr) { 43 Serial.print("Failed to find our service UUID: "); 44 Serial.println(serviceUUID.toString().c_str()); 45 return false; 46 } 47 pRemoteCharacteristic2 = pRemoteService->getCharacteristic(charUUID2); 48 Serial.print(" - Found our characteristic UUID: "); 49 Serial.println(charUUID2.toString().c_str()); 50 if (pRemoteCharacteristic2 == nullptr) { 51 Serial.print("Failed to find our characteristic UUID: "); 52 return false; 53 } 54 55 pRemoteCharacteristic2 = pRemoteService->getCharacteristic(charUUID2); 56 Serial.print(" - Found our characteristic UUID: "); 57 Serial.println(charUUID2.toString().c_str()); 58 if (pRemoteCharacteristic2 == nullptr) { 59 Serial.print("Failed to find our characteristic UUID: "); 60 return false; 61 } 62 63 Serial.println(" - Found our characteristic"); 64 pRemoteCharacteristic2->registerForNotify(notifyCallback); 65 pRemoteCharacteristic->registerForNotify(notifyCallback); 66 return true; 67} 68 69 70 71class MyAdvertisedDeviceCallbacks: public BLEAdvertisedDeviceCallbacks { 72 void onResult(BLEAdvertisedDevice advertisedDevice) { 73 Serial.print("BLE Advertised Device found: "); 74 Serial.println(advertisedDevice.toString().c_str());//Name: esp32devc, Address: 30:ae:a4:02:a3:fe, txPower: -21 75 Serial.println(advertisedDevice.getName().c_str());//esp32devc 76 77 if(advertisedDevice.getName()==SERVER_NAME){ // 78 Serial.println(advertisedDevice.getAddress().toString().c_str()); 79 advertisedDevice.getScan()->stop(); 80 pServerAddress = new BLEAddress(advertisedDevice.getAddress()); 81 doConnect = true; 82 } 83 } 84}; 85 86 87void setup() { 88 pinMode(0, OUTPUT); 89 Serial.begin(115200); 90 Serial.println("BLE_DataClient180130"); 91 BLEDevice::init(""); 92 BLEScan* pBLEScan = BLEDevice::getScan(); 93 Serial.println("getScan"); 94 pBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks()); 95 Serial.println("setAdvertisedDeviceCallbacks"); 96 pBLEScan->setActiveScan(true); 97 pBLEScan->start(10); 98 Serial.println(""); 99 Serial.println("End of setup"); 100} 101void loop() { 102 if (doConnect == true) { 103 if (connectToServer(*pServerAddress)) { 104 Serial.println("We are now connected to the BLE Server."); 105 connected = true; 106 doConnect = true; 107 } else { 108 Serial.println("We have failed to connect to the server; there is nothin more we will do."); 109 connected = false; 110 } 111 doConnect = false; 112 } 113 if (connected) { 114 std::string value2 = pRemoteCharacteristic2->readValue(); 115 String strVal2=value2.c_str(); 116 int strNum2=strVal2.toInt(); 117 Serial.println(strNum2); 118 if(strNum2==1){ 119 digitalWrite(0, HIGH); 120 Serial.print("No.2"); 121 delay(1000); 122 } 123 } else{ 124 Serial.println("Not connected2"); 125 doConnect = true; 126 } 127if (connected) { 128 std::string value = pRemoteCharacteristic->readValue(); 129 String strVal=value.c_str(); 130 int strNum=strVal.toInt(); 131 Serial.println(strNum); 132 if(strNum==1){ 133 digitalWrite(0, HIGH); 134 Serial.print("No.1"); 135 delay(1000); 136 } 137 } else{ 138 Serial.println("Not connected"); 139 doConnect = true; 140 } 141 142 digitalWrite(0, LOW); 143 delay(100); 144}
↓受信側(1対1)
arduino
1//Based on BLE client example 2//サーバーが停止するとエラーで回復できない 3 4#include "BLEDevice.h" 5 6#define SERVICE_UUID "81d78b05-4e0a-4644-b364-b79312e4c307" 7#define CHARACTERISTIC_UUID "0989bf07-e886-443e-82db-7108726bb650" 8#define SERVER_NAME "esp32devc" 9 10static BLEUUID serviceUUID(SERVICE_UUID); 11static BLEUUID charUUID(CHARACTERISTIC_UUID); 12 13 14static BLEAddress *pServerAddress; 15static boolean doConnect = false; 16static boolean connected = false; 17static BLERemoteCharacteristic* pRemoteCharacteristic; 18 19 20static void notifyCallback( 21 BLERemoteCharacteristic* pBLERemoteCharacteristic, 22 uint8_t* pData, 23 size_t length, 24 bool isNotify) { 25 Serial.print("Notify callback for characteristic "); 26 Serial.print(pBLERemoteCharacteristic->getUUID().toString().c_str()); 27 Serial.print(" of data length "); 28 Serial.println(length); 29} 30 31 32bool connectToServer(BLEAddress pAddress) { 33 Serial.print("Forming a connection to "); 34 Serial.println(pAddress.toString().c_str()); 35 BLEClient* pClient = BLEDevice::createClient(); 36 pClient->connect(pAddress); 37 BLERemoteService* pRemoteService = pClient->getService(serviceUUID); 38 Serial.print(" - Connected to server :"); 39 Serial.println(serviceUUID.toString().c_str()); 40 if (pRemoteService == nullptr) { 41 Serial.print("Failed to find our service UUID: "); 42 Serial.println(serviceUUID.toString().c_str()); 43 return false; 44 } 45 pRemoteCharacteristic = pRemoteService->getCharacteristic(charUUID); 46 Serial.print(" - Found our characteristic UUID: "); 47 Serial.println(charUUID.toString().c_str()); 48 if (pRemoteCharacteristic == nullptr) { 49 Serial.print("Failed to find our characteristic UUID: "); 50 return false; 51 } 52 Serial.println(" - Found our characteristic"); 53 pRemoteCharacteristic->registerForNotify(notifyCallback); 54 return true; 55} 56 57 58class MyAdvertisedDeviceCallbacks: public BLEAdvertisedDeviceCallbacks { 59 void onResult(BLEAdvertisedDevice advertisedDevice) { 60 Serial.print("BLE Advertised Device found: "); 61 Serial.println(advertisedDevice.toString().c_str());//Name: esp32devc, Address: 30:ae:a4:02:a3:fe, txPower: -21 62 Serial.println(advertisedDevice.getName().c_str());//esp32devc 63 64 if(advertisedDevice.getName()==SERVER_NAME){ // 65 Serial.println(advertisedDevice.getAddress().toString().c_str()); 66 advertisedDevice.getScan()->stop(); 67 pServerAddress = new BLEAddress(advertisedDevice.getAddress()); 68 doConnect = true; 69 } 70 } 71}; 72 73 74void setup() { 75 pinMode(0, OUTPUT); 76 Serial.begin(115200); 77 Serial.println("BLE_DataClient180130"); 78 BLEDevice::init(""); 79 BLEScan* pBLEScan = BLEDevice::getScan(); 80 Serial.println("getScan"); 81 pBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks()); 82 Serial.println("setAdvertisedDeviceCallbacks"); 83 pBLEScan->setActiveScan(true); 84 pBLEScan->start(10); 85 Serial.println(""); 86 Serial.println("End of setup"); 87} 88 89 90void loop() { 91 if (doConnect == true) { 92 if (connectToServer(*pServerAddress)) { 93 Serial.println("We are now connected to the BLE Server."); 94 connected = true; 95 } else { 96 Serial.println("We have failed to connect to the server; there is nothin more we will do."); 97 connected = false; 98 } 99 doConnect = false; 100 } 101 if (connected) { 102 std::string value = pRemoteCharacteristic->readValue(); 103 String strVal=value.c_str(); 104 int strNum=strVal.toInt(); 105 Serial.println(strNum); 106 if(strNum==1){ 107 digitalWrite(0, HIGH); 108 delay(1000); 109 } 110 } else{ 111 Serial.println("Not connected"); 112 doConnect = true; 113 } 114 digitalWrite(0, LOW); 115 delay(100); 116}
試したこと
ここに問題に対して試したことを記載してください。
補足情報(FW/ツールのバージョンなど)
ここにより詳細な情報を記載してください。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。