I have a MAC MOnterey 12.6.3 and installed all necessary espressif toolchain.

I just installed AWS-FreeRTOS in a ESP32-S3 and ran the mqtt/tls_mutual_auth example. This example is to publish a hello world in the AWS-I0T core mqtt broker.

It is working. The problem is that I want to publish a JSON like this {"certs_state": "2"}. The code to do that is the following:

char *myString=NULL;
int x = 2;
asprintf(&myString, "{\"certs_state\": \"%d\"}", x);

// outgoingPublishPackets[ publishIndex ].pubInfo.pPayload = MQTT_EXAMPLE_MESSAGE;
outgoingPublishPackets[ publishIndex ].pubInfo.pPayload = myString;

The portion outgoingPublishPackets[ publishIndex ].pubInfo.pPayload = is where you pass the string you want to publish. I replaced the variable MQTT_EXAMPLE_MESSAGE "Hello World!" which is in the line 194 in the original code by another variable myString to be used in the line 1361 in the same code.

My goal is to publish a JSON like this {"certs_state":"2"} but I am getting a JSON like this {"certs_state":"2" published in the AWS-IoT Core. The } is missing.

I am testing my code in another C Online Compiler, using the same coe I am using inside the FreeRTOS and I am getting the correct result:

// Online C compiler to run C program online
#include <stdio.h>

int main() {
    char *myString=NULL;
    int x = 2;
    asprintf(&myString, "{\"certs_state\": \"%d\"}", x);
    return 0;

//Output {"certs_state": "2"} as expected.

The conclusion is that the code is not wrong. Maybe there is something more causing the issue. Any idea what that could be?


I think I found portion of the issue, I guess. Maybe it was not working because of the quantity of characters. If I remove the " it releases enough space to create the full string {"certs_state": 3}, without ". enter image description here code is :

char *myString=NULL;
int x = 3;
asprintf(&myString, "{\"certs_state\": %d}",x);

But it is still not a JSON.

In another test if I use x = 3456 it only display until the 5: enter image description here and the code is:

char *myString=NULL;
int x = 3456;
asprintf(&myString, "{\"certs_state\": %d}",x);

Doing another test with a small JSON: enter image description here The code is:

char *myString=NULL;
int x = 3;
asprintf(&myString, "{\"t\":\"%d\"}",x);

It seems that there is no enough memory allocated to that simple string. And besides that it seems that {"t":"3"} is not a JSON.

any thoughts?

  • Have you tested that code on the ESP32? That would tell you more than testing it im “C Online Compiler”.
    – romkey
    Apr 25 at 14:42
  • Is there maybe a packet size limit somewhere which would be exceeded?
    – jcaron
    Apr 25 at 14:56
  • I added an update
    – IgorAlves
    Apr 25 at 21:22
  • Again, I am asking you to print out the value generated by asprintf() in your ESP32 program. I don’t want to know what AWS showed. I want to know what asprintf() generated immediately after you called it, from the ESP32, not an online simulator, not after you try to send it to AWS.
    – romkey
    Apr 26 at 0:12


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Browse other questions tagged or ask your own question.