0

So basically I'm sending this struct data from my Arduino to my RPi, both fitted with an NRF24L01 module. Library on the Arduino is tmrh20, and on the RPi, BLavery.

struct values{
       char nameSensor[4]; // user inputs name of sensorNode e.g. "Home"
       float tempToSend;
       float humiToSend;
       float lightToSend;
       float soilMoist;
       float soilTemp;
       float gasSensor;
       int motionSensor; //struct size should be max 32 bytes for the RF24 pipe
};
struct values dataToSend;
//dataToSend.nameSensor=saved_nameSensor; //from EEPROM
//...
radio.write(&dataToSend,sizeof(dataToSend));

A few months ago, all I had to do was transmit temp and humidity float array from the Arduino, and I found the following code to work:

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

I'll just get right to it. I don't know how to convert/decode this struct into their respective data types in RPi. I don't even know where to begin, I could not find any Python code that receives a struct.

The only example I found was this post on Reddit: https://www.reddit.com/r/arduino/comments/juivxw/sending_struct_with_nrf24l01/

But in this post, both the receiver and transmitter are in Arduino architecture.

Does anyone know of a way to decode struct data similar to the float array struct.unpack above?

3
  • Well, it is easy to write you own encoder and decoder, to translate between a string of text characters and and it higher level structured info. I would suggest to use python for rapid prototyping. Python dictionary (sort of C struct) is many time more expressive that C struct. Ref: How can Rpi communicate with Arduino in RF (Radio Frequency), Say Using nRFL2401+? - Asked 2 years, 3 months ago raspberrypi.stackexchange.com/questions/108959/…
    – tlfong01
    Jun 16 at 3:13
  • This is a general programming question.
    – joan
    Jun 16 at 7:32
  • Could you update the question with example data? It would be good to know what the expected values are for a given receivedMessage value. I think the struct needs to be changed to data = struct.unpack('<4c6fl',receivedMessage) for the new struct.
    – ukBaz
    Jul 10 at 7:06

1 Answer 1

0

Basically you can't. There is a python module struct but this an advanced module that requires quite a bit of python expertise. Binary representation differs between platforms.

You would be better to write c/c++ on the Pi or learn python and transmit character arrays from the Arduino rather than binary data.

1
  • Thank you, if I were to transmit character arrays, I think I'll have to send some of them one at a time since their total size is 125 bytes. Would this be okay, to send data to NRF24 pipe one at a time? Jun 16 at 9:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.