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);
printf(myString);
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?
UPDATE
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 "
.
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
:
and the code is:
char *myString=NULL;
int x = 3456;
asprintf(&myString, "{\"certs_state\": %d}",x);
Doing another test with a small JSON: 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?