Skip to main content
deleted 91 characters in body; edited title
Source Link
dda
  • 1.6k
  • 1
  • 12
  • 17

sending Sending struct from Arduino to Raspberry Pi - wrong types

I'm using Raspberry Pi and Arduinos for my home automation project where RaspiRaspberry Pi is the controler of Arduino nodes. I use nRF24 wireless transceivertransceivers to communicate these two.

struct TempSensorData
{
    uint32_t result;
    uint32_t temperature;
    uint32_t humidity;
};

from{ uint32_t result; uint32_t temperature; uint32_t humidity; };

From Raspberry to Raspberry everything went fine but now when I use Arduino as sender I get very strange results:

whileWhile on Raspberry it was

Can this be a problem with types?

Can this be a problem with types? Or with different type of architecture (like different sizes on 32bit3-2bit and 64bit64-bit arch)?

codeCode on Raspberry:

if (radio.available())
{
    // dump the payloads until we've got everything
    Message receivedData = {0};
    radio.read(&receivedData, sizeof(Message));
    TempSensorData data = receivedData.msgData.tempSensorData;
    std::cout << "received: status: " << data.result << ", temperature: " << data.temperature << " degrees, humidity: " << data.humidity << "%" << std::endl; //TODO here we have some strange numbers - check if we have proper types
}

