0

I have a float variable on my Arduino slave and I want to send it to my Raspberry Pi 3 Model B master. What I know is that I probably need to send an array of bytes from Arduino and then read it properly on my Raspberry Pi. I had no luck using this so far.

What I don't know is how should I read it, since WiringPi libraries offer only int values in return (sending 0-255 works like a charm, though).

It has been done here, but for Python, and here. C has a different set of libraries for I2C and doesn't offer such thing as struct.

Is it possible to read an array of bytes from I2C using WiringPi and convert it to float in C?

Multiplying the number on Arduino (to get rid of comma) and dividing it on Raspberry Pi is okay as well, but the problem of sending more than 255 via WiringPi remains.

Here's how I send the data on Arduino:

void sendData(){
  Wire.write((byte*) &floatNumber, 4);
}

And here's how I can read 8-bit values (0-255) on Raspberry (Wire.write(129) - for example):

int fd;
int data;
wiringPiSetup();

fd=wiringPiI2CSetup(0x04);

data=wiringPiI2CRead(fd);

ui->lcdNumber->display(data);
dda
  • 6,030
  • 2
  • 25
  • 34
VapeKop
  • 349
  • 2
  • 4
  • 13
  • You PI code is reading an int, I think that's 32 bits on a PI, so you should be able to cast it to a float and it MIGHT work. `ui->lcdNumber->display((float)data);` – Code Gorilla Jan 05 '18 at 11:31
  • Nothing special about a float its just a collection of bits, are you able to move a collection of bits from one platform to the other? Then after that the C language is the C language either your compiler works or doesnt, just use the C language, this part of the question has nothing to do with the chips or platforms (a float is just a collection of bits that you manipulate). – old_timer Jan 05 '18 at 12:58
  • Like I wrote, I only had success in moving 8 bits so far (with given code) and I'm missing something in my code (probably on Raspberry Pi side) to move 32 bits. – VapeKop Jan 05 '18 at 13:01

1 Answers1

0

You can convert float values to ASCII-represented float values (string) before transmitting and convert the ASCII representation of the float value (string) back to a float on Raspberry Pi 3.

dda
  • 6,030
  • 2
  • 25
  • 34
Dark Sorrow
  • 1,681
  • 14
  • 37
  • 1
    Since `float` representation and/or byte-order might be different on both ends, this is the best! You don't want to waste time on low-level code to make these conversions at byte level. – meaning-matters Jan 05 '18 at 11:42
  • But what if I want to work with this variable on Rpi later on? Converting the string back to float on Rpi side? – VapeKop Jan 05 '18 at 11:50
  • Also, sending a String is more than 8bits (0-255) and that would require me to send an array of bytes just like I need for converting float, right? – VapeKop Jan 05 '18 at 11:51
  • For converting a `float` to a string, I recommend using `sprintf(buf, "%a", value);` or `sprintf(buf, "%.*e", FLT_DECIMAL_DIG - 1, value);` [Details](https://stackoverflow.com/q/16839658/2410359) – chux - Reinstate Monica Jan 05 '18 at 13:35
  • @VapeKop, You will have to send more data over I2C bus but this method guarantees accuracy and constancy of your data. You will not have worry about low level intricacies. On receiving data on Raspberry Pi you can convert this string back to float. – Dark Sorrow Jan 05 '18 at 14:02