我有一个结构 IoT_Publish_Message_Params 需要准备以准备在 AWS IoT 上发布。当我将字符串作为有效负载传递时,下面的代码片段完全可以正常工作。
/**
* @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);
现在我想发送一个实际的 JSON 有效负载,但我不确定我该怎么做。我尝试使用 cJSON 创建 JSON 对象:
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);
但是,我的问题是,我在这里传递什么作为 IoT_Publish_Message_Params.payloadLen ?以及如何将 json 对象传递给 IoT_Publish_Message_Params.payload?