Publish a message on a topic.
Attention: Valid only with Altair Communication Extension.
Syntax
[mid,status,messageinfo] = mqttpublish(clientid, topic, Payload, qos, retain)
Inputs
- clientid
- client id, an output of mqttclient.
- Type: string
- topic
- topic to which message to be published.
- Type: string
- payload
- Message to publish.
- Type: string
- qos
- Quality of service, valid options are 0,1,2.
- Type: scalar
-
- qos = 0:
- This level guarantees a best-effort delivery. There is no guarantee of delivery. The recipient does not acknowledge receipt of the message and the message is not stored and re-transmitted by the sender.
- qos = 1:
- This level guarantees that a message is delivered at least one time to the receiver. The sender stores the message until it gets a PUBACK packet from the receiver that acknowledges receipt of the message. It is possible for a message to be sent or delivered multiple times.
- qos = 2:
- This level guarantees that each message is received only once by the intended recipients. It is the safest and slowest quality of service level.
- retain
- to specify whether to retain the the message.Valid values are falseand true.
- Type: Boolean
Outputs
- mid
- message id of publish command.
- Type: scalar
- status
- status return codes.
- Type: scalar
- messageinfo
- description of publish status flag.
- Type: string
-
- status = 0:
- on success.
- status = 1:
- out of memory condition occurred.
- status = 2:
- protocol error communicating with the broker.
- status = 3:
- input parameters were invalid.
- status = 4:
- client isn’t connected to a broker.
- status = 9:
- payloadlen is too large.
- status = 18:
- topic is not valid UTF-8
- status = 24:
- QoS is greater than that supported by the broker.
Examples
publish to a topic with
cld = mqttclient('test.mosquitto.org','port',1883);
[mid, rc, Mqttmessageinfo] = mqttpublish(cld, 'topic100','hello - test mqtt broker',2,true);
mid = 1
rc = 0
Mqttmessageinfo = on success.
publish to a topic with on_publish callback
function test_publishcallback(clientid, mid)
printf('publish callback is triggered successfully\n');
printf('message id: %s\n', num2str(mid));
%printf('return message: %s\n', Mqttmessageinfo);
end
cld = mqttclient('test.mosquitto.org','port',1883,'on_publish','test_publishcallback');
[mid, rc, Mqttmessageinfo] = mqttpublish(cld, 'topic100','hello - test mqtt broker',2,true);
publish callback is triggered successfully
message id: 1