Receiving float from Arduino "BLEFloatCharacteristic" via BLE
-
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! -
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 -
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 argumentconst QByteArray &value
I had to modify your suggestion in :
float fNum = *reinterpret_cast<const float*>(value.constData());