Skip to content
  • MySensors
  • OpenHardware.io
  • Categories
  • Recent
  • Tags
  • Popular
Skins
  • Light
  • Brite
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Brand Logo
  1. Home
  2. General Discussion
  3. How to make sense of Struct data received from Arduino in RPi via NRF24?

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

Scheduled Pinned Locked Moved General Discussion
arduinoraspberry pirf24
2 Posts 2 Posters 19 Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    nrf24_is_hard
    wrote on last edited by
    #1

    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?

    OldSurferDudeO 1 Reply Last reply
    0
    • N nrf24_is_hard

      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?

      OldSurferDudeO Online
      OldSurferDudeO Online
      OldSurferDude
      wrote on last edited by
      #2

      @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

      1 Reply Last reply
      1
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      26

      Online

      11.7k

      Users

      11.2k

      Topics

      113.1k

      Posts


      Copyright 2025 TBD   |   Forum Guidelines   |   Privacy Policy   |   Terms of Service
      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • MySensors
      • OpenHardware.io
      • Categories
      • Recent
      • Tags
      • Popular