A quick share of a solution and maybe a chip in from someone with a better way of doing it?
The problem was to send a float type variable from another MCU (Atmega 328P) to a Photon using I2C.
The sender encodes the float like this following receipt of the request from the Photon:
uint8_t sendbytes[sizeof(float)] = {0};
*(float*) (sendbytes) = batteryVoltage;
Wire.write(sendbytes[0]);
Wire.write(sendbytes[1]);
Wire.write(sendbytes[2]);
Wire.write(sendbytes[3]);
and the Photon (as central) receives like this:
float result = 0.0;
uint8_t result_byte[4];
uint8_t byte_count = 0;
requestBatteryVoltage();
Wire.requestFrom(9,4); //request 4 bytes from peripheral device address 9
while(Wire.available() && byte_count < 4)
{
result_byte[byte_count] = Wire.read();
byte_count++;
}
if (byte_count > 0) //Wire request returned 4 bytes - typecast to float
{
result = *(float *) (result_byte);
}
return result;