How to Send Struct via Virtual Wire.

Guys

I have been searching far and wide but can find an example of how to use a struct properly or more to the point to send Struct via Virtual wire.

the TX side compiles ( dont know if it is correct) but the Rx side report problems, this sketch is a sketch i used to send a string over but need to upgrade to a struct now because. the real code base is far to long to post ,but if i can get this example woking i can work of it in the other code.

I dont think i am packing and unpacking the Struct properly on both side.

any help will be great

My Tx code

#include <VirtualWire.h>


int Sensor1Pin = A1;// The pins were sensor are attached
int Sensor2Pin = A2;
int Sensor3Pin = A3;
int Sensor4Pin = A4;
int ledPin = 13;

typedef struct roverRemoteData 
{
int    TX_ID; 
int    Sensor1Data;// The variable were the data from each sensor
int    Sensor2Data;// will be stored
int    Sensor3Data;
int    Sensor4Data;
int    Sensor5Data;// will be stored
int    Sensor6Data;
};



void setup() {
    Serial.begin(9600);
 // LED
    pinMode(ledPin,OUTPUT);
  
 // Sensor(s)
    pinMode(Sensor1Pin,INPUT);
    pinMode(Sensor2Pin,INPUT);
    pinMode(Sensor3Pin,INPUT);
    pinMode(Sensor4Pin,INPUT);
  
 // VirtualWire setup
  vw_setup(4000); // Bits per sec
  vw_set_tx_pin(11);
 
}
 
void loop() {
   
  // Read and store Sensor Data
  struct roverRemoteData payload;
  
  payload.TX_ID = 10;
  payload.Sensor1Data =255;// analogRead(Sensor1Pin);
  payload.Sensor2Data =255;// analogRead(Sensor2Pin);
  payload.Sensor3Data =255;// analogRead(Sensor3Pin);
  payload.Sensor4Data =255;// analogRead(Sensor4Pin);
  payload.Sensor5Data =4;// analogRead(Sensor1Pin);
  payload.Sensor6Data =4;// analogRead(Sensor2Pin);
  
  
  vw_send((uint8_t *)&payload, sizeof(payload));
  vw_wait_tx();
  
 digitalWrite(ledPin,HIGH);
 delay(1);
 digitalWrite(ledPin,LOW);
 // Turn off a light after transmission
 delay(1000);
 
}

my Rx code

#include <VirtualWire.h>
  
int RX_ID;
int TX_ID =10;


typedef struct roverRemoteData
{
int    TX_ID; 
int    Sensor1Data;// The variable were the data from each sensor
int    Sensor2Data;// will be stored
int    Sensor3Data;
int    Sensor4Data;
int    Sensor5Data;// will be stored
int    Sensor6Data;
};

int Led_Tx = 12;
int Led_Rx = 13;


void setup() {
              Serial.begin(9600);
              pinMode(Led_Tx,OUTPUT);
              pinMode(Led_Rx,OUTPUT);
              vw_setup(4000);
              vw_set_rx_pin(11); 
              vw_rx_start(); 
                   
              }  
void loop()
{
  // struct roverRemoteData receivedData;
struct roverRemoteData receivedData;
int rcvdSize = sizeof(receivedData);

if (vw_get_message((uint8_t *)&receivedData, &rcvdSize)) 
{
  if (receivedData.TX_ID == 10) 
    {
    int RX_ID = Data.TX_ID;
    int SensorData1 = Data.Sensor1Data;
    int SensorData2 = Data.Sensor2Data;
    int SensorData3 = Data.Sensor3Data;
    int SensorData4 = Data.Sensor4Data;
    int SensorData5 = Data.Sensor5Data;
    int SensorData6 = Data.Sensor6Data;
    }
        
      //if( RX_ID == TX_ID)
        { 
         Serial.println("------------------------New MSG-----------------------");
         Serial.print("TX ID = ");
         Serial.println(RX_ID);
         Serial.print("Sensor1Data:");
         Serial.println(SensorData1);
         Serial.print("Sensor2Data:");
         Serial.println(SensorData2);
         Serial.print("SensorData3:");
         Serial.println(SensorData3);
         Serial.print("SensorData4:");
         Serial.println(SensorData4);
          Serial.print("SensorData5:");
         Serial.println(SensorData5);
         Serial.print("SensorData6:");
         Serial.println(SensorData6);
         
         
         Serial.println("-----------------------End of MSG--------------------");
        } 
      else
        {
          Serial.println(" ID Does not match waiting for next transmission ");
          memset( StringReceived, 0, sizeof( StringReceived));
        }
      //Serial.println(StringReceived);
          memset( StringReceived, 0, sizeof( StringReceived));
  }
 
}
int    Sensor1Data;// The variable were the data from each sensor
int    Sensor2Data;// will be stored
int    Sensor3Data;
int    Sensor4Data;
int    Sensor5Data;// will be stored
int    Sensor6Data;