before{ // dump the payloads until we've got everything Message receivedData = {0}; radio.read(&receivedData, sizeof(Message)); TempSensorData data = receivedData.msgData.tempSensorData; std::cout << "received: status: " << data.result << ", temperature: " << data.temperature << " degrees, humidity: " << data.humidity << "%" << std::endl; //TODO here we have some strange numbers - check if we have proper types }

Before that I have:

radio.begin();
 
radio.setPALevel(RF24_PA_LOW);
radio.setChannel(0x4c);
 
radio.openReadingPipe(1, RASPI_READ_ADDR);
radio.openWritingPipe(RASPI_WRITE_ADDR);

radio.enableDynamicPayloads();
radio.setAutoAck(true);
radio.powerUp();
radio.startListening();

andAnd on Arduino:

Header header = {thisNodeId, thisNodeType, 0, static_cast<uint8_t>(MsgType::TEMP_SENSOR_DATA), 12345, Status::ok};
 
TempSensorData dhtData;
dhtData.result = DHT.read11(DHT11_PIN);
dhtData.humidity = (int)DHT.humidity;
dhtData.temperature = (int)DHT.temperature;

 
Message message = {0};
message.header = header;
message.msgData.tempSensorData = (dhtData);
 
radio.stopListening();
radio.write(&message, sizeof(message));
radio.startListening();

I also use a common header with defined structures for both Arduino and Raspberry, which contains:

#define RASPI_WRITE_ADDR 0xF0F0F0F0F0LL
#define RASPI_READ_ADDR 0xF0F0F0F0E1LL

struct TempSensorData
  {   
    uint32_t result;
    uint32_t temperature;
    uint32_t humidity;
};  

enum class Status : uint8_t
{   
    ok,
    error,
    fail
};

{
ok, error, fail };

enum class MsgType : uint8_t
  {
    INITIALIZATION,
    RESET_REQUEST,
    ACK_NACK,
    TEMP_SENSOR_DATA,
};

struct Header
  {
    uint8_t nodeId;
    uint8_t nodeType;
    uint8_t location;
    uint8_t msgType;
    uint16_t checksum;
    Status status;
};

union MsgData
  {
    InitMsgData initMsgData;
    AckNack ackNack;
    TempSensorData tempSensorData;
};

struct Message
  {
    Header header;
    MsgData msgData;
};

radioradio is an item of RF24 class from https://github.com/TMRh20/RF24/

Unfortunately the RF24 repo is 64 commits ahead of what I use..

sending struct from Arduino to Raspberry Pi - wrong types

I'm using Raspberry Pi and Arduinos for my home automation project where Raspi is controler of Arduino nodes. I use nRF24 wireless transceiver to communicate these two.

struct TempSensorData
{
    uint32_t result;
    uint32_t temperature;
    uint32_t humidity;
};

from Raspberry to Raspberry everything went fine but now when I use Arduino as sender I get very strange results:

while on Raspberry it was

Can this be a problem with types?

Or with different type of architecture (like different sizes on 32bit and 64bit arch)?

code on Raspberry:

if (radio.available())
{
    // dump the payloads until we've got everything
    Message receivedData = {0};
    radio.read(&receivedData, sizeof(Message));
    TempSensorData data = receivedData.msgData.tempSensorData;
    std::cout << "received: status: " << data.result << ", temperature: " << data.temperature << " degrees, humidity: " << data.humidity << "%" << std::endl; //TODO here we have some strange numbers - check if we have proper types
}

before that I have:

radio.begin();
 
radio.setPALevel(RF24_PA_LOW);
radio.setChannel(0x4c);
 
radio.openReadingPipe(1, RASPI_READ_ADDR);
radio.openWritingPipe(RASPI_WRITE_ADDR);

radio.enableDynamicPayloads();
radio.setAutoAck(true);
radio.powerUp();
radio.startListening();

and on Arduino

Header header = {thisNodeId, thisNodeType, 0, static_cast<uint8_t>(MsgType::TEMP_SENSOR_DATA), 12345, Status::ok};
 
TempSensorData dhtData;
dhtData.result = DHT.read11(DHT11_PIN);
dhtData.humidity = (int)DHT.humidity;
dhtData.temperature = (int)DHT.temperature;

 
Message message = {0};
message.header = header;
message.msgData.tempSensorData = (dhtData);
 
radio.stopListening();
radio.write(&message, sizeof(message));
radio.startListening();

I also use common header with defined structures for both Arduino and Raspberry which contains:

#define RASPI_WRITE_ADDR 0xF0F0F0F0F0LL
#define RASPI_READ_ADDR 0xF0F0F0F0E1LL

struct TempSensorData
 {   
    uint32_t result;
    uint32_t temperature;
    uint32_t humidity;
};  

enum class Status : uint8_t
{   
    ok,
    error,
    fail
};

enum class MsgType : uint8_t
 {
    INITIALIZATION,
    RESET_REQUEST,
    ACK_NACK,
    TEMP_SENSOR_DATA,
};

struct Header
 {
    uint8_t nodeId;
    uint8_t nodeType;
    uint8_t location;
    uint8_t msgType;
    uint16_t checksum;
    Status status;
};

union MsgData
 {
    InitMsgData initMsgData;
    AckNack ackNack;
    TempSensorData tempSensorData;
};

struct Message
 {
    Header header;
    MsgData msgData;
};

radio is an item of RF24 class from https://github.com/TMRh20/RF24/

Unfortunately RF24 repo is 64 commits ahead of what I use..

Sending struct from Arduino to Raspberry Pi - wrong types

I'm using Raspberry Pi and Arduinos for my home automation project where Raspberry Pi is the controler of Arduino nodes. I use nRF24 wireless transceivers to communicate.

struct TempSensorData

{ uint32_t result; uint32_t temperature; uint32_t humidity; };

From Raspberry to Raspberry everything went fine but now when I use Arduino as sender I get very strange results:

While on Raspberry it was

Can this be a problem with types? Or with different type of architecture (like different sizes on 3-2bit and 64-bit arch)?

Code on Raspberry:

if (radio.available())

{ // dump the payloads until we've got everything Message receivedData = {0}; radio.read(&receivedData, sizeof(Message)); TempSensorData data = receivedData.msgData.tempSensorData; std::cout << "received: status: " << data.result << ", temperature: " << data.temperature << " degrees, humidity: " << data.humidity << "%" << std::endl; //TODO here we have some strange numbers - check if we have proper types }

Before that I have:

radio.begin();
radio.setPALevel(RF24_PA_LOW);
radio.setChannel(0x4c);
radio.openReadingPipe(1, RASPI_READ_ADDR);
radio.openWritingPipe(RASPI_WRITE_ADDR);

radio.enableDynamicPayloads();
radio.setAutoAck(true);
radio.powerUp();
radio.startListening();

And on Arduino:

Header header = {thisNodeId, thisNodeType, 0, static_cast<uint8_t>(MsgType::TEMP_SENSOR_DATA), 12345, Status::ok};
TempSensorData dhtData;
dhtData.result = DHT.read11(DHT11_PIN);
dhtData.humidity = (int)DHT.humidity;
dhtData.temperature = (int)DHT.temperature;
Message message = {0};
message.header = header;
message.msgData.tempSensorData = (dhtData);
radio.stopListening();
radio.write(&message, sizeof(message));
radio.startListening();

I also use a common header with defined structures for both Arduino and Raspberry, which contains:

#define RASPI_WRITE_ADDR 0xF0F0F0F0F0LL
#define RASPI_READ_ADDR 0xF0F0F0F0E1LL

struct TempSensorData {   
  uint32_t result;
  uint32_t temperature;
  uint32_t humidity;
};  

enum class Status : uint8_t

{
ok, error, fail };

enum class MsgType : uint8_t {
    INITIALIZATION,
    RESET_REQUEST,
    ACK_NACK,
    TEMP_SENSOR_DATA,
};

struct Header {
  uint8_t nodeId;
  uint8_t nodeType;
  uint8_t location;
  uint8_t msgType;
  uint16_t checksum;
  Status status;
};

union MsgData {
  InitMsgData initMsgData;
  AckNack ackNack;
  TempSensorData tempSensorData;
};

struct Message {
    Header header;
    MsgData msgData;
};

radio is an item of RF24 class from https://github.com/TMRh20/RF24/

Unfortunately the RF24 repo is 64 commits ahead of what I use.

Rollback to Revision 3
Link
Chris Stratton
  • 5.4k
  • 19
  • 40
added compiler
Source Link
Loading
added code examples
Source Link
Loading
Source Link
Loading