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);
3
  • 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); Commented Jan 5, 2018 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). Commented Jan 5, 2018 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. Commented Jan 5, 2018 at 13:01

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

5 Comments

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.
But what if I want to work with this variable on Rpi later on? Converting the string back to float on Rpi side?
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?
For converting a float to a string, I recommend using sprintf(buf, "%a", value); or sprintf(buf, "%.*e", FLT_DECIMAL_DIG - 1, value); Details
@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.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.