I have a structure IoT_Publish_Message_Params that needs to be prepared in order to prepare a publishing on AWS IoT. The below snippet works completely fine when I'm passing a string as the payload.
/**
* @brief Publish Message Parameters Type
*
* Defines a type for MQTT Publish messages. Used for both incoming and out going messages
*
*/
typedef struct {
QoS qos; ///< Message Quality of Service
uint8_t isRetained; ///< Retained messages are \b NOT supported by the AWS IoT Service at the time of this SDK release.
uint8_t isDup; ///< Is this message a duplicate QoS > 0 message? Handled automatically by the MQTT client.
uint16_t id; ///< Message sequence identifier. Handled automatically by the MQTT client.
void *payload; ///< Pointer to MQTT message payload (bytes).
size_t payloadLen; ///< Length of MQTT payload.
} IoT_Publish_Message_Params;
IoT_Publish_Message_Params paramsQOS0;
sprintf(cPayload, "%s : %d ", "Hello from HOME!!", i);
paramsQOS0.qos = QOS0;
paramsQOS0.payload = (void *) cPayload;
paramsQOS0.isRetained = 0;
paramsQOS0.payloadLen = strlen(cPayload);
rc = aws_iot_mqtt_publish(&client, TOPIC, TOPIC_LEN, ¶msQOS0);
Now I want to send an actual JSON payload and I'm not sure how I can do that. I've tried using cJSON to create a JSON object:
cJSON *root,*fmt;
root=cJSON_CreateObject();
paramsQOS0.payload = (void *) root
cJSON_AddItemToObject(root, "response", fmt=cJSON_CreateObject());
cJSON_AddNumberToObject(fmt,"hello", 123);
cJSON_AddNumberToObject(fmt,"bye", 321);
However, my question is, what do I pass as IoT_Publish_Message_Params.payloadLen here? And how do I pass the json object to IoT_Publish_Message_Params.payload?