You got something against arrays?

  if (receivedData.TX_ID == 10) 
    {
    int RX_ID = Data.TX_ID;
    int SensorData1 = Data.Sensor1Data;
    int SensorData2 = Data.Sensor2Data;
    int SensorData3 = Data.Sensor3Data;
    int SensorData4 = Data.Sensor4Data;
    int SensorData5 = Data.Sensor5Data;
    int SensorData6 = Data.Sensor6Data;
    }

When you get some data, store it in some variables that immediately go out of scope. Hmmm, that doesn't seem like a great idea to me.

The receiver code won't even compile.

Hi Paul

I dont have anything against anything , i just do not understand how to use Arrays or Struct that is why the code looks that way.
please provide me with a simple example of any Array that will do the same thing and i will try and apply it my self, and report back I need to learn.

typedef struct roverRemoteData 
{
int    TX_ID; 
int    SensorData [6];
};

Hi Awol

Thank you for the code, but how must i derive from that how to int, pack and unpack the array ?

Why do you need to pack and unpack it?
You send a struct, and you receive a struct.
Just access the data straight out of the struct.

I have 6 values all int. I need to send over with , an ID and if possible a checksum at the end.
the first value is the ID , second is 0-255 and the rest is all ether HIGH or LOW.

please give me an example of you you would do that with an Array or struct , my attempt at a struct is above , and as paul pointed out it is not going to work

"not going to work" and "not going to compile" are two different things.
If you get your receiver code to compile, then you have a good (at least a much better) chance that it will work.

okay when i compile the Receiver code this is the error i get before it gets to the part i need to fix that paul was speaking about
sketch_nov13a.ino: In function 'void loop()':
sketch_nov13a:37: error: cannot convert 'int*' to 'uint8_t*' for argument '2' to 'uint8_t vw_get_message(uint8_t*, uint8_t*)'

any ideas?

sketch_nov13a:37: error: cannot convert 'int*' to 'uint8_t*' for argument '2' to 'uint8_t vw_get_message(uint8_t*, uint8_t*)'

This is telling you that the 2nd argument to the function is supposed to be an unsigned int pointer. You have an int pointer. Do you really need to be able to send or receive -27 bytes?

struct roverRemoteData receivedData;
int rcvdSize = sizeof(receivedData);

if (vw_get_message((uint8_t *)&receivedData, &rcvdSize))

sketch_nov13a.ino: In function 'void loop()':
sketch_nov13a:37: error: cannot convert 'int*' to 'uint8_t*' for argument '2' to 'uint8_t vw_get_message(uint8_t*, uint8_t*)'

The compiler is telling you exactly what is wrong, and where.

Paul

Thank you I have changed it now. this is how the code looks now, when i try and compile it reports that RX_ID was not declared , i am assuming this is becuase the RX_ID is no longer a global var. but how would i go about reading the information now .

#include <VirtualWire.h>
  
int TX_ID = 10;
int ID;
int Led_Tx = 12;
int Led_Rx = 13;


void setup() {
              Serial.begin(9600);
              pinMode(Led_Tx,OUTPUT);
              pinMode(Led_Rx,OUTPUT);
              vw_setup(4000);
              vw_set_rx_pin(11); 
              vw_rx_start(); 
                   
              }  
void loop()
{
  typedef struct roverRemoteData
{
int    RX_ID; 
int    Sensor1Data;// The variable were the data from each sensor
int    Sensor2Data;// will be stored
int    Sensor3Data;
int    Sensor4Data;
int    Sensor5Data;// will be stored
int    Sensor6Data;
};
  // struct roverRemoteData receivedData;
struct roverRemoteData receivedData;
uint8_t rcvdSize = sizeof(receivedData);

if (vw_get_message((uint8_t *)&receivedData, &rcvdSize)) 
{
  
  if (RX_ID == 10) 
    {
     //DO THE REST  Serial.println(TX_ID);
    }
        } 
      else
        {
          Serial.println(" ID Does not match waiting for next transmission ");
        //  memset( receivedData, 0, sizeof( receivedData));
        }
      //Serial.println(StringReceived);
         // memset( receivedData, 0, sizeof( receivedData));
  }

should it look like this

if (receivedData.RX_ID == 10)

Why is the definition of the struct now in loop?

The only RX_ID you have defined is in the struct. If you want to reference the member of the struct, you need to do so properly.

I did it try and stop the declaration errors

if i do it like this

#include <VirtualWire.h>
  
