-1

I am trying to send this JSON packet to AWS IoT, but it is not recognized by AWS. I am using the example ESP32 AWS FreeRTOS code, but cannot understand what would be the correct format for the JSON packet with the following code:

#define echoMAX_DATA_LENGTH 20

char cDataBuffer[ echoMAX_DATA_LENGTH ];

(void) snprintf(cDataBuffer, echoMAX_DATA_LENGTH, "{\"state\":{\"reported\":%.*d}, \"clientToken\":\"%d\"}", x, x, x);

/* Setup the publish parameters. */
memset( &( xPublishParameters ), 0x00, sizeof( xPublishParameters ) );
xPublishParameters.pucTopic = echoTOPIC_NAME;
xPublishParameters.pvData = cDataBuffer;
xPublishParameters.usTopicLength = ( uint16_t ) strlen( ( const char * ) echoTOPIC_NAME );
xPublishParameters.ulDataLength = ( uint32_t ) strlen( cDataBuffer );
xPublishParameters.xQoS = eMQTTQoS1;

The AWS test page, cannot display the message and has converted it to UTF-8 (this error message is below)

enter image description here

gre_gor
  • 6,669
  • 9
  • 47
  • 52
Brendon Shaw
  • 141
  • 13
  • 2
    [**Do not post images of code or errors!**](https://meta.stackoverflow.com/q/303812/995714) Images and screenshots can be a nice addition to a post, but please make sure the post is still clear and useful without them. If you post images of code or error messages make sure you also copy and paste or type the actual code/message into the post directly. – Rob Jan 04 '19 at 13:41
  • 1
    How is cDataBuffer declared? What is the value of echoMAX_DATA_LENGTH? – romkey Jan 04 '19 at 16:49
  • char cDataBuffer[ echoMAX_DATA_LENGTH ]; – Brendon Shaw Jan 05 '19 at 07:32
  • #define echoMAX_DATA_LENGTH 20 (this could be too short for my message length, it's cutting off the JSON packet) – Brendon Shaw Jan 05 '19 at 07:33
  • 1
    Yeah, how would that possibly work if your buffer is too small for the JSON? – romkey Jan 05 '19 at 15:49

1 Answers1

1

Increase echoMAX_DATA_LENGTH to be large enough to fit your entire JSON message.

The static part of the JSON in your code (without the values filled in by snprintf()) is 34 characters, so there's no way this could ever work with echoMAX_DATA_LENGTH set to 20 - it would always produce a fragment of JSON instead of an entire JSON object.

Remember that the length that snprintf() uses includes a byte for the C string terminating character '\0', so you'll want to make echoMAX_DATA_LENGTH be one greater than the maximum total JSON message length.

When you increase echoMAX_DATA_LENGTH, try adding a debug message after the snprintf() so that you can see the JSON you're generating. If your code is set up to use Serial already, add:

Serial.println(cDataBuffer);

after the snprintf() so you can confirm that you've generated the JSON properly.

romkey
  • 6,218
  • 3
  • 16
  • 12