appending variables in a string separated by commas

Hi,

I have 3 variables declared as characters: id, temp, humidity.

How could I append them and write them as a string separated by commas.

For example: id:12,temp:23,humidity:50

This is my code so far.

Kind regards

char id[2];
char humidity;
char temp[2];
string example;

void setup()
{
//setup stuff
 }

void loop(void)
{
// Receive message

e = sx1272.receivePacketTimeout(10000);
e = sx1272.getRSSIpacket();

Serial.println(e, DEC);

id[0] = sx1272.packet_received.data[0]; 
id[1] = sx1272.packet_received.data[1];  
humidity = sx1272.packet_received.data[4]; 
temp[0] = sx1272.packet_received.data[9]; 
temp[1] = sx1272.packet_received.data[10]; 


example = String.format("id:%c,crc:%c,humidity:%c)", id, temp, humidity);

}

Why do you want to do this? Do you just want to output it to the serial monitor or store in to an SD card or something?

If outputting to a serial monitor, just do Serial.print();

Otherwise use char*;

Hi,

I would like to format it like this to write it to a file. It needs to be in this format to be picked up by another process and transmitted.

Is this valid arduino syntax?

Thanks

string example; 

example = String.format("id:%c,temp:%c,humidity:%c)", id, temp, humidity);

Please don't use the stupid string class. (Like said before ;) )

But think outside the box ;) You don't need to first format it an then write/transmit it. You can format while transmitting/writing.

Serial.print(F("id:"));
Serial.print(id);
Serial,print(F(",temp:")
Serial.print(temp);
Serial,print(F(",humidity:"));
Serial.print(humidity);
Serial.println();

You can do the same for storing it to SD.

Use sprintf.

Hi guix,

Would you mind posting a simple example that I could build on?

I'm very new to c++.

Thanks

int id = 10;
int temp = 53;
int humidity = 45;

char buffer[64];
sprintf( buffer, "ID: %d, TEMP: %d, HUMIDITY: %d\n", id, temp, humidity );

But why wast 64 bytes for an extra buffer???

Just write the the value, then the glue, then the next value etc and you are done!

Oh I'm with you, but he chose to ignore those suggestions and asked for an example using sprintf. I provided ;)

It's not a really a waste if the variable is local, it will be destroyed when out of scope.

You are right that it's more efficient the way you did, but his id and temp variables aren't C strings, they are char arrays that are not null terminated, so I don't know how your method will behave with that... Also I'm not sure he is doing right with the rest of his code.

conor1, can you show us exactly the content of sx1272.packet_received.data ?

Hi guix,

Here is the output of the serial console. The content of the packet is currently '12345678901234'.

Starting 'getPacket'
## Packet correctly received in LoRa mode ##
## Packet received:
Destination: 8
Source: 3
Packet number: 123
Packet length: 19
Data: 12345678901234
Retry number: 0

My code is below. I'm essentially assigning parts of the data packet to the 3 different variables.

After this what I want to do is combine all the information into a single string of this format id:12,temp:01,humidity:5

I'm a bit confused now as to the best way to do it. My fault for not explaining properly. Should I still use sprinf()

Thanks.

char id[2];
char humidity;
char temp[2];


void setup()
{
//setup stuff
 }

void loop(void)
{
// Receive message

e = sx1272.receivePacketTimeout(10000);
e = sx1272.getRSSIpacket();

Serial.println(e, DEC);

id[0] = sx1272.packet_received.data[0]; 
id[1] = sx1272.packet_received.data[1];  
humidity = sx1272.packet_received.data[4]; 
temp[0] = sx1272.packet_received.data[9]; 
temp[1] = sx1272.packet_received.data[10]; 

Serial.print("ID:");
Serial.println(id);
Serial.print("Humidity:");
Serial.println(humidity);
Serial.print("Temp:");
Serial.println(temp);

}

Then yes I would use sprintf(), like this:

char buffer[25];
sprintf( buffer, "id:%c%c,temp:%c%c,humidity:%c", id[0], id[1], temp[0], temp[1], humidity );

OR:

char id[3];
char humidity;
char temp[3];

...

id[0] = sx1272.packet_received.data[0]; 
id[1] = sx1272.packet_received.data[1];  
id[2] = '\0';

humidity = sx1272.packet_received.data[4]; 

temp[0] = sx1272.packet_received.data[9]; 
temp[1] = sx1272.packet_received.data[10];
temp[2] = '\0';

...

char buffer[25];
sprintf( buffer, "id:%s,temp:%s,humidity:%c", id, temp, humidity );

Thank you Guix :)