Sending/Receiving floats or big numbers between Arduino and RPI using I2C and C++

Hello guys,

I have been doing a lot of research about this and I haven't found a solution yet. My problem is the next one:

I am using I2C to interface an Arbotix Robocontroller (16MHz AVR microcontroller (ATMEGA644p)) with a RPI and I want to use C++ in the RPI side (Not Python). The reason why is basically because I have an entire algorithm that I need to run in RPI (using OpenCV) and written in C++, that's why I need C++ in RPI. I found this code online (Sending float from arduino to raspberry pi using Wire (arduino) and smbus (python) libraries · GitHub), that works perfectly but uses Python in RPI. I am wondering if I could do something similar in C++.

In addition I have tried this code too (Connecting an Arduino to a Raspberry PI using I2C – Peter Mount's Blog) that uses C++ in RPI, but it receives only 1 byte, no negative values, no floats, only from 0-255. I have tried to modify the code by requesting more than 1 byte but no luck, it seems that is stores always in the same buffer.

I attach here the last code that I have tried in Arduino, trying to send the number in different packages but for some reason the RPI always skips the first number sent (b1) and only displays b2. I guess is more a problem with the receiver side rather than the sender side, but I would like to know your opinion and if anyone can help me. BTW the RPI is the Master and the Arbotix the Slave.

Thank you so much.

#include <Wire.h> 
#define SLAVE_ADDRESS 0x04
unsigned int number = 0;
int state = 0;
int num;
int rem;

 
double temp;
 
void setup() {
 pinMode(0, OUTPUT);
 Serial.begin(9600);
    
 // initialize i2c as slave
 Wire.begin(SLAVE_ADDRESS);
 
 // define callbacks for i2c communication
 Wire.onReceive(receiveData);
 Wire.onRequest(sendData);
}
 
void loop() {
 delay(100);
}
 
// callback for received data
void receiveData(int byteCount){
 
 while(Wire.available()) {
  number = Wire.read();
  if (number == 1){
   if (state == 0){
    digitalWrite(0, HIGH); // set the LED on
    state = 1;
   } else{
    digitalWrite(0, LOW); // set the LED off
    state = 0;
   }
  }
 
  if(number==2) {
   number = 0x123; 
   //int n=256;
   //num=number/n;
   //rem=number%n;
   //Serial.println(b1);
   //Serial.println(b2);
   //num=num<<4;
   //rem+=num;
   //Wire.write(num);
   //Wire.write(rem);
  }
 }
}
 
// callback for sending data
void sendData(){
   //Wire.write(num);
   unsigned char b1=0x12;
   unsigned char b2=0x34;
   //b1=(0xFF&number);
   //b2=((0xFF00&number)>>8);
   Wire.write(b1);
   Wire.write(b2);
   Serial.println(b1);
   Serial.println(b2);
 //Wire.write(number);
}
void sendData(){
   //Wire.write(num);
   unsigned char b1=0x12;
   unsigned char b2=0x34;
   //b1=(0xFF&number);
   //b2=((0xFF00&number)>>8);
   Wire.write(b1);
   Wire.write(b2);
   Serial.println(b1);
   Serial.println(b2);
 //Wire.write(number);
}

RPI always skips the first number sent (b1) and only displays b2

Yep, that's what happens.

See my page: Gammon Forum : Electronics : Microprocessors : I2C - Two-Wire Peripheral Interface - for Arduino

On that:

You can only do one Wire.write in a requestEvent!

If you want to send more than one thing, send a struct (with a single Wire.write). Be warned that a float on the Arduino, and a float on the Pi may have different internal representation.

   Serial.println(b1);

Serial.println(b2);

Also read the bit about not doing serial prints in an interrupt service routine.