Invalid initialization of reference of type 'SenseMeData&' from expression of type 'BLEDevice'

*Please can help me. I want to scan multiple Nano 33 Ble sense and Nicla Sense Me cards. Then they connect to post the data.*

*Here are the errors they show me on the screen.*
**test__arduino_portenta.ino: In function 'void loop()':
test__arduino_portenta:151:28: error: invalid initialization of reference of type 'SenseMeData&' from expression of type 'BLEDevice'
     printData(peripherals[i] ,true );
               ~~~~~~~~~~~~~^
\test__arduino_portenta.ino:265:6: note: in passing argument 1 of 'void printData(SenseMeData&, bool)'
 void printData(SenseMeData& senseMeData, bool printMode)
      ^~~~~~~~~
\test__arduino_portenta.ino: In function 'void connect_to_peripherals(int)':
\test__arduino_portenta:240:68: error: invalid conversion from 'const char*' to 'uint16_t {aka short unsigned int}' [-fpermissive]
      auto isArduino = checkCompany(peripherals[i], ArduinoCompanyID);
                                                                    ^
\test__arduino_portenta.ino:183:6: note:   initializing argument 2 of 'bool checkCompany(const BLEDevice&, uint16_t)'
 bool checkCompany(const BLEDevice& peripheral, const uint16_t companyId)
      ^~~~~~~~~~~~
\test__arduino_portenta:243:33: error: 'peripheral' was not declared in this scope
             auto data = getData(peripheral[i]);
                                 ^~~~~~~~~~
\test__arduino_portenta.ino:243:33: note: suggested alternative: 'peripherals'
             auto data = getData(peripheral[i]);
                                 ^~~~~~~~~~
                                 peripherals
\test__arduino_portenta:244:28: error: 'class BLEDevice' has no member named 'data'
             peripherals[i].data;
                            ^~~~
\test__arduino_portenta:246:32: error: invalid initialization of reference of type 'SenseMeData&' from expression of type 'BLEDevice'
         printData(peripherals[i], true);
                   ~~~~~~~~~~~~~^
\test__arduino_portenta.ino:265:6: note: in passing argument 1 of 'void printData(SenseMeData&, bool)'
 void printData(SenseMeData& senseMeData, bool printMode)
      ^~~~~~~~~
exit status 1
invalid initialization of reference of type 'SenseMeData&' from expression of type 'BLEDevice'
** 
//#include 
#include <ArduinoBLE.h>           // Bluetooth Library
#include <Arduino_DebugUtils.h>
#include <map>
//----------------------------------------------------------------------------------------------------------------------

#define ArduinoCompanyID "0x09A3"

// define max number of peripherals we want Scan 
#define BLE_MAX_PERIPHERALS 15  
// max interval for find peripherals
#define BLE_SCAN_INTERVALL 10000

// array of scaned peripherals
BLEDevice peripherals[BLE_MAX_PERIPHERALS];
// Build with --build-property "build.extra_flags=-DSNS_DBG_LEVEL=DBG_INFO -DSNS_DBG_TIMESTAMP=1"
#if !defined(SNS_DBG_LEVEL)
#define SNS_DBG_LEVEL DBG_INFO
#endif

#if !defined(SNS_DBG_TIMESTAMP)
#define SNS_DBG_TIMESTAMP 1
#endif

#if !defined(SNS_PRINT_CSV)
#define SNS_PRINT_CSV 0
#endif

constexpr auto debugLevel { SNS_DBG_LEVEL };
constexpr bool debugTimestamp { SNS_DBG_TIMESTAMP };
constexpr bool printModeCSV { SNS_PRINT_CSV };


//  final number of connected peripherals
int peripheralsConnected = 0;

// array of valid name of peripherals
String validperipheralNames[7] = { "Nano 1 data" ,"Nano 2 data" ,"NiclaSenseME Advertising Data","NANO_3" ,"Nicla"  ,"NANO_6" ,"NANO_7" } ;


