Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Receiving float from Arduino "BLEFloatCharacteristic" via BLE

Receiving float from Arduino "BLEFloatCharacteristic" via BLE

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 3 Posters 1.1k Views 3 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    giorgiogandolfi_
    wrote on last edited by
    #1

    Hello everyone,
    I'm currently experiencing a problem while sending gyroscope and accelerometer data from an Arduino Sense BLE to a QT application.

    The code I wrote on Arduino creates a single service containing two characteristics, one for sending the gyro data and one for the accelerometer data. Each one of them is a "BLEFloatCharacteristic", so a float value should be sent.

    Using the notify property of BLE, I connected each one of the characteristic to the signal "characteristicChanged", as per own QT guide on BLE.

    connect(ptr_dataService, &QLowEnergyService::characteristicChanged, this, &MainWindow::updateGyroscopeData);
    connect(ptr_dataService, &QLowEnergyService::characteristicChanged, this, &MainWindow::updateAccelerometerData);
    

    The SLOTs to get the data are very basic:

    void MainWindow::updateGyroscopeData(const QLowEnergyCharacteristic &c, const QByteArray &value)
    {
        if (c.uuid() != QBluetoothUuid(BLE_UUID_GYROSCOPE_CHARACTERISTIC))
            return;
    
        qDebug()<<"Gyro";
        qDebug()<<value;
    }
    //------------------------------------------------------------------------------
    void MainWindow::updateAccelerometerData(const QLowEnergyCharacteristic &c, const QByteArray &value)
    {
        if (c.uuid() != QBluetoothUuid(BLE_UUID_ACCELEROMETER_CHARACTERISTIC))
            return;
    
        qDebug()<<"Acc";
        qDebug()<<value;
    }
    

    What I get printed out is something like

    Gyro
    "\x00\x00z\xBF"
    Gyro
    "\x00\x00\xFA\xBD"
    Acc
    "\x00\x00\xDD<"
    Acc
    "\x00H^?"
    

    How can I get a float number instead?
    Thanks to everyone!

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mpergand
      wrote on last edited by mpergand
      #2

      Hi,

      Warning: we enter the twillight zone :)

      To convert QByteArray to float, I tried this:

          float f=12.5;
          QByteArray d((char*)&f,sizeof(f));
          
          float ff=*reinterpret_cast<float*>(d.data());
          qDebug()<<ff;
      

      prints 12.5 so far so good
      but what is the format of the float you send from the Ardiuno, a 4 bytes float like in C ?
      Which endianess little/big endian ?

      First try to print the QByteArray in a readable format with:
      qDebug()<<value.toHex();

      [EDIT] Seems to be valid floats:
      -0.976563 giro 1
      -0.12207 giro 2
      0.0269775 acc 1
      0.868774 acc2

      artwawA 1 Reply Last reply
      1
      • G Offline
        G Offline
        giorgiogandolfi_
        wrote on last edited by
        #3

        Yes, your suggestions worked to perfection, thank you very much!

        Adding only a small note for potential future viewers:
        because the functions I use to update gyro and accelerometer data get as argument

        const QByteArray &value
        

        I had to modify your suggestion in :

        float fNum = *reinterpret_cast<const float*>(value.constData());
        
        1 Reply Last reply
        1
        • M mpergand

          Hi,

          Warning: we enter the twillight zone :)

          To convert QByteArray to float, I tried this:

              float f=12.5;
              QByteArray d((char*)&f,sizeof(f));
              
              float ff=*reinterpret_cast<float*>(d.data());
              qDebug()<<ff;
          

          prints 12.5 so far so good
          but what is the format of the float you send from the Ardiuno, a 4 bytes float like in C ?
          Which endianess little/big endian ?

          First try to print the QByteArray in a readable format with:
          qDebug()<<value.toHex();

          [EDIT] Seems to be valid floats:
          -0.976563 giro 1
          -0.12207 giro 2
          0.0269775 acc 1
          0.868774 acc2

          artwawA Offline
          artwawA Offline
          artwaw
          wrote on last edited by
          #4

          @mpergand What is wrong with QByteArray::toFloat() that you offer the above?

          For more information please re-read.

          Kind Regards,
          Artur

          M 1 Reply Last reply
          0
          • artwawA artwaw

            @mpergand What is wrong with QByteArray::toFloat() that you offer the above?

            M Offline
            M Offline
            mpergand
            wrote on last edited by
            #5

            @artwaw
            As far as I know, it only works with ASCII string:

            char n[]="1.25";
            QByteArray d(n);
            bool ok;
            float f=d.toFloat(&ok);
            qDebug()<<f<<ok;
            
            artwawA 1 Reply Last reply
            2
            • M mpergand

              @artwaw
              As far as I know, it only works with ASCII string:

              char n[]="1.25";
              QByteArray d(n);
              bool ok;
              float f=d.toFloat(&ok);
              qDebug()<<f<<ok;
              
              artwawA Offline
              artwawA Offline
              artwaw
              wrote on last edited by
              #6

              @mpergand thanks.

              For more information please re-read.

              Kind Regards,
              Artur

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved