system
October 12, 2012, 11:48pm
1
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?
system
October 12, 2012, 11:52pm
2
Is there any solution for this problem?
Of course. The sprintf() function comes to mind.
system
October 13, 2012, 6:13pm
3
It was said somewher on the forum, that sprintf() does not accept floats.
system
October 13, 2012, 6:20pm
4
And it says elsewhere on the forum different workarounds.
system
October 14, 2012, 6:20am
5
Can you post some links/examples please?
system
October 14, 2012, 11:59am
6
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.
system
October 14, 2012, 8:01pm
7
It would be more efficent to send the data in binary form:
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 *)¤t, 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.
perhof
October 14, 2012, 9:01pm
8
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.