VirtualWire

Hi,

I have compiled a program to receive data from a 433mhz receiver with virtualwire and print it on a led display, this is working fine but i have an issue in the data I can send to it.
What I want to display is "TEMP=" followed by my data (temp).
I can send a const char

const char *msg = "TEMP = 36.52"; // works fine shows( TEMP = 36.52 ) but the temp displayed obviously is not my data.

sprintf(buf, "TEMP-",temp); // shows ( TEMP- ) but no temp data.

sprintf(temp,buf, " Temp"); // wont let me compile saying invalid conversion from 'long unsigned int' to 'char*'.

Do I need to create a const char containing all my info and data before calling it msg or is there another way of doing this?

My code is,

#include <VirtualWire.h>
#undef int
#undef abs
#undef double
#undef float
#undef round
unsigned long temp;

void setup()
{ temp = 36.52;
vw_set_tx_pin(3);
vw_setup(2000);
}

void loop()
{
char buf[20];
//const char *msg = "TEMP = 36.52";
sprintf(temp,buf, " Temp");
//sprintf(buf, "TEMP-",temp);

delay(500);
const char *msg = buf;
vw_send((uint8_t *)msg, strlen(msg));
vw_wait_tx();
delay(500);
}

Thanks from a newbie.

sprintf(buf, "TEMP-",temp); // shows ( TEMP- ) but no temp data.

this is not a right way to write sprintf().

try this:

sprintf(buf, "Temp- %lu",temp);

Thanks that's much appreciated. Only one slight problem which I can live with if I need is the temp data does not show the two decimal places when printing. I usually get over this on the lcd by saying temp = temp/1.00; that works on the lcd but not here? I did read on the learning part here that sprintf does not accept float, I did try it but I got a load of numbers come up and not the ones I was after.
anyway thanks again for your help.

I think that the way to go is to send and receive the float variable temperature using the cast to uint8_t on the tx and memcpy of the received buffer to a float on the rx.

In both the tx and rx sketches you will declare a float temperature.

On the tx you read the sensor into that variable, and then send it with the following

vw_send((uint8_t *) & temperature, sizeof(temperature));

The cast to uint8_t take care of breaking the data into bytes for transmission. The * and & are a pointer to the address of the variable. sizeof(temperature) will be the four bytes of a float.

On the rx you have the data in a received buffer and a buffer length. You should have some code which looks like this

uint8_t buf[VW_MAX_MESSAGE_LEN];
    uint8_t buflen = VW_MAX_MESSAGE_LEN;

    if (vw_get_message(buf, &buflen))

You can then use memcpy to transfer the buffer to the address of temperature and it will be recast to the float

  if (vw_get_message(buf, &buflen))
  { 
    memcpy (& temperature, buf, buflen);
   }

The lcd will be able to print the float variable temperature with 2 decimal places with this

lcd.print(temperature,2)

This method of using uint8_t* on the tx and memcpy on the rx can be adapted for use with multiple variables in arrays, or different types of variables in a structure.