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?
As I see it you have two options. Send the JSON as a string, or as raw bytes.
If you want to send it as a string (e.g.
{"CarType":"BMW","carID":"bmw123"}
), then you want to convert it to a string. Found some code here.However, it would be much more efficient to send it as raw bytes. For this, you would need a pointer to the start of the object, and the size of the object in bytes. Quickly scanning the GitHub Page I found the
cJSON_GetArraySize
which apparently returns the object size.Then it should look something like this:
Disclaimer, I haven't used cJSON and haven't tested the code. I'm trying to show you which direction to take.