Sending sensor values w/ VirtualWire

Hi all!
I'm working on a wireless sensor comprised of a light dependent resistor and temp sensor using a 433MHZ RF link and virtualwire to send the resulting values.
Got everything connected up but I cannot seem to send an int value using virtualwire.
I'm using the itoa function as I think vw requires the message to be a string.
I seem to be receiving a range of numbers between 0 and 6.
I've included the code below (no temp sensor), and if anyone could point me on the right path I'd be very grateful!

Thanks!

TX Code

#include <VirtualWire.h>

int lightVal;
char msgArray[4];


void setup()
{
  vw_set_tx_pin(3);  
  vw_set_ptt_inverted(true);
  vw_setup(2000);
}

void loop()
{
  lightVal = analogRead(0);      //ldr

  itoa(lightVal, msgArray, 10);  //int to ascii
  char *msg = msgArray;          //can only send chars with VW?
  
  digitalWrite(13, true);
  vw_send((uint8_t *)msg, strlen(msg));   //send the value
  vw_wait_tx();
  digitalWrite(13, false);
 
}

RX Code

#include <VirtualWire.h>

int i;

void setup()
{
    Serial.begin(9600);	
    Serial.println("Serial started");
    vw_set_ptt_inverted(true); 
    vw_set_rx_pin(2);
    vw_setup(2000);	
    vw_rx_start();       // Start the receiver
}

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

    if (vw_get_message(buf, &buflen)) // Non-blocking
    {
     digitalWrite(13, true); 
	for (i = 0; i < buflen; i++)
	{
	  Serial.println(buf[i]);
	}
     digitalWrite(13, false);
    }
}

You can send ints directly using vw_send, by casting the int, as you do with the message, with a length of 2 (2 btyes in an int).

You can also use Serial.print to confirm what you are sending and receiving, to see if the problem is with the communication or the radios.

@PaulS - Could you explain what you mean by casting the int?
I tried

int *msg = msgArray;

It generated a compiler error "cannot convert char to int"

Not too sure what I'm doing here!
Thanks for the quick reply!

Could you explain what you mean by casting the int?

  vw_send((uint8_t *)lightVal, 2);   //send the value

Hi PaulS,

I'm still getting garbage on the RX side. I've posted the code and output again, would you mind taking a quick look at it? I'm determined to get my head around virtualwire - with a little help!
I've commented out the itoa and array elements - is this correct?

TX Code

#include <VirtualWire.h>

int lightVal;
//char msgArray[4];


void setup()
{
  vw_set_tx_pin(3);  
  vw_set_ptt_inverted(true);
  vw_setup(2000);
  Serial.begin(9600);
  Serial.println("Serial started TX Side");
}

void loop()
{
  lightVal = analogRead(0);        //ldr

//  itoa(lightVal, msgArray, 10);  //int to ascii
//  char *msg = msgArray;          //can only send chars with VW?
  
  digitalWrite(13, true);
  Serial.println(lightVal);        //debugging
  vw_send((uint8_t *)lightVal, 2);   //send the value
  vw_wait_tx();
  digitalWrite(13, false);
 
}

TX Output (Serial)

=~=~=~=~=~=~=~=~=~=~=~= PuTTY log 2011.07.19 22:03:50 =~=~=~=~=~=~=~=~=~=~=~=
Serial started TX Side
108
111
110
107
111
111
109
109
111
111
108
109
111
111
110
106
67
29
15
15
24
33
23
23
23
24
25
22
22
21
25
22
21
22
22
22
27
59
118
118
119
119
119

RX code is the same as above post.
Here is the RX output...

=~=~=~=~=~=~=~=~=~=~=~= PuTTY log 2011.07.19 22:03:11 =~=~=~=~=~=~=~=~=~=~=~=
þSerial started RX Side
 
 
#
#
$
$
 
 
b
b
$
 
$
 
$
$
 
 
$
 
$
$
 
 
$
$
 
 
b
 
 
 
$
 
b
 
$
$
$
 
$
 
b
b
 
 
$
$
 
 
$
 
 
 
$
 
$
 
 
 
$
$
$
 
$
 
$
$
$
 
$
 
$
$
 
 
$
 
 
 
$
$
 
 
$
 
$
 
b
b
$
 
$
 
$
$
b
 
$
 
$
 
 

 

 

•

RX code is the same as above post.

If you change how you are sending data, doesn't it make sense that you need to change how you are receiving it?

vw_send((uint8_t *)&lightVal, 2);   //send the value

surely?

surely?

Uh, yeah. I knew I was leaving something out. Like actually trying it.

Right, I finally have it working!
@PaulS - Yes it was a dumb not to change my rx code. Thank you so much for you help, I was going in entirely the wrong direction!

if (vw_get_message(buf, &buflen)) // Non-blocking
    {
     digitalWrite(13, true); 
	Serial.println(buf[0],DEC);
     digitalWrite(13, false);
    }

@AWOL Does this create a pointer?

vw_send((uint8_t *)

and then create a reference pointing to the value contained in lightVal?

&lightVal

Just trying to get my head around exactly whats going on with that line of code.

Many many thanks to you both!

A pointer points to an address. The & (address of) operator gets the address of a variable. This is then treated as a pointer. Since all pointers are the same size, a pointer of one type can be cast to a pointer of another type (as long as due care is taken). So, the pointer to int is cast to (treated as) a pointer to unsigned int (8 bit) (also known as byte) data, which is all that the vw_send function knows how to deal with.

It would be nice to see the complexities hidden in the virtual wire library, but for now, you need to make your data fit the methods provided.

Thank you very much, I've got my head around this! I had no idea the solution was this simple! I spent ages trying to convert to char and back again...

I had no idea the solution was this simple!

Pointers are probably both the greatest strength and the greatest weakness of the C language.
As PaulS accidentally wrote it first time, it would compile (yes, you can cast an integer to a pointer) and stuff would be transmitted - it just wouldn't be what you expected!

TBH I was just poking around in the dark! I tried all manner of things, unaware that the solution was right in front of me. That line of code makes a whole lot more sense now!