Pages: [1]   Go Down
Author Topic: VirtualWire send multiple variables at once  (Read 638 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 654
Posts: 50933
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Is there any solution for this problem?
Of course. The sprintf() function comes to mind.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 310
Posts: 26636
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

And it says elsewhere on the forum different workarounds.
Logged

"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.

Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Can you post some links/examples please?
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 654
Posts: 50933
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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.
Logged

Finland
Offline Offline
Sr. Member
****
Karma: 1
Posts: 270
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
  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.
Logged

Värmland, Sweden
Offline Offline
Sr. Member
****
Karma: 9
Posts: 262
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Pages: [1]   Go Up
Jump to: