# Arduino float to bytes for LoRa transmission

For a recent Arduino project; a gas sensor with a LoRa radio, I needed to transmit large, floating decimal values over the air from one LoRa radio to another. There doesn’t seem to be a lot of information out there for this, and the information that is out there, didn’t really explain things to me well enough to feel confident. But I finally got it working using unions. Here is how I understand it to work:

The basics, bulleted:

• Unions are ‘containers’ that can contain different data types (a float and a byte in our case)
• Unions consume only as much memory as the largest element (32 bits or 4 bytes in our case)
• All elements in a union share the same memory space

We’ll take advantage of the ‘shared memory’ aspect to make the union a little data type-converter where we put in a float and we take out a byte, or vice versa.

First, define the union:

union gasUnion
{
float gasFloat;
unsigned char gasBytes[4];
};

To put data in, we do the following:

gasUnion a; //declare the union
a.gasFloat = 99.9; //put the float into place

Now our union holds the value 99.9 expressed as either a float or four bytes. As long as we don’t put anything in the a.gasBytes[] array, it will contain the byte form of the float “99.9” because of the shared memory aspect of unions.

To transmit the 99.9 via LoRa, just pump the gasBytes[] into a LoRa payload:


//take bytes from the union and place into LoRa payload
GasPayLoad[3] = a.gasBytes[3];

On the receiving end, I created an identical union and did the same operation, but in reverse; pull bytes out of the LoRa payload, pump them into the union, and immediately pull a float out of the union:


//pull bytes out of LoRa payload

...

gasUnion a; //declare the union

//put bytes in
ValueNH3 = a.gasFloat; //take out a float