struct __attribute__((packed)) SenseMeData {
    String address;
    unsigned long latest;
    unsigned long elapsed;
    uint16_t company;
    float temperature;
    int16_t acc_x;
    int16_t acc_y;
    int16_t acc_z;
    int16_t gyro_x;
    int16_t gyro_y;
    int16_t gyro_z;

    // A simple constructor
    SenseMeData(const uint8_t* data)
    {
        memcpy((void*)&company, data, 2);
        memcpy((void*)&temperature, data + 2, sizeof(float));
        memcpy((void*)&acc_x, data + 6, sizeof(int16_t));
        memcpy((void*)&acc_y, data + 8, sizeof(int16_t));
        memcpy((void*)&acc_z, data + 10, sizeof(int16_t));
        memcpy((void*)&gyro_x, data + 12, sizeof(int16_t));
        memcpy((void*)&gyro_y, data + 14, sizeof(int16_t));
        memcpy((void*)&gyro_z, data + 16, sizeof(int16_t));
    };

    SenseMeData(const String addr, const unsigned long now, const unsigned long prev, const uint8_t* data)
    {
        address = addr;
        latest = now;
        elapsed = now - prev;

        memcpy((void*)&company, data, 2);
        memcpy((void*)&temperature, data + 2, sizeof(float));
        memcpy((void*)&acc_x, data + 6, sizeof(int16_t));
        memcpy((void*)&acc_y, data + 8, sizeof(int16_t));
        memcpy((void*)&acc_z, data + 10, sizeof(int16_t));
        memcpy((void*)&gyro_x, data + 12, sizeof(int16_t));
        memcpy((void*)&gyro_y, data + 14, sizeof(int16_t));
        memcpy((void*)&gyro_z, data + 16, sizeof(int16_t));
    };
};
//constexpr uint16_t ArduinoCompanyID { 0x09A3 };
std::map<String, unsigned long> prevMillis {};

constexpr auto changeStatusInterval { 500ul };
auto changeStatusNow { 0ul };

// set of central 
void setup()
{
  Debug.setDebugLevel(debugLevel);
    if (debugTimestamp)
        Debug.timestampOn();

    Serial.begin(115200);
    for (const auto timeout = millis() + 2500; millis() < timeout && !Serial; delay(250))
        ;

    if (!BLE.begin()) {
        Debug.print(DBG_WARNING, "Starting BLE failed!");

        while (1);
    }

    if (!printModeCSV)
        Debug.print(DBG_INFO, "Arduino BLE Manufacturer Data Sensor");

    changeStatusNow = millis() + changeStatusInterval;
  BLE.begin();

  BLE.scanForUuid( ArduinoCompanyID );  // scaning for finding peripherals with the input service UUID

  int peripheralCounter = 0;  // count founded peripherals 
  unsigned long startMillis = millis();

  // Continue until either the number of repetitions or the number of peripherals reaches its maximum
  while ( millis() - startMillis < BLE_SCAN_INTERVALL && peripheralCounter < BLE_MAX_PERIPHERALS )
  {
    //Serial.println("wile is started");
    BLEDevice peripheral = BLE.available();  // return one BLEDevice( peripherals)
    //Serial.println("Serching started");
    Serial.println(BLE.available());
    if ( peripheral ) { // if peripherals exist 
      if (isValidPeripheral(peripheral.localName())) // check name of founded peripheral
     {
         if( !is_prepheral_repeated( peripheral, peripheralCounter) ){// If device was not found before
          
           peripherals[peripheralCounter] = peripheral; //  add peripheral to array of founded peripherals
           peripheralCounter++;  // increase founded peripherlas number 
           Serial.println(peripherals[peripheralCounter]);
         }
      }
    }
 // Serial.println("wile is End");
  }
  
  BLE.stopScan(); // stop scanning for new peripherals 
  Serial.println ("Stooop scaaaan") ;
  connect_to_peripherals ( peripheralCounter ) ;
  Serial.println("connect_to_peripherals");
}



void loop()
{
  // print the all data  peripherals 
  for ( int i = 0; i < peripheralsConnected; i++ )
  {  
    printData(peripherals[i] ,true );
    delay(1000);
  }
}


