// 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()
{
}