int RX_ID;
int TX_ID =10;


typedef struct roverRemoteData
{
int    TX_ID; 
int    Sensor1Data;// The variable were the data from each sensor
int    Sensor2Data;// will be stored
int    Sensor3Data;
int    Sensor4Data;
int    Sensor5Data;// will be stored
int    Sensor6Data;
};

int Led_Tx = 12;
int Led_Rx = 13;


void setup() {
              Serial.begin(9600);
              pinMode(Led_Tx,OUTPUT);
              pinMode(Led_Rx,OUTPUT);
              vw_setup(4000);
              vw_set_rx_pin(11); 
              vw_rx_start(); 
                   
              }  
void loop()
{
  // struct roverRemoteData receivedData;
struct roverRemoteData receivedData;
uint8_t rcvdSize = sizeof(receivedData);

if (vw_get_message((uint8_t *)&receivedData, &rcvdSize)) 
{
  if (receivedData.TX_ID == 10) 
    {
      //if( RX_ID == TX_ID)
         
         Serial.println("------------------------New MSG-----------------------");
         Serial.print("TX ID = ");
         Serial.println(RX_ID);
         Serial.print("Sensor1Data:");
         Serial.println(SensorData1);
         Serial.print("Sensor2Data:");
         Serial.println(SensorData2);
         Serial.print("SensorData3:");
         Serial.println(SensorData3);
         Serial.print("SensorData4:");
         Serial.println(SensorData4);
          Serial.print("SensorData5:");
         Serial.println(SensorData5);
         Serial.print("SensorData6:");
         Serial.println(SensorData6);
         
         
         Serial.println("-----------------------End of MSG--------------------");
        } 
      else
        {
          Serial.println(" ID Does not match waiting for next transmission ");
         // memset( StringReceived, 0, sizeof( StringReceived));
        }
      //Serial.println(StringReceived);
         // memset( StringReceived, 0, sizeof( StringReceived));
  }
 
}

then it tels me all of my var has not been declared

 Serial.println(SensorData1);

Same problem.
SensorData1 is part of your structure, and has to be referenced correctly.

how do I do that ?
declare it as a int outside the struct and then write it as
Serial.print(receivedData.SensorData1); ?

how do I do that ?
declare it as a int outside the struct and then write it as
Serial.print(receivedData.SensorData1); ?

No and yes.

It's the same problem as reply #11, with the same solution a reply #12, with the added twist that what you're trying to print doesn't have the same name as the corresponding structure element.

Bollocks! :~ I only see now that Sensor1Data and SensorData1 are different ( been looking at the screen to long)

so one last time and this time the code compiled. will this work ?

#include <VirtualWire.h>
  
int RX_ID;
int TX_ID =10;

typedef struct roverRemoteData
{
int    TX_ID; 
int    Sensor1Data;// The variable were the data from each sensor
int    Sensor2Data;// will be stored
int    Sensor3Data;
int    Sensor4Data;
int    Sensor5Data;// will be stored
int    Sensor6Data;
};

int Led_Tx = 12;
int Led_Rx = 13;


void setup() {
              Serial.begin(9600);
              pinMode(Led_Tx,OUTPUT);
              pinMode(Led_Rx,OUTPUT);
              vw_setup(4000);
              vw_set_rx_pin(11); 
              vw_rx_start(); 
                   
              }  
void loop()
{
  // struct roverRemoteData receivedData;
struct roverRemoteData receivedData;
uint8_t rcvdSize = sizeof(receivedData);

if (vw_get_message((uint8_t *)&receivedData, &rcvdSize)) 
{
  if (receivedData.TX_ID == 10) 
    {
      //if( RX_ID == TX_ID)
         
         Serial.println("------------------------New MSG-----------------------");
         Serial.print("TX ID = ");
         Serial.println(RX_ID);
         Serial.print("Sensor1Data:");
         Serial.println(receivedData.Sensor1Data);
         Serial.print("Sensor2Data:");
         Serial.println(receivedData.Sensor2Data);
         Serial.print("SensorData3:");
         Serial.println(receivedData.Sensor3Data);
         Serial.print("SensorData4:");
         Serial.println(receivedData.Sensor4Data);
          Serial.print("SensorData5:");
         Serial.println(receivedData.Sensor5Data);
         Serial.print("SensorData6:");
         Serial.println(receivedData.Sensor6Data);
         
         
         Serial.println("-----------------------End of MSG--------------------");
        } 
      else
        {
          Serial.println(" ID Does not match waiting for next transmission ");
         // memset( StringReceived, 0, sizeof( StringReceived));
        }
      //Serial.println(StringReceived);
         // memset( StringReceived, 0, sizeof( StringReceived));
  }
 
}