前提・実現したいこと
BLEの勉強として、
ESP32(ESP-WROOM-32D開発ボード)でBLEで送信し、iPhone 6s アプリBLE Scannerで受信しています。
ESP32を制御する為に、Arduino IDE 1.8.13を用いて以下のサイトを参照し設定を行いました。
参照サイト
スケッチはArduino IDEに設定した以下のESP32のスケッチ例をベースに作成・編集しました。
ESP32_BLE_Arduino > BLE_notify
行いたい事として、
BLE通信が接続後、一定時間毎カウントアップしたカウントに応じて奇数,偶数を送信させる事です。
イメージ:「Count = カウント数 -> Even or Odd」を送り、iPhoneで受信する。
発生している問題・エラーメッセージ
試したこと にも記載しましたが、setValue("Number -> Even")のように直接文字列入力では
データ受信する事まで確認が取れました。
以下のようにsetValueで送信する内容を文字列の変数strSendとして、コンパイルしたところ以下のようなエラーが表示されました。
arduino
1 // notify changed value 2 String strSend; 3 if (deviceConnected) { 4 Serial.println(value); 5 if (value % 2 == 0){ 6 strSend = "Count = " + String(value) + " -> Even"; 7 Serial.println(strSend); 8 pCharacteristic->setValue(&strSend); 9 //pCharacteristic->setValue("Number -> Even"); 10 11 } else if (value % 2 == 1){ 12 strSend = "Count = " + String(value) + " -> Odd"; 13 Serial.println(strSend); 14 pCharacteristic->setValue(&strSend); 15 //pCharacteristic->setValue("Number -> Odd"); 16 } 17 18 pCharacteristic->notify(); 19 value++; 20 delay(500); 21 }
以下、エラー内容
Arduino
1Arduino:1.8.13 (Windows 10), ボード:"ESP32 Dev Module, Disabled, Default 4MB with spiffs (1.2MB APP/1.5MB SPIFFS), 240MHz (WiFi/BT), QIO, 80MHz, 4MB (32Mb), 921600, None" 2 3D:\user\ドキュメント\Arduino\BLE_notify_mod\BLE_notify_mod.ino: In function 'void loop()': 4 5BLE_notify_mod:117:45: error: no matching function for call to 'setValue(String*)' 6 7C:\user\AppData\Local\Arduino15\packages\esp32\hardware\esp32\1.0.4\libraries\BLE\src/BLECharacteristic.h:75:7: note: candidate: void BLECharacteristic::setValue(uint16_t&) <near match> 8 9 void setValue(uint16_t& data16); 10 11 ^ 12 13C:\user\AppData\Local\Arduino15\packages\esp32\hardware\esp32\1.0.4\libraries\BLE\src/BLECharacteristic.h:75:7: note: conversion of argument 1 would be ill-formed: 14 15BLE_notify_mod:117:45: error: invalid conversion from 'String*' to 'uint16_t {aka short unsigned int}' [-fpermissive] 16 17BLE_notify_mod:117:45: error: cannot bind rvalue '(uint16_t)((int)(& strSend))' to 'uint16_t& {aka short unsigned int&}' 18 19C:\user\AppData\Local\Arduino15\packages\esp32\hardware\esp32\1.0.4\libraries\BLE\src/BLECharacteristic.h:76:7: note: candidate: void BLECharacteristic::setValue(uint32_t&) <near match> 20 21 void setValue(uint32_t& data32); 22 23 ^ 24 25C:\user\AppData\Local\Arduino15\packages\esp32\hardware\esp32\1.0.4\libraries\BLE\src/BLECharacteristic.h:76:7: note: conversion of argument 1 would be ill-formed: 26 27BLE_notify_mod:117:45: error: invalid conversion from 'String*' to 'uint32_t {aka unsigned int}' [-fpermissive] 28 29BLE_notify_mod:117:45: error: cannot bind rvalue '(uint32_t)(& strSend)' to 'uint32_t& {aka unsigned int&}' 30 31C:\user\AppData\Local\Arduino15\packages\esp32\hardware\esp32\1.0.4\libraries\BLE\src/BLECharacteristic.h:77:7: note: candidate: void BLECharacteristic::setValue(int&) <near match> 32 33 void setValue(int& data32); 34 35 ^ 36 37C:\user\AppData\Local\Arduino15\packages\esp32\hardware\esp32\1.0.4\libraries\BLE\src/BLECharacteristic.h:77:7: note: conversion of argument 1 would be ill-formed: 38 39BLE_notify_mod:117:45: error: invalid conversion from 'String*' to 'int' [-fpermissive] 40 41BLE_notify_mod:117:45: error: cannot bind rvalue '(int)(& strSend)' to 'int&' 42 43BLE_notify_mod:123:45: error: no matching function for call to 'setValue(String*)' 44 45C:\user\AppData\Local\Arduino15\packages\esp32\hardware\esp32\1.0.4\libraries\BLE\src/BLECharacteristic.h:75:7: note: candidate: void BLECharacteristic::setValue(uint16_t&) <near match> 46 47 void setValue(uint16_t& data16); 48 49 ^ 50 51C:\user\AppData\Local\Arduino15\packages\esp32\hardware\esp32\1.0.4\libraries\BLE\src/BLECharacteristic.h:75:7: note: conversion of argument 1 would be ill-formed: 52 53BLE_notify_mod:123:45: error: invalid conversion from 'String*' to 'uint16_t {aka short unsigned int}' [-fpermissive] 54 55BLE_notify_mod:123:45: error: cannot bind rvalue '(uint16_t)((int)(& strSend))' to 'uint16_t& {aka short unsigned int&}' 56 57C:\user\AppData\Local\Arduino15\packages\esp32\hardware\esp32\1.0.4\libraries\BLE\src/BLECharacteristic.h:76:7: note: candidate: void BLECharacteristic::setValue(uint32_t&) <near match> 58 59 void setValue(uint32_t& data32); 60 61 ^ 62 63C:\user\AppData\Local\Arduino15\packages\esp32\hardware\esp32\1.0.4\libraries\BLE\src/BLECharacteristic.h:76:7: note: conversion of argument 1 would be ill-formed: 64 65BLE_notify_mod:123:45: error: invalid conversion from 'String*' to 'uint32_t {aka unsigned int}' [-fpermissive] 66 67BLE_notify_mod:123:45: error: cannot bind rvalue '(uint32_t)(& strSend)' to 'uint32_t& {aka unsigned int&}' 68 69C:\user\AppData\Local\Arduino15\packages\esp32\hardware\esp32\1.0.4\libraries\BLE\src/BLECharacteristic.h:77:7: note: candidate: void BLECharacteristic::setValue(int&) <near match> 70 71 void setValue(int& data32); 72 73 ^ 74 75C:\user\AppData\Local\Arduino15\packages\esp32\hardware\esp32\1.0.4\libraries\BLE\src/BLECharacteristic.h:77:7: note: conversion of argument 1 would be ill-formed: 76 77BLE_notify_mod:123:45: error: invalid conversion from 'String*' to 'int' [-fpermissive] 78 79BLE_notify_mod:123:45: error: cannot bind rvalue '(int)(& strSend)' to 'int&' 80 81exit status 1 82 83no matching function for call to 'setValue(String*)' 84 85 86
該当のソースコード
以下コード全体です
Arduino
1 2#include <BLEDevice.h> 3#include <BLEServer.h> 4#include <BLEUtils.h> 5#include <BLE2902.h> 6 7BLEServer* pServer = NULL; 8BLECharacteristic* pCharacteristic = NULL; 9bool deviceConnected = false; 10bool oldDeviceConnected = false; 11int value = 0; 12 13#define SERVICE_UUID "4fafc201-1fb5-459e-8fcc-c5c9c331914b" 14#define CHARACTERISTIC_UUID "beb5483e-36e1-4688-b7f5-ea07361b26a8" 15 16 17class MyServerCallbacks: public BLEServerCallbacks { 18 void onConnect(BLEServer* pServer) { 19 deviceConnected = true; 20 }; 21 22 void onDisconnect(BLEServer* pServer) { 23 deviceConnected = false; 24 } 25}; 26 27void setup() { 28 Serial.begin(115200); 29 30 // Create the BLE Device 31 BLEDevice::init("ESP32_BLE"); 32 33 // Create the BLE Server 34 pServer = BLEDevice::createServer(); 35 pServer->setCallbacks(new MyServerCallbacks()); 36 37 // Create the BLE Service 38 BLEService *pService = pServer->createService(SERVICE_UUID); 39 40 // Create a BLE Characteristic 41 pCharacteristic = pService->createCharacteristic( 42 CHARACTERISTIC_UUID, 43 BLECharacteristic::PROPERTY_READ | 44 BLECharacteristic::PROPERTY_WRITE | 45 BLECharacteristic::PROPERTY_NOTIFY | 46 BLECharacteristic::PROPERTY_INDICATE 47 ); 48 49 // Create a BLE Descriptor 50 pCharacteristic->addDescriptor(new BLE2902()); 51 52 // Start the service 53 pService->start(); 54 55 // Start advertising 56 BLEAdvertising *pAdvertising = BLEDevice::getAdvertising(); 57 pAdvertising->addServiceUUID(SERVICE_UUID); 58 pAdvertising->setScanResponse(false); 59 //pAdvertising->setMinPreferred(0x0); // set value to 0x00 to not advertise this parameter 60 pAdvertising->setMinPreferred(0x06); // functions that help with iPhone connections issue 61 pAdvertising->setMinPreferred(0x12); 62 BLEDevice::startAdvertising(); 63 Serial.println("Waiting a client connection to notify..."); 64 65} 66 67void loop() { 68 // notify changed value 69 String strSend; 70 if (deviceConnected) { 71 Serial.println(value); 72 if (value % 2 == 0){ 73 strSend = "Count = " + String(value) + " -> Even"; 74 Serial.println(strSend); 75 pCharacteristic->setValue(&strSend); 76 //pCharacteristic->setValue("Number -> Even"); 77 78 } else if (value % 2 == 1){ 79 strSend = "Count = " + String(value) + " -> Odd"; 80 Serial.println(strSend); 81 pCharacteristic->setValue(&strSend); 82 //pCharacteristic->setValue("Number -> Odd"); 83 } 84 85 pCharacteristic->notify(); 86 value++; 87 delay(500); 88 } 89 // disconnecting 90 if (!deviceConnected && oldDeviceConnected) { 91 delay(500); // give the bluetooth stack the chance to get things ready 92 pServer->startAdvertising(); // restart advertising 93 Serial.println("start advertising"); 94 oldDeviceConnected = deviceConnected; 95 } 96 // connecting 97 if (deviceConnected && !oldDeviceConnected) { 98 // do stuff here on connecting 99 oldDeviceConnected = deviceConnected; 100 } 101}
試したこと
以下のコードで、奇数:Number -> Even,偶数:Number -> Odd とデータ受信する事まで確認が取れました。
Arduino
1 2if (value % 2 == 0){ 3 pCharacteristic->setValue("Number -> Even"); 4} else if (value % 2 == 1){ 5 pCharacteristic->setValue("Number -> Odd"); 6pCharacteristic->notify(); 7
補足情報(FW/ツールのバージョンなど)
Arduino IDE 1.8.13
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/12/15 22:15
2021/01/11 06:18