1

I recently purchased an nRF8001 Bluefruit LE and I am now trying to read/send data with an app. But I am not sure how I can write/send a double value. I want to send two double values at once and to successfully send this, do I have to convert these doubles to a byte array? If so I would very much appreciate how that would look!

This is documentation on how my module works: https://learn.adafruit.com/getting-started-with-the-nrf8001-bluefruit-le-breakout/nrf-uart-in-detail

Under the headline Writing data they give examples on how to send data but as far as I can see (I am a bit new to Arduino code) in their example they only send an int and a string.

I know how to read and set up the module, so it is only the writing/sending doubles that I am struggling with.

These are the two doubles that I wish to send and I control it with:

double valueOne;
double valueTwo;

The Bluetooth control:

Adafruit_BLE_UART BTLEserial = Adafruit_BLE_UART(ADAFRUITBLE_REQ, ADAFRUITBLE_RDY, ADAFRUITBLE_RST);

So if I for example do this:

BTLEserial.write(valueOne, valueTwo);

I get this message: no matching function for call to 'Adafruit_BLE_UART::write(double&, double&)'

So do I need to convert these two double values to a byte array and if so how would that look?

Appreciate every help, tips, code! :)

16
  • After the first successful message received, did you see the location refreshed on the serial monitor by the Serial.println(gps.location.lat(),6); ?
    – J. Piquard
    Jan 3, 2017 at 21:59
  • I see that once yes and also the Serial.println(gps.location.lng(),6); once
    – Martman
    Jan 3, 2017 at 22:01
  • Also the Serial.println(F("* Connected!")); once
    – Martman
    Jan 3, 2017 at 22:01
  • Seeing once Serial.println(F("* Connected!")); is normal, because the message is sent only when the status change. But are you sure that your gps.location.isUpdated() is true ?
    – J. Piquard
    Jan 3, 2017 at 22:07
  • I removed gps.location.isUpdated() in the if-statement but it still stops right when I connect my app to the bluetooth. I also added a Serial.println("test"); right at the top of the while loop and that stops once i connect the app with the bluetooth as well so there must be something else.
    – Martman
    Jan 3, 2017 at 22:10

2 Answers 2

1

As the link "Adafruit - nRF UART In Detail" describes, the function write(sendbuffer, sendbuffersize); is waiting for a buffer of uint8_t followed by the size of the message.

Here are the step--by-step solution to send the 2 doubles by using that function.

Step1 - convert the floating-point values into a char string.

In the Arduino environment, the standard format-specifier %f or %lf is not well-managed and the function dtostrf() is recommended.

The format is char * dtostrf (double __val, signed char __width, unsigned char __prec, char *__s) where:

  • __val is the floating-point value to be converted,
  • __s is the array of char to store the result,
  • __width is the number of characters to store the result,
  • __prec is the number of digit

The conversion is:

char sValueOne[16], sValueTwo[16];
char sBuffer[33];

strcpy(sBuffer,dtostrf(valueOne, 15, 8, sValueOne));
strcat(sBuffer,",");
strcat(sBuffer,dtostrf(valueTwo, 15, 8, sValueTwo));

Step2 - write the buffer over the BTLEserial object.

BTLEserial.write((uint8_t *)sBuffer, strlen(sBuffer));

Alternate output solution using BTLEserial.print() function.

BTLEserial.print(sBuffer);
12
  • so should I use a strcpyon the sValueOne and a strcaton sValueTwo? What does these strcpy and strcpy mean? What type of function is it? Btw, thanks a lot for the answer!! I learned a lot with your example
    – Martman
    Jan 3, 2017 at 20:04
  • Both functions strcpy() and strcat() are used to copy char strings. The first is used to initialize the buffer char sBuffer[33] and the second is used to concatenate the extra string to the existing. If valueOne= 8.89 and valueTwo=9.88, the first string is sValueOne = "8.89000000" and the second string is sValueTwo = "9.98000000" and the output buffer is sBuffer = "8.89000000,9.98000000".
    – J. Piquard
    Jan 3, 2017 at 20:14
  • How often can I write code? Is it possible to do it each 0.5 second?
    – Martman
    Jan 3, 2017 at 20:14
  • Ok. Thanks a lot for explaining that! my both doubles cointains 6 numbers after. I send coordinates looking like this: double latvalue; double lngvalue; latvalue = (gps.location.lat(),6); lngvalue = (gps.location.lng(),6);
    – Martman
    Jan 3, 2017 at 20:15
  • The period is highly depending of the real baudrate of your BTLEserial device. The conversion is enough fast for two samples per second.
    – J. Piquard
    Jan 3, 2017 at 20:19
0

I didn't try this, but since Adafruit_BLE_UART inherits from Stream, which in turn inherits from Print, you should be able to

BTLEserial.print(valueOne);
BTLEserial.print(",");
BTLEserial.println(valueTwo);
2
  • Codes uploads without any trouble! However the while-loop stops with my current code when I use your code as well. It works once (when I connect to the bluetooth with my phone) I edited the post with my source code if you want to check it out.
    – Martman
    Jan 3, 2017 at 21:43
  • If i completely remove write or print and connect to the bluetooth with my app the while loop keeps going but if i add your code or j.piquards code the while loop stops right away when i connect the app with the bluetooth
    – Martman
    Jan 3, 2017 at 23:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.