Sending int variable over virtualwire?

Hi all, Ive been looking at this post here: and was wondering how to pull this off and send values over virtualwire. However in this example they are using a float value and when I try to do this in the code that i'm using, i recieve the message "invalid conversion from 'int' to 'float*'. How do i fix this?

Here is the code that i am using:

int val;
  int val2;
  int temp;
  int humid;
  sendCommandSHT(gTempCmd, theDataPin, theClockPin);
  val = getData16SHT(theDataPin, theClockPin);
  skipCrcSHT(theDataPin, theClockPin);
  temp = -40.0 + 0.018 * (float)val;
  Serial.print(temp, DEC);
  const char *msgT = "t";       // this is your message to send
  vw_send((uint8_t *)msgT, strlen(msgT));  
  float *msg = (temp);  
  int tempC1 = (int)temp;
  int tempC2 = (int)(temp - tempC1) * 100; // For two decimal points
  char msg[24];
  sprintf(msg, "%i.%i", tempC1,tempC2);
  vw_send((uint8_t *)msg, strlen(msg)); 


Thanks for your time.

Try breaking your data into bytes, load it up into an array, and send the array.
Here is my array going out. 1 byte, but its the format thats the example.

    // go read the keypad
  char key = keypad.getKey();                 // reading the keypad
  if(key)                                     // same as if(key != NO_KEY)- did something change?
    msg[0]=key;                               // load the array with the key character
    // msg[1]=NULL;                           // Rx side seems to work without this

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

    vw_send((uint8_t *)msg, strlen(msg));     // send the character out

//    Serial.println(key);                // for debugging only

    vw_wait_tx();                             // Wait until the whole message is gone

Thanks for the reply,

I am not sure i completely understand the process of doing this, i tried this however i still had the same error message. I also tried combining it with the code, and even removing the original float *msg = (temp); but then it said that msg was not defined. This is what I did in the code originally:

const char *msgT = "t";       // this is your message to send
  vw_send((uint8_t *)msgT, strlen(msgT));  
  float *msg = (temp);
  vw_send((uint8_t *)msg, strlen(msg));
  /*float *msg = (temp);  
  int tempC1 = (int)temp;
  int tempC2 = (int)(temp - tempC1) * 100; // For two decimal points
  char msg[24];
  sprintf(msg, "%i.%i", tempC1,tempC2);
  vw_send((uint8_t *)msg, strlen(msg)); 

What would i need to change to make this work? Would i still need the part i commented out?


You need to convert the float variable into bytes and then load the array, or put the bytes of the float into the array.

Something like this, mask off off the upper bits and shift them down into the lower byte.

msg[0] = byte(float_number & 0xFF);
msg[1] = byte(float_number>>8 & 0xFF);
msg[2] = byte(float_number >> 16 & 0xFF);
msg[3] = byte(float_number >> 24 & 0xFF);

Maybe with byte(x) the 0XFF masking is not even needed.
Try it see.

Hmm, i dont exactly understand the code that much. When doing this, would i send the msg along with the [ 0 ], or just msg by itself? What is the 0xFF doing in this particular case? So far i tried this, but my main problem is defining msg. How would i do this?

Im pretty new to C++, so sorry if i take a minute...

If possible, please show the exact code
Thanks for the help

back before void setup() you're going to define how many bytes in your array:
char msg[24]; // bytes will be from 0 to 23 for example
0xFF is masking off all but the lower 8 bits, forcing the number to be from 00 00 00 00 up to 00 00 00 FF only.

And like I said, byte(x) might take care of that also.
Try a simple test, load an array that is manipulated thusly, read it back and see what you get.

Then in void loop(), you will load up the array and virtualwire will send it out as I posted, just using msg. If you only want to send part of it, then I would suggest separate arrays for the separate parts.

vw_send((uint8_t *)msg, strlen(msg)); // send the character(s) out

strlen(msg) tells it how long the array is.
maybe sizeof
would do the same. uses strlen. I had help getting the array thing working myself, seems to do the trick tho.

After working with it a little, I was able to get it to send it, and constantly work. I was recieving capital F's for "70" degrees when I tried it. I found out that I only needed to do this:

msg[0] = byte(temp);
msg[1] = '\0';

vw_send((uint8_t *)msg, strlen(msg)); 

but needed to end it with the msg[1] = '\0'; to end the array

My main question is how to decode this on the receiving end, and put into an int form?

Thanks for your time

Never mind, found out how to do it. Thanks for the help!

The code is on github for future reference if anyone would like:

ok :slight_smile: