How to make sense of Struct data received from Arduino in RPi via NRF24?



  • Hi guys,

    I know that for a simple transmission e.g. humidity and temperature, the Arduino can simply fit these into a float array and send it to the RPi.

    The RPi can then execute the following code snippet to get the value:

    DataBytes=8
    receivedMessage=bytearray(DataBytes)
    radio.read(receivedMessage,radio.getDynamicPayloadSize())
    data = struct.unpack('ff',receivedMessage)
    humidity=data[0]
    temperature=data[1]
    

    Arduino snippet:

    struct values{
    char name[15];
    float temp;
    float humi;
    float light;
    float soilMoist;
    float soilTemp;
    float gasSensor;
    float pirSensor;
    };
    struct values DataToSend;
    DataToSend.temp=dht.ReadTemperature();
    //continue storing data in DataToSend
    radio.write(&DataToSend,sizeof(DataToSend))
    

    My question is now, how would I send this struct from the Arduino to the RPi with a similar method like the float array?



  • @nrf24_is_hard When I sent data between an Aduino and the RPi I discovered that I have to make the the receiving and the sending structs the same size AND the variables be on 4 byte boundaries. Change your char name[15] to char name[16]

    Another challenge is that the number of bytes of type int are different. If I remember correctly, Arduino Uno is 2 bytes and RPi is 4 bytes.

    To make matters worse, some compilers make the least significant byte the first byte of a word and others make it the last byte of a word. eg, sending the value 1 would be received as 16777216 (0x00000001 vs 0x01000000)

    There are a lot more gotcha's 😉

    OSD



Suggested Topics

61
Online

11.4k
Users

11.1k
Topics

112.7k
Posts