coreMQTT v2.1.0
MQTT 3.1.1 Client Library
 
Loading...
Searching...
No Matches
MQTT_Publish
const MQTTPublishInfo_t * pPublishInfo,
uint16_t packetId );
MQTTStatus_t MQTT_Publish(MQTTContext_t *pContext, const MQTTPublishInfo_t *pPublishInfo, uint16_t packetId)
Publishes a message to the given topic name.
Definition: core_mqtt.c:2747
MQTTStatus_t
Return codes from MQTT functions.
Definition: core_mqtt_serializer.h:99
A struct representing an MQTT connection.
Definition: core_mqtt.h:162
MQTT PUBLISH packet parameters.
Definition: core_mqtt_serializer.h:214

Publishes a message to the given topic name.

Parameters
[in]pContextInitialized MQTT context.
[in]pPublishInfoMQTT PUBLISH packet parameters.
[in]packetIdpacket ID generated by MQTT_GetPacketId.
Returns
MQTTNoMemory if pBuffer is too small to hold the MQTT packet; MQTTBadParameter if invalid parameters are passed; MQTTSendFailed if transport write failed; MQTTSuccess otherwise.

Example

// Variables used in this example.
MQTTStatus_t status;
MQTTPublishInfo_t publishInfo;
uint16_t packetId;
// This context is assumed to be initialized and connected.
MQTTContext_t * pContext;
// QoS of publish.
publishInfo.qos = MQTTQoS1;
publishInfo.pTopicName = "/some/topic/name";
publishInfo.topicNameLength = strlen( publishInfo.pTopicName );
publishInfo.pPayload = "Hello World!";
publishInfo.payloadLength = strlen( "Hello World!" );
// Packet ID is needed for QoS > 0.
packetId = MQTT_GetPacketId( pContext );
status = MQTT_Publish( pContext, &publishInfo, packetId );
if( status == MQTTSuccess )
{
// Since the QoS is > 0, we will need to call MQTT_ReceiveLoop()
// or MQTT_ProcessLoop() to process the publish acknowledgments.
}
uint16_t MQTT_GetPacketId(MQTTContext_t *pContext)
Get a packet ID that is valid according to the MQTT 3.1.1 spec.
Definition: core_mqtt.c:3104
@ MQTTSuccess
Definition: core_mqtt_serializer.h:100
@ MQTTQoS1
Definition: core_mqtt_serializer.h:123
MQTTQoS_t qos
Quality of Service for message.
Definition: core_mqtt_serializer.h:218
uint16_t topicNameLength
Length of topic name.
Definition: core_mqtt_serializer.h:238
size_t payloadLength
Message payload length.
Definition: core_mqtt_serializer.h:248
const char * pTopicName
Topic name on which the message is published.
Definition: core_mqtt_serializer.h:233
const void * pPayload
Message payload.
Definition: core_mqtt_serializer.h:243