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);
}
}
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);
}
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!
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...
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!