Hello, I have a code that works well, to send data to a mosquitto broker. Now I need to send the photon device ID, but I don’t know how to build the char variable for it.
By example:
this variable works ok, now i need to do something like: char id[] = System.deviceID()
in the previos format.
I was searching information about this with no sucess.
Thanks for your help.
PD/ I try to post this in general category, but webpage don’t let me do that.
Nope, that’s not how you’d do it
You want to put a String into a char array, not the other way round.
As I said that method is part of the String object but id is not one of them, it’s a character array.
But System.deviceID() returns a String object, hence you’d do it this way
Thanks for your help, sadly it doesn’t work.
As i said, i have to transform a simple string ( the photon ID) to the format in first example i posted, with \ and " ( image in first post )
to send to the mqtt broker.
char name[] = "Photon";
char mqttTemplate[]="{\"name\":\"%s\",\"site\":%i,\"deviceID\":\"%s\"}";
int site = 1;
...
char json[64];
snprintf(json, sizeof(json)
, mqttTemplate
, name
, site
, (const char*)System.deviceID()
);
I would not embed the escaped double quotes (\") in the individual fields but in the MQTT string template.
Neither the device name nor the device ID do comprise escaped double quotes so the strings holding them should not either - they are only needed for the MQTT string so they should be added there and then.