Go Down

Topic: VirtualWire send multiple variables at once (Read 1 time) previous topic - next topic


I am using VirtualWire library (http://www.open.com.au/mikem/arduino/VirtualWire.pdf) and RF transmitter/reciver to communicate between two arduinos.

There is function in this library to send data:
vw_send((uint8_t *)msg, strlen(msg));

I need to send data like this:

string timestamp;
float voltage;
float current;
float power;

DataToSend = timestamp + "," + voltage + "," + current + "," + power;
for example: "2012.10.13, 4.59, 0.36, 1.65";

Is there any solution for this problem?


Is there any solution for this problem?

Of course. The sprintf() function comes to mind.


It was said somewher on the forum, that sprintf() does not accept floats.


And it says elsewhere on the forum different workarounds.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.


Can you post some links/examples please?


Can you post some links/examples please?

The usual methods of printing floats involve either printing the whole number and the fractional portion separately, or using dtostrf() to convert the float to a (unformatted) string.


It would be more efficent to send the data in binary form:

Code: [Select]

  const char * timestamp = "2012.10.13";
  float voltage = 4.59;
  float current = 0.36;
  float power = 1.65;

  int size_of_float = sizeof(float);
  int size_of_timestamp = strlen(timestamp);

  uint8_t buffer[size_of_timestamp+size_of_float*3];
  void *p = buffer;
  memcpy(p, (void *)timestamp, size_of_timestamp);
  p += size_of_timestamp;
  memcpy(p, (void *)&voltage, size_of_float);
  p += size_of_float;
  memcpy(p, (void *)&current, size_of_float);
  p += size_of_float;
  memcpy(p, (void *)&power, size_of_float);

  vw_send(buffer, sizeof(buffer));

And you could also send the timestamp as a 32bit integer instead of a string.


It's worth taking into account that VirtualWire has a send limit of 27 bytes per transmission so it's possible that you will have to split your data.

Go Up

Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

via Egeo 16
Torino, 10131