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
-
Using Pin Change Interrupt (PCINT) for wakeup
Development • 3 Feb 2015, 11:55 • Dirk_H 11 Apr 2015, 18:37 -
Full-Duplex Network
My Project • 4 Dec 2014, 18:14 • sfam 20 Feb 2017, 19:33 -
TCP service for Raspberry Pi NRF24l01 direct connection
Hardware • 21 Feb 2016, 07:44 • Ivan Z 15 Mar 2016, 19:31 -
Arduino Pro mini + RF 433 MHz as Gateway
General Discussion • 8 Nov 2015, 20:47 • lrtsenar 9 Nov 2015, 02:37 -
💬 CM1k Breakout Board -- Neuromorphic Chip
OpenHardware.io • 3 Sept 2016, 01:24 • openhardware.io 29 Dec 2016, 17:48 -
How to check if serial gateway is operational ?
Development • 7 Jun 2015, 13:07 • joshmosh 8 Jun 2015, 18:31 -
💬 The Logger Machine - Short and long term serial logging
OpenHardware.io • 24 Nov 2017, 21:01 • openhardware.io 18 Dec 2018, 08:58 -
💬 Raspberry Pi Fan Breakout
OpenHardware.io • 20 Jul 2021, 13:31 • openhardware.io 20 Jul 2021, 13:31