Kia Ora, I would love for some assistance on this matter:
I am using the Arduino Nano 33 BLE for my project. at the moment i am just playing around trying to figure out how the Bluetooth works, along with the Arduino BLE library.
I have been able to advertise and transmit data to my android phone, reading it with the "LightBlue App" which is Great!
I have an understanding of how the services and charastics work with the GATT profile, and what is needed for UUID etc.
But, I am stuck on something that I feel is simple and probably straight forward but I can't figure it out. So I ask here for help?
I want to send Coordinates from the Arduino Nano 33 BLE to the android device via Bluetooth.
But what happens is this: Arduino: Data = -37.667676 -----> Android: = -37
Just two variables come through?
I made sure that the Characteristic was the right variable type:
BLEFloatCharacteristic SailBoxTestChar("2AAE", // standard 16-bit characteristic UUID
BLERead | BLENotify);
I am fairly new to coding, so I may be just making a dumb mistake. I look forward to who has a solution or is interested in helping!
Tried this! the Andriod LightBlue App, Comes back with numbers random to what it should say...
Here is the code below:
#include <ArduinoBLE.h>
//0d6e489e-783b-41bd-b6cc-d3d5dcfa6b07
//894dccb7-fada-4bec-9af5-26713bb1db96
// Bluetooth® Low Energy Battery Service
BLEService SailBoxService("1821");
// Bluetooth® Low Energy Battery Level Characteristic
BLEFloatCharacteristic SailBoxTestChar("2AAE", // standard 16-bit characteristic UUID
BLERead | BLENotify); // remote clients will be able to get notifications if this characteristic changes
double GPS0LAT = -37.971255; // Example test Coordonate
double GPS1LAT = -37.671255;
long previousMillis = 0;
void setup() {
Serial.begin(9600); // initialize serial communication
while (!Serial);
pinMode(LED_BUILTIN, OUTPUT); // initialize the built-in LED pin to indicate when a central is connected
// begin initialization
if (!BLE.begin()) {
Serial.println("starting BLE failed!");
while (1);
}
/* Set a local name for the Bluetooth® Low Energy device
This name will appear in advertising packets
and can be used by remote devices to identify this Bluetooth® Low Energy device
The name can be changed but maybe be truncated based on space left in advertisement packet
*/
BLE.setLocalName("SailBoxService");
BLE.setAdvertisedService(SailBoxService); // add the service UUID
SailBoxService.addCharacteristic(SailBoxTestChar); // add the battery level characteristic
BLE.addService(SailBoxService); // Add the battery service
SailBoxTestChar.writeValue(GPS0LAT); // set initial value for this characteristic
/* Start advertising Bluetooth® Low Energy. It will start continuously transmitting Bluetooth® Low Energy
advertising packets and will be visible to remote Bluetooth® Low Energy central devices
until it receives a new connection */
// start advertising
BLE.advertise();
Serial.println("Bluetooth® device active, waiting for connections...");
}
void loop() {
// wait for a Bluetooth® Low Energy central
BLEDevice central = BLE.central();
// if a central is connected to the peripheral:
if (central) {
Serial.print("Connected to central: ");
// print the central's BT address:
Serial.println(central.address());
// turn on the LED to indicate the connection:
digitalWrite(LED_BUILTIN, HIGH);
// while the central is connected:
while (central.connected()) {
long currentMillis = millis();
// if 200ms have passed
if (currentMillis - previousMillis >= 200) {
previousMillis = currentMillis;
SailBoxTestChar.writeValue(GPS1LAT);
Serial.println(GPS1LAT);
}
}
// when the central disconnects, turn off the LED:
digitalWrite(LED_BUILTIN, LOW);
Serial.print("Disconnected from central: ");
Serial.println(central.address());
}
}
LightBlue does not know how to deal with ieee 754 floating point numbers. If you send the data as a float, not a double, you will see in LightBlue the receipt of 0x 5d af 16 c2,
When you enter that into a ieee converter program
you get -37.671253 when the hex values are entered in the correct endian order C216AF5D.
Oh yeah, I see what you mean! that makes more sense now. I have tried and it works. But the format does come out in an incorrect order to what the conversion program needs as you mentioned.
Do you have any ideas why this would be? Is it a light blue app problem or ?
LightBlue just reports the bytes as it receives them. The data type selection field does not deal with floats.
You may want to send the floating point numbers as text, as this makes the debugging and interpretation far more simple. The additional byte length and time taken by that for the transfer is often not relevant