How to send numbers using WIRE.H


I have an RF 433Mhz and I can send strings with it with no problems. But I need to send integers.

I have this code:

char mystring[] = "message";
vw_send((uint8_t *)mystring,sizeof(mystring)-1);

How can I send an int number?

A good way to send and receive something other than character strings is using a cast to uint8_t on the tx and memcpy of the received buffer on the rx.

In both the tx and rx sketches you will declare your array of integers so that the data structures look the same in both sketches.

On the tx you then send it with the following

int data[] = {1234,2345,3456,4567,5678};
vw_send((uint8_t *) & data, sizeof(data));

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(data) will be the number of bytes.

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

int data[5] = {0};
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 your variable and it will be recast to //the integer.

 if (vw_get_message(buf, &buflen))
   memcpy (& data, buf, buflen);

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.

ints are made up of 2 bytes so you could send the higher byte and the lower byte separately and put them back together at the receiving end. You can separate the high and low bytes at the transmitter end using the highByte() and lowByte() functions and reconstruct the int on the receiving end using (256 * highByte + lowByte

You could, of course, convert the into to a string (using the itoa() function), send it as a string and turn it back into an int at the other end (using the atoi() function)