sending and receiving Int array 433MHz wireless

Trying to now understand MHz433 tx & rx ???

I feel my problem is that I don’t understand what is contained in the “buf” and how the information

held in “buf” is converted at the Rx end.

What I am trying to achieve is to send 5 integers from TX and be able to convert the RX “buf” back to those same integers.

Searched the forum and found a reply from “cattledog” which I thought would fit the bill

but alas I cannot get it to work.

Perhaps some kind soul would help me out with what I have done wrong in the following sketches.

............................................TRANSMITTER................................................

// transmitter.pde

#include <VirtualWire.h>

 int i;               // used as an index into the array
 int my_array[5];     // an array with 5 integer elements#include <VirtualWire.h>


void setup()
{
   vw_set_tx_pin (12);
   vw_setup(2000);	// Bits per sec. Lower BPS to increase range
   
   Serial.begin(9600);
}

void loop()
{
  my_array[0] = 18;    
  my_array[1] = 1;  
  my_array[2] = 240;
  my_array[3] = 46;
  my_array[4] = 2;

 
  
  for (i = 0; i < 5; i++) 
  
  Serial.println(my_array[i]);
  
  vw_send((uint8_t *)my_array, sizeof(my_array));

  digitalWrite(13, true); // Flash a light to show transmitting
   
  vw_wait_tx();                        
  delay(1000);

..................................RECEIVER.......................................


}
// receiver.pde


#include <VirtualWire.h>
 

int solenoid    = 0;
int onOff       = 0;
int time_length = 0;
int pressure    = 0;
char my_array    [5];


void setup()
{
  
  
    Serial.begin(9600);	
    Serial.println("setup");
    

   
    vw_setup(2000);	 // Bits per sec

    vw_rx_start();       // Start the receiver PLL running
   
   
}

void loop()
{

  
    uint8_t buf[VW_MAX_MESSAGE_LEN];
    uint8_t buflen = VW_MAX_MESSAGE_LEN;
    
    

   if (vw_get_message(buf, &buflen))

   // Turn on a light to show received good message
   digitalWrite(13, true);
   

   memcpy(my_array,buf,buflen); 
  



 
   int i;
   for (i = 0; i < buflen; i++)
   
  
   solenoid       =  my_array[0]; 
   onOff          =  my_array[1]; 
   time_length    =  my_array[2]; 
   pressure       =  my_array[3]; 

  
    

   
   Serial.println (solenoid);
   Serial.println (onOff);
   Serial.println (time_length);
   Serial.println (pressure);

   
   delay (1000);
   Serial.println("");
   digitalWrite(13, false);
    
}

Thanks in advance

Please explain what ‘does not work’ means. Does a simple VirtualWire example work?

Why do you have a for-loop in the receiver code? That for-loop just copies my_array[0] buflen times to the variable solenoid.

If you use tools->autoformat in the IDE, you will see

  int i;
  for (i = 0; i < buflen; i++)


    solenoid       =  my_array[0];
  onOff          =  my_array[1];
  time_length    =  my_array[2];
  pressure       =  my_array[3];

The indentation indicates that the for-loop only applies to the ‘solenoid’ line. This however is not the cause of whatever your problem is.

Your receiver code has some issues.

  1. You send 5 integer values (10 bytes)int my_array [5]
    but have only declared the receiving array as 5 bytes. You will overwrite the allocated memory.
