Converting a double to a unsigned char?

Hi.

I'm pretty new to all this. But I'm using 2 different libraries. The output from my sensor is a double and then I'm trying to send this value from my sensor to bluetooth device. But the bluetooth device require a usigned char?

My value could be something like this:

double sensorValue = 22.22

And then I would like to use the value of "sensorValue" like this:

ble_write(sensorValue);

But that's not possible.

I looked into the library and it tells me that it's a unsigned char:

void ble_write(unsigned char data);

So how can I "send" this value from my sensor to my bluetooth device?

celfa:
Hi.

I'm pretty new to all this. But I'm using 2 different libraries. The output from my sensor is a double and then I'm trying to send this value from my sensor to bluetooth device. But the bluetooth device require a usigned char?

My value could be something like this:

double sensorValue = 22.22

And then I would like to use the value of "sensorValue" like this:

ble_write(sensorValue);

But that's not possible.

I looked into the library and it tells me that it's a unsigned char:

void ble_write(unsigned char data);

So how can I "send" this value from my sensor to my bluetooth device?

OLD_THREAD_HERE

But the bluetooth device require a usigned char?

Doesn't the library support a smart function that can send an array (print(), maybe?)?

mrsummitville:
OLD_THREAD_HERE

I've already tried everything mentioned there.

PaulS:
Doesn't the library support a smart function that can send an array (print(), maybe?)?

Maybe. But I'm not sure. The only to thing I can see is this:

void ble_write(unsigned char data);
void ble_write_bytes(unsigned char *data, unsigned char len);

This is my complete code:

#include <SPI.h>
#include <EEPROM.h>
#include <boards.h>
#include <RBL_nRF8001.h>
#include <Wire.h>
#include <Adafruit_MLX90614.h>

Adafruit_MLX90614 mlx = Adafruit_MLX90614();

void setup()
{
  // Init. and start BLE library.
  ble_begin();

  // Enable serial debug
  Serial.begin(57600);
  mlx.begin();
}

unsigned char buf[16] = {0};
unsigned char len = 0;

void loop()
{  
  Serial.print("Ambient = "); Serial.print(mlx.readAmbientTempC());
  Serial.print("\tObject = "); Serial.print(mlx.readObjectTempC());

  Serial.println();
  
  ble_write(mlx.readObjectTempC());

  delay(1000);

  ble_do_events();
}

The serial part works fine, but my value received through bluetooth is just empty.

The only to thing I can see is this:

So:

char buff[10];
float likeDuck = 3.14159;

dtostrf(likeDuck, 8, 4, buff);

ble_write_bytes((unsigned char *)buff, strlen(buff));

but my value received through bluetooth is just empty.

Well, mlx.readObjectTempC() most likely does not return an unsigned char, so pretending that it does and being surprised when it doesn't is silly.

PaulS:
So:

char buff[10];

float likeDuck = 3.14159;

dtostrf(likeDuck, 8, 4, buff);

ble_write_bytes((unsigned char *)buff, strlen(buff));

Thanks for your reply :slight_smile: I appreciate that. But when I'm trying to do this, I got the following error: "expected constructor, destructor, or type conversion before '(' token" regarding this line:

dtostrf(likeDuck, 8, 4, buff);

Well, mlx.readObjectTempC() most likely does not return an unsigned char, so pretending that it does and being surprised when it doesn't is silly.

Yeah, I know that. Trust me, I've tried many different things. It was just a silly try from a newbie to illustrate what I was trying to do.

My mistake! I placed dtostrf outside my loop! Wooops. Sorry.

Thank you! Now it works like a charm!