Sending multiple integers and floats through wireless (RF22)

Hello!

I am making a weather station. It will have outside unit and inside unit.

Between the units will be 433MHz communication (RF22 library).

I will need to extract my numbers from the data I will recieve. (and construct the data I willl send).

Here is an example:
I have:

float temp=25.68;
int humidity= 76;
unsigned int lux=40000;

I will have to convert it in this to be able to send the all at once:
uint8_t data[];
How do I do it? This is Byte array am I right?

Then I will recieve data into this buffer:

uint8_t buf[RH_RF22_MAX_MESSAGE_LEN];

And I will have to extract those integers and floats out of it.
How?

Thanks

Hi jule553648

You could define a struct to hold your three items of data and use a union to map those into a byte array.

http://www.cplusplus.com/doc/tutorial/other_data_types/

Regards

Ray

// Define a struct type for the data
struct myStruct_t
{
  float temp;
  int humidity;
  unsigned int lux;
};

// Define a union type to map from the struct to a byte buffer
union myUnion_t 
{
  myStruct_t s;
  uint8_t b[sizeof(myStruct_t)];
};

// Declare demo variables
myUnion_t dataFrom, dataTo;
uint8_t transferBuffer[sizeof(myStruct_t)] = {0};

void setup()
{
  Serial.begin(115200);

  // Initialise one of the structs
  dataFrom.s.temp = 25.68;
  dataFrom.s.humidity = 76;
  dataFrom.s.lux = 40000L;

  // Copy the bytes into the temporary buffer
  for (int i = 0; i < sizeof(myStruct_t); i++)
  {
    transferBuffer[i] = dataFrom.b[i];
  }

  // Copy the bytes from the buffer to the second struct
  for (int i = 0; i < sizeof(myStruct_t); i++)
  {
    dataTo.b[i] = transferBuffer[i];
  }

  Serial.println(dataTo.s.temp);
  Serial.println(dataTo.s.humidity);
  Serial.println(dataTo.s.lux);

}

void loop()
{
}

First sorry for long delay in my response. I had many other (sadly more important) things to do and I had no time to work on my weather station.

But now I tried your idea and it works! 8)

It is very elegant way to transfer data! I would never thought of that! (where do you guys learn this stuff?)

Thanks again for support!

What I would do is similar.

I just make a struct.

Then I make a char* and allocate the same size.

Then, I just copy the struct to char*.