0
votes

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);
1
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
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
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

1 Answers

0
votes

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.