Receiving Float From Arduino "BLEFloatCharacteristic" Via BLE
Maybe your like
- Home
- Qt Development
- General and Desktop
- Receiving float from Arduino "BLEFloatCharacteristic" via BLE
- Oldest to Newest
- Newest to Oldest
- Most Votes
- Reply as topic
- 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
A 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 &valueI 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
A Offline
A 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 -
A 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;A 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;A Offline
A Offline artwaw wrote on last edited by #6
@mpergand thanks.
For more information please re-read.
Kind Regards, Artur
1 Reply Last reply 0
- First post Last post Go to my next post
- Categories
- Recent
- Tags
- Popular
- Users
- Groups
- Search
- Get Qt Extensions
- Unsolved
Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.
Tag » Arduino Ble Float
-
Passing Float Values With Bluetooth - Arduino Forum
-
Sending Float Over BLE - Nano 33 IoT - Arduino Forum
-
Sending Multiple Floats Over BLE - Nano 33 BLE - Arduino Forum
-
Sending Lat And Long Via BLE - Nano 33 BLE - Arduino Forum
-
Nano 33 BLE, Bluetooth Values - Arduino Forum
-
CurieBLE Library | Arduino Documentation
-
Conversion From Float To Formatted Character Array On Arduino Nano ...
-
Get Continuously The Value Of A BLE Service, Communication ...
-
Best Way To Send Float Values Over BLE - Arduino - Stack Overflow
-
What Is The Correct Approach To Transmit Rapidly Changing Real-time ...
-
Sending A Float Value Through BLE With A Custom Characteristic #1755
-
Manage Float Data Received From Ble - MIT App Inventor Help
-
Can't Discover Arduino Nano 33 IOT With BLE Scanner