I'm using a Bluefruit Feather to send information from a temperature sensor to a Python script on my computer, over BLEUART.
I have a working version of this code for an accelerometer, the only difference being that the information sent is 4 ints (for the accelerometer: x, y, z, id), vs 2 floats for the temperature sensor (temp, float id). I have checked the values that the sensor sends over the Serial monitor and they are correct. The problem occurs when the floats are unpacked in Python: they are a factor of approximately 1.36*e-45
off.
In both cases, I store the variables in a struct and send this struct over BLE. The feather code looks like:
void sendData(){
int numVals = 2;
int vals[numVals];
vals[0] = temp;
vals[1] = id; //1.0
Serial.println(temp);
Serial.println(id);
int cnt = numVals * sizeof(float) ;
uint8_t buf[cnt];
for (int _i=0; _i<numVals; _i++)
memcpy(&buf[_i*sizeof(float)], &vals[_i], sizeof(float));
bleuart.write( buf, cnt );
}
And on the Python end it looks like:
def received(data):
floats = struct.unpack('ff', data)
print('Received:', floats)
This is really mysterious! I have a feeling it's something to do with how floats are represented between C and Python, but have tried changing the values to doubles / unsigned longs and it's either crashed, or had similar issues.