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
-
π¬ ESP-LINK ESP8266 WeMos D1 Mini Adapter Board
OpenHardware.io β’ 7 Nov 2016, 17:03 β’ openhardware.io 9 Feb 2017, 04:56 -
π¬ Hover SmartHome sensor and controller system
OpenHardware.io β’ 26 Sept 2016, 03:15 β’ openhardware.io 26 Sept 2016, 03:15 -
π¬ UtiliPi
OpenHardware.io β’ 27 Aug 2018, 08:27 β’ openhardware.io 27 Aug 2018, 08:27 -
Update RF24 library to latest version
Bug Reports β’ 23 Mar 2014, 23:37 β’ andriej 24 Mar 2014, 22:52 -
π¬ Baby BusyCube design journal
OpenHardware.io β’ 23 Jul 2017, 22:46 β’ openhardware.io 19 Aug 2017, 14:49 -
π¬ MDMSNode "Lighting" Rev 1.1
OpenHardware.io β’ 27 Jul 2017, 06:45 β’ openhardware.io 30 Nov 2018, 11:56 -
π¬ Epi 32U4 Arduino-compatible, 4-layer version
OpenHardware.io β’ 4 Feb 2023, 13:41 β’ openhardware.io 4 Feb 2023, 13:41 -
Garage Door opener
Vera β’ 27 Dec 2014, 16:45 β’ discorover0 27 Dec 2014, 21:50