Help with char construct for mqtt

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:
image
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.

System.deviceID() returns a String object which features a toCharArray() method to copy the string into a character array.

1 Like

Hi thanks for answering, I forgot to mention that I had already tried with id.toCharArray(System.deviceID(), 65) and it dosen’t work.

Nope, that’s not how you’d do it :wink:
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

char id[25];
System.deviceID().toCharArray(id, sizeof(id));
3 Likes

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.

I'm pretty sure it will work when done right :wink:

Can you produce a sample string you want constructed?
Then I can cobble together the code to achieve that.

BTW, my original suggestion was aiming to create what your non-working attempt was indicating you were after.

This would result in a string only containing 24 hex digits without any kind of "decoration" like \"
And that is what my code actually does.

Ok, i have this code to send the information to mqtt, is working:

char name[]="\"Photon\"";
char mqttstring[]="{\"name\":%s,\"site\":%i}";
int site = 1;

send to mqtt with this:

sprintf(jsonString,mqttstring,name,site);
client.publish(topic,jsonString);

Not sure where you want your device ID in that.
But if you intended to replace the name with the device ID then this would be one way to do it

char mqttTemplate[]="{\"deviceID\":%s,\"site\":%i}";
int site = 1;
...
  char json[64];
  snprintf(json, sizeof(json), mqttTemplate, (const char*)System.deviceID(), site);

i need to add the id on the string and send it:
sprintf(jsonString,mqttstring,name,site, id);

In that case I’d do this

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.

Thanks, i am going to modify my code to your example and test,

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.