iT邦幫忙

0

Arduino BLE ESP32串接系統出問題

  • 分享至 

  • xImage
  •  

大家好,我是iceshadow,最近我在做室內導航系統,以10個ESP32串接而成的低功耗藍牙(Bluetooth Low Energy, BLE)感應系統加上iOS app介面,用iBeacon當作導航器協助導航。
其中感應系統是用Arduino去撰寫的,但一直有語法過不了的問題。

這邊是全部的程式碼:

#include <BLEDevice.h>
#include <BLEUtils.h>
#include <BLEScan.h>
#include <BLEClient.h>
#include <unordered_map>
#include <string>
#include <iostream>

#define TARGET_DEVICES 10

// 目標設備的名稱和對應的 MAC 地址
const char *targetDeviceNames[TARGET_DEVICES] = {
  "ESP32_Device_1", "ESP32_Device_2", "ESP32_Device_3", "ESP32_Device_4",
  "ESP32_Device_5", "ESP32_Device_6", "ESP32_Device_7", "ESP32_Device_8",
  "ESP32_Device_9", "ESP32_Device_10"};
  

// 目標設備的 MAC 地址
const char *targetMacAddresses[TARGET_DEVICES] = {
    "CC:AA:00:33:66:77", "CC:AA:00:33:66:78", "CC:AA:00:33:66:79", "CC:AA:00:33:66:7A",
    "CC:AA:00:33:66:7B", "CC:AA:00:33:66:7C", "CC:AA:00:33:66:7D", "CC:AA:00:33:66:7E",
    "CC:AA:00:33:66:7F", "CC:AA:00:33:66:80"};
    
// 儲存目標設備是否已找到
std::unordered_map<std::string, bool> foundDevices;

// BLE 掃描物件
BLEScan *pBLEScan;
int scanTime = 5; // 掃描時間(秒)

// 初始化目標設備的導航狀態
void initializeFoundDevices() {
    for (int i = 0; i < TARGET_DEVICES; i++) {
        foundDevices[targetMacAddresses[i]] = false; // 設定初始狀態為未找到
    }
}

// 封裝的掃描函數,帶有 MAC 地址篩選邏輯
BLEScanResults someFunctionThatReturnsBLEScanResults() {
    pBLEScan->setActiveScan(true); // 啟用主動掃描

    Serial.println("開始掃描 BLE 裝置...");
    // 使用非指標類型接收結果
    BLEScanResults* results = pBLEScan->start(scanTime, false); // 使用指標類型
    Serial.printf("找到 %d 個裝置\n", results->getCount());

    for (int i = 0; i < results->getCount(); i++) {
        BLEAdvertisedDevice device = results->getDevice(i);
        std::string macAddressStd = std::string(device.getAddress().toString().c_str());

        // 檢查是否為目標設備
        if (foundDevices.find(macAddressStd) != foundDevices.end() && !foundDevices[macAddressStd]) {
            foundDevices[macAddressStd] = true; // 標記為已找到
            Serial.printf("找到目標設備: Name=%s, Address=%s\n", device.getName().c_str(),
                          macAddressStd.c_str());
        }
    }
    return results; // 返回掃描結果
}

// 依照 MAC 地址順序導航設備
void navigateDevices() {
    for (int i = 0; i < TARGET_DEVICES; i++) {
        std::string macAddress = targetMacAddresses[i];

        // 如果設備已找到,則執行導航邏輯
        if (foundDevices[macAddress]) {
            Serial.printf("導航至設備: Name=%s, Address=%s\n", targetDeviceNames[i], macAddress.c_str());
            // 執行導航操作或其他邏輯
        } else {
            Serial.printf("設備未找到: Name=%s, Address=%s\n", targetDeviceNames[i], macAddress.c_str());
        }
    }
}

void setup() {
    Serial.begin(115200);
    BLEDevice::init("");
    pBLEScan = BLEDevice::getScan();

    initializeFoundDevices(); // 初始化目標設備的導航狀態
}

void loop() {
    // 掃描設備
    BLEScanResults* scanResults = someFunctionThatReturnsBLEScanResults();

    // 列出所有掃描到的設備
    for (int i = 0; i < scanResults->getCount(); i++) {
        BLEAdvertisedDevice device = scanResults->getDevice(i);
        Serial.printf("Device found: Name=%s, Address=%s\n",
                      device.getName().c_str(),
                      device.getAddress().toString().c_str());
    }

    // 按順序導航目標設備
    navigateDevices();

    // 清除掃描結果,準備下一輪
    pBLEScan->clearResults();

    Serial.println("等待 7 秒後再次掃描...");
    delay(7000); // 等待 7 秒
}

會有上面的寫法是因為,我想要以物件的方式直接以一個中控去感應其他9個ESP32去維持這個系統的暢通,然後再跟自己寫好的iOS導航應用程式與iBeacon串接,不過常常在指標的地方翻車。

出錯的Code如下:

/Users/lilywang/Library/CloudStorage/OneDrive-臺北醫學大學/論文研究方向/學位考試/project/Arduino/Central/Central.ino: In function 'BLEScanResults someFunctionThatReturnsBLEScanResults()':
/Users/lilywang/Library/CloudStorage/OneDrive-臺北醫學大學/論文研究方向/學位考試/project/Arduino/Central/Central.ino:58:12: error: could not convert 'results' from 'BLEScanResults*' to 'BLEScanResults'
   58 |     return results; // 返回掃描結果
      |            ^~~~~~~
      |            |
      |            BLEScanResults*
/Users/lilywang/Library/CloudStorage/OneDrive-臺北醫學大學/論文研究方向/學位考試/project/Arduino/Central/Central.ino: In function 'void loop()':
/Users/lilywang/Library/CloudStorage/OneDrive-臺北醫學大學/論文研究方向/學位考試/project/Arduino/Central/Central.ino:86:72: error: cannot convert 'BLEScanResults' to 'BLEScanResults*' in initialization
   86 |     BLEScanResults* scanResults = someFunctionThatReturnsBLEScanResults();
      |                                   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~
      |                                                                        |
      |                                                                        BLEScanResults

exit status 1

Compilation error: could not convert 'results' from 'BLEScanResults*' to 'BLEScanResults'

問過ChatGPT、查過Stackoverflow跟Arduino的論壇,仍然不斷地循環出錯,不知道是我的環境問題還是語法問題,這邊想要請教各路大神,解救碩士生脫離碩論苦海感謝看到這篇文章的大家。

以下是我查過的資源,提供大家參考:
https://forum.arduino.cc/t/error-no-match-for-operator-operand-types-are-eeref-and-string/875704
https://forum.arduino.cc/t/ble-scan-issue-blescan-does-not-name-a-type/1062924/7
https://blog.csdn.net/qq_35630119/article/details/122628094


圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 則留言

1
leon123858
iT邦新手 2 級 ‧ 2024-12-03 17:32:40

回傳指標就用指標接試試

我要留言

立即登入留言