Go Down

Topic: VirtualWire send multiple variables at once (Read 658 times) 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?

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

Quick Reply

With Quick-Reply you can write a post when viewing a topic without loading a new page. You can still use bulletin board code and smileys as you would in a normal post.

Warning: this topic has not been posted in for at least 120 days.
Unless you're sure you want to reply, please consider starting a new topic.

Note: this post will not display until it's been approved by a moderator.

shortcuts: alt+s submit/post or alt+p preview