char my_array[5]
  1. There is no bracketed block of code after the conditional statement. if (vw_get_message(buf, &buflen)
    Only the next line is executed conditionally, and the others are executed unconditionally whether or not there was a message received. Create a conditional block of code.

  2. If you are using the indicator LED, you need to set it as anoutput.

pinMode(13,OUTPUT);
// receiver.pde

#include <VirtualWire.h>

int solenoid    = 0;
int onOff       = 0;
int time_length = 0;
int pressure    = 0;
//char my_array[5];
int my_array[5];

void setup()
{

  Serial.begin(9600);
  Serial.println("setup");

  vw_setup(2000);  // Bits per sec
  vw_rx_start();       // Start the receiver PLL running

  pinMode(13,OUTPUT);
}

void loop()
{
  uint8_t buf[VW_MAX_MESSAGE_LEN];
  uint8_t buflen = VW_MAX_MESSAGE_LEN;

  if (vw_get_message(buf, &buflen))
  {//start conditional block
    // Turn on a light to show received good message
    digitalWrite(13, true);

    memcpy(my_array, buf, buflen);

    // int i;
    // for (i = 0; i < buflen; i++)

    solenoid       =  my_array[0];
    onOff          =  my_array[1];
    time_length    =  my_array[2];
    pressure       =  my_array[3];

    Serial.println (solenoid);
    Serial.println (onOff);
    Serial.println (time_length);
    Serial.println (pressure);

  }//end conditional block

  delay (1000);
  Serial.println("");
  digitalWrite(13, false);

}

Changed the Rx sketch as follows

// receiver.pde


#include <VirtualWire.h>
 

int solenoid    = 0;
int onOff       = 0;
int time_length = 0;
int pressure    = 0;
//char my_array    [5];//WRONG. Need an Integer array
int my_array [5];

void setup()
{
 
 
    Serial.begin(9600);  
    Serial.println("setup");

    pinMode(13,OUTPUT);

   

   
    vw_setup(1000); // Bits per sec

    vw_rx_start();       // Start the receiver PLL running
   
   
}

void loop()
{
  
      
 
    uint8_t buf[VW_MAX_MESSAGE_LEN];
    uint8_t buflen = VW_MAX_MESSAGE_LEN;
    
   
   

   if (vw_get_message(buf, &buflen))

   // Turn on a light to show received good message
   digitalWrite(13, true);
   

   memcpy(my_array,buf,buflen);
   
 



 
   //int i;                           //NOT NECESSARY
   //for (i = 0; i < buflen; i++)
   
 
   solenoid       =  my_array[0];
   onOff          =  my_array[1];
   time_length    =  my_array[2];
   pressure       =  my_array[3];

 
   

  
   Serial.println (solenoid);
   Serial.println (onOff);
   Serial.println (time_length);
   Serial.println (pressure);

   
   delay (1000);
   Serial.println("");
   digitalWrite(13, false);
   
}

Output at the Rx is

" the letters “SE” followed by 4 spaces then a “SQUARE” and then repeated over and over."

Have not changed the Transmitter sketch.

Have dropped the transfer rate.Makes no difference.

Have noticed there seems to be no declaration of a “vw_set_rx_pin (12);”. Is that necessary.

Changed the Receiver UNO. No difference.

Any help appreciated.

You haven't fixed Issue #2 as pointed out by @cattledog.

//…RECEIVER…

// receiver.pde

#include <VirtualWire.h>

int solenoid = 0;
int onOff = 0;
int time_length = 0;
int pressure = 0;

int my_array [5]; // NOT SURE if should be array 4 or bytes 10

void setup()
{

Serial.begin(9600);
Serial.println(“setup”);

pinMode(13, OUTPUT);

vw_setup(1000); // Bits per sec

vw_rx_start(); // Start the receiver PLL running

}

void loop()
{

Serial.println(“xxxxxxxxx”);

uint8_t buf[VW_MAX_MESSAGE_LEN];
uint8_t buflen = VW_MAX_MESSAGE_LEN;

if (vw_get_message(buf, &buflen))

Serial.println (“yyyyyyyyyy”);
//start of condition
{

// Turn on a light to show received good message
digitalWrite(13, true);

memcpy(my_array, buf, buflen);

Serial.println (“zzzzzzzzzzzz”);

solenoid = (my_array[0]);
onOff = (my_array[1]);
time_length = (my_array[2]);
//pressure = (my_array[3]);
pressure = (my_array);

Serial.println (solenoid);
Serial.println (onOff);
Serial.println (time_length);
Serial.println (pressure);

delay (5000);
Serial.println("");
digitalWrite(13, false);
// end of condition
}

}

Thanks think I now understand about if conditional brackets BUT

I have included Serial.prints in the sketch to try to see how far through the sketch

things progress.

With the " memcpy(my_array, buf, buflen);" in the sketch it seems to go nowhere

just outputs a continuous stream of “SQUARES”’

With it commented out (//) sketch proceed right through and prints out

the Serial.print line “zzzzzzzzzzz” followed by each of the 3 variables with a

=0 value and a my_array value of 463.

Also not sure if I should declare the " my_array" to be [5] for the number of elements

or [10] for the number of Bytes received

Are you wearing your glasses?

In reply #2 I provided complete receiver code with the errors I saw corrected. Did you try it?

Yep. Glasses or no glasses it did nothing.

Sorry I should have remarked on that.

With that sketch it only output "setup" to serial output then nothing else.

With that sketch it only output “setup” to serial output then nothing else.

Was the transmitter sketch running? The posted sketch in #2 was tested with a direct connection to your last posted transmitter sketch.

Run the direct connection test. If you don’t see results, post both sketches, and explain which two pins were connected. Make sure both arduinos are on the same usb connection so that the grounds are connected.

Transmitter sketch:

//............................................TRANSMITTER................................................

// transmitter.pde

#include <VirtualWire.h>

int i;               // used as an index into the array
int my_array[5];     // an array with 5 integer elements#include


void setup()
{
  pinMode(13,OUTPUT);
  vw_set_tx_pin (12);
  vw_setup(1000);  // Bits per sec. Lower BPS to increase range

  Serial.begin(9600);
}

void loop()
{
  my_array[0] = 18;
  my_array[1] = 1;
  my_array[2] = 240;
  my_array[3] = 46;
  my_array[4] = 2;

  vw_send((uint8_t *)my_array, sizeof(my_array));

  for (i = 0; i < 5; i++)
  
  Serial.println(my_array[i]);

  digitalWrite(13, true); // Flash a light to show transmitting

  vw_wait_tx();
  delay(3000);
}

Receiver sketch:

// receiver.pde

#include <VirtualWire.h>

int solenoid    = 0;
int onOff       = 0;
int time_length = 0;
int pressure    = 0;
//char my_array[5];
int my_array[5];

void setup()
{

  Serial.begin(9600);
  Serial.println("setup");

  vw_setup(2000);  // Bits per sec
  vw_rx_start();       // Start the receiver PLL running

  pinMode(13,OUTPUT);
}

void loop()
{
  uint8_t buf[VW_MAX_MESSAGE_LEN];
  uint8_t buflen = VW_MAX_MESSAGE_LEN;

  if (vw_get_message(buf, &buflen))
  {//start conditional block
    // Turn on a light to show received good message
    digitalWrite(13, true);

    memcpy(my_array, buf, buflen);

    // int i;
    // for (i = 0; i < buflen; i++)

    solenoid       =  my_array[0];
    onOff          =  my_array[1];
    time_length    =  my_array[2];
    pressure       =  my_array[3];

    Serial.println (solenoid);
    Serial.println (onOff);
    Serial.println (time_length);
    Serial.println (pressure);

  }//end conditional block

  delay (1000);
  Serial.println("");
  digitalWrite(13, false);

}

Transmitter Board

uno pin 5v to Tx vcc centre pin

uno pin gnd to Tx gnd gnd pin

uno pin 12 to Tx data data pin

Tx data data pin wired direct to Rx data pin

uno pin 13 not connected

Onboard Rx light flashes. (Is this normal. I would have thought the Tx light would flash)

Receiver Board

uno pin 5v to Rx vcc vcc pin

uno pin gnd to Rx gnd gnd pin

uno pin 12 to Rx data centre pin

Rx data data pin wired direct to Tx data pin

Onboard Tx light flashes. (Is this normal. I would have thought the Rx light would flash)

I have used 2 new Rx and Tx modules (Cheapie $2 specials)

2 New Uno boards

Changed ports

and disconnected and reconnected all the pin wires.

vw_set_tx_pin (12);
  vw_setup(1000);  // Bits per sec. Lower BPS to increase range
vw_setup(2000);  // Bits per sec
  vw_rx_start();       // Start the receiver PLL running

The baud rates must be the same in both sketches! You have changed the TX rate from the code originally posted.

Receiver Board

uno pin 5v to Rx vcc vcc pin

uno pin gnd to Rx gnd gnd pin

uno pin 12 to Rx data centre pin

The default RX pin is 11. The default TX pin is 12.

When I connect pin 12 on the uno running the TX sketch to pin 11 on the uno running the RX sketch I can see the data on both sides in the serial monitor. You only receive 4 of the 5 transmitted values.

Thanks yet again cattledog.

I feel like such a doofus for not having seen such obvious errors.

Everything is working fine.

Still havn't got the wireless transmission working but have ordered some new Rx Tx modules

and will have another go.

Can you help me understand the Tx Rx process.

As i see if on the Tx side " vw_send((uint8_t *)my_array, sizeof(my_array));" somehow converts

each element of " my_array" into a binary format and transmits each element as single Byte (for

character).or 2 Bytes for each Integer element.Am I correct in assuming that the 2 Bytes required for

integers are necessary to cater for integers in excess of 255.

On the receiver side "(vw_get_message(buf, &buflen))" retrieves the Binary data received and stores it

in"buf". Then "memcpy(my_array, buf, buflen);" retrieves the Binary data from "buf" and transfers that

data to "my_array" as either a character or an integer.

If this is so How does "memcpy" recognise whether to use character or integer.

Thanks again for your help.