boolean is_prepheral_repeated( BLEDevice peripheral ,int peripheralCounter ) {
  
    Serial.println( peripheral.localName() ) ;
    boolean peripheralAlreadyFound = false;  //boolean use for detect duplication of peripheral

    for ( int i = 0; i < peripheralCounter; i++ ) // Checks this peripheral has already been found or not 
    {
        if ( peripheral.address() == peripherals[i].address() )
        {
          peripheralAlreadyFound = true;
          break ;
        }
    }
    return peripheralAlreadyFound ; 
    
}

bool checkManufacturerData(const BLEDevice& peripheral)
{
    // Skip packets w/o Manufacturer Data
    if (!peripheral.hasManufacturerData())
        return false;

    return true;
}

bool checkCompany(const BLEDevice& peripheral, const uint16_t companyId)
{
    // Get the raw advertising data
    auto manufLen = peripheral.manufacturerDataLength();
    uint8_t manufData[manufLen] {};
    auto _ret = peripheral.manufacturerData(manufData, manufLen);

    SenseMeData senseMeData { manufData };

    // Filter by Company ID
    if (senseMeData.company != companyId)
        return false;

    return true;
}

SenseMeData getData(const BLEDevice& peripheral)
{
    const auto now = millis();

    // Get the raw advertising data
    const auto manufLen = peripheral.manufacturerDataLength();
    uint8_t manufData[manufLen] {};
    const auto _ret = peripheral.manufacturerData(manufData, manufLen);

    const auto hexData = toHexString(manufData, manufLen);
    Debug.print(DBG_DEBUG, "Manufacturer Data: %s", hexData.c_str());

    const auto prev = prevMillis[peripheral.address()];
    prevMillis[peripheral.address()] = now;

    const SenseMeData senseMeData { peripheral.address(), now, prev, manufData };
    return senseMeData;
}

String toHexString(const uint8_t data[], const size_t len)
{
    String hexString;
    hexString.reserve(len * 2 + 1);

    for (auto i = 0u; i < len; i++) {
        if (data[i] < 0x10)
            hexString += "0";
        hexString += String(data[i], HEX);
    }

    return hexString;
}


void connect_to_peripherals (int peripheralCounter ) {

  for ( int i = 0; i < peripheralCounter; i++ ) // for each founded peripherals 
  {
    Serial.println(peripherals[i].localName()) ;
    peripherals[i].connect() ; // connect to peripheral
    peripherals[i].discoverAttributes();
     auto isArduino = checkCompany(peripherals[i], ArduinoCompanyID);
       Debug.print(DBG_DEBUG, "CHECK_COMPANY: %s", isArduino ? "OK" : "KO");
        if (isArduino) {
            auto data = getData(peripheral[i]);
            peripherals[i].data;
        // Do something with data
        printData(peripherals[i], true);
        
        } else {
        Serial.println("no character..."); // if characteristic not exist print this
        }
  }
  peripheralsConnected = peripheralCounter;
}

// check input name is valid peripherals name
boolean isValidPeripheral( String fullname ) {
  for( int i= 0 ; i < 7 ; i++){
    if( fullname == validperipheralNames[i]) {
        return true ;
    }
  }
   return false ;
}

void printData(SenseMeData& senseMeData, bool printMode)
{
    if (printMode == 0) {
        Debug.print(DBG_INFO, "=== === === === === === ===");
        Debug.print(DBG_INFO, "Address: %s", senseMeData.address.c_str());
        Debug.print(DBG_INFO, "Temperature: %.2f", senseMeData.temperature);

        Debug.print(DBG_INFO, "=== === === === === === ===");
    } else {
        Debug.timestampOff();
        Debug.print(DBG_INFO, "%d,%d,%s,%.2f,%d,%d,%d,%d,%d,%d", senseMeData.latest, senseMeData.elapsed, senseMeData.address.c_str(), senseMeData.temperature, senseMeData.acc_x, senseMeData.acc_y, senseMeData.acc_z, senseMeData.gyro_x, senseMeData.gyro_y, senseMeData.gyro_z);
        Debug.timestampOn();
    }
}

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.