MQTT Encoding and Decoding: Difference between revisions
Jump to navigation
Jump to search
(Created page with "MQTT =MQTT Encoding and Decoding= The MQTT protocol is very lightweight (does not use many bytes to get the message across). Each MQTT packet consists of: * Fixed head...") |
No edit summary |
||
Line 15: | Line 15: | ||
Text is encoded in UTF-8 without a terminating "0" byte, after the number of bytes in the text encoded as two bytes, MSB first. | Text is encoded in UTF-8 without a terminating "0" byte, after the number of bytes in the text encoded as two bytes, MSB first. | ||
{| | {| class="wikitable" style="border: 2px solid" | ||
! Type | ! rowspan="2" | Type | ||
! Number | ! rowspan="2" | Number | ||
! Direction | ! rowspan="2" | Direction | ||
! Flags | ! rowspan="2" | Flags | ||
! Variable Header | ! rowspan="1" colspan="2" | Variable Header | ||
! Payload | ! rowspan="2" | Payload | ||
|- | |||
! Msg ID | |||
! | |||
|- | |- | ||
| CONNECT | | CONNECT | ||
Line 27: | Line 30: | ||
| Device to Broker | | Device to Broker | ||
| 0 | | 0 | ||
| | | - | ||
| | | "MQTT":String<br/>protocol level (4):byte<br/>connect flags:byte (b7: user, b6: password, b5: will retain, b4-3: will QoS, b2: will, b1: clean session)<br/>keep alive:word | ||
| client ID:String<br/>will topic:String (if present)<br/>will payload:Array (if present)<br/>user name:String (if present)<br/>password:Array (if present) | |||
|- | |- | ||
| CONNACK | | CONNACK | ||
Line 34: | Line 38: | ||
| Broker to Device | | Broker to Device | ||
| 0 | | 0 | ||
| | | - | ||
| | | connect acknowledge flags:byte (b0: session present)<br/>connect return code:byte (0: connection accepted, 1: unacceptable | ||
protocol version, 2: identifier rejected, 3: server unavailable, 4: bad user name or password, 5: not authorized) | |||
| - | |||
|- | |- | ||
| PUBLISH | | PUBLISH | ||
Line 41: | Line 47: | ||
| Both | | Both | ||
| b3: DUP, b2-1: QoS, b0: RETAIN | | b3: DUP, b2-1: QoS, b0: RETAIN | ||
| | | QoS > 0 ? y | ||
| | | topic name:String<br/>message ID:word | ||
| payload:Array (optional) | |||
|- | |- | ||
| PUBACK | | PUBACK | ||
Line 48: | Line 55: | ||
| Both | | Both | ||
| 0 | | 0 | ||
| | | y | ||
| | | message ID of PUBLISH packet:word | ||
| - | |||
|- | |- | ||
| PUBREC | | PUBREC | ||
Line 55: | Line 63: | ||
| Both | | Both | ||
| 0 | | 0 | ||
| | | y | ||
| | | message ID of PUBLISH packet:word | ||
| - | |||
|- | |- | ||
| PUBREL | | PUBREL | ||
Line 62: | Line 71: | ||
| Both | | Both | ||
| 1 | | 1 | ||
| | | y | ||
| | | message ID of PUBREC packet:word | ||
| - | |||
|- | |- | ||
| PUBCOMP | | PUBCOMP | ||
Line 69: | Line 79: | ||
| Both | | Both | ||
| 0 | | 0 | ||
| | | y | ||
| | | message ID of PUBREL packet:word | ||
| - | |||
|- | |- | ||
| SUBSCRIBE | | SUBSCRIBE | ||
Line 76: | Line 87: | ||
| Device to Broker | | Device to Broker | ||
| 1 | | 1 | ||
| | | y | ||
| | | message ID:word | ||
| topic filter:String<br/>requested QoS:byte (b1-0: QoS)<br/>(repeat sequence one or more times) | |||
|- | |- | ||
| SUBACK | | SUBACK | ||
Line 83: | Line 95: | ||
| Broker to Device | | Broker to Device | ||
| 0 | | 0 | ||
| | | y | ||
| | | message ID of SUBSCRIBE packet:word | ||
| return code:byte (b7: failure, b1-0: maximum QoS)<br/>(repeat sequence one or more times) | |||
|- | |- | ||
| UNSUBSCRIBE | | UNSUBSCRIBE | ||
Line 90: | Line 103: | ||
| Device to Broker | | Device to Broker | ||
| 1 | | 1 | ||
| | | y | ||
| | | message ID:word | ||
| topic filter:String<br/>(repeat sequence one or more times) | |||
|- | |- | ||
| UNSUBACK | | UNSUBACK | ||
Line 97: | Line 111: | ||
| Broker to Device | | Broker to Device | ||
| 0 | | 0 | ||
| | | y | ||
| | | message ID of SUBSCRIBE packet:word | ||
| - | |||
|- | |- | ||
| PINGREQ | | PINGREQ | ||
Line 104: | Line 119: | ||
| Device to Broker | | Device to Broker | ||
| 0 | | 0 | ||
| - | |||
| | | | ||
| | | - | ||
|- | |- | ||
| PINGRESP | | PINGRESP | ||
Line 111: | Line 127: | ||
| Broker to Device | | Broker to Device | ||
| 0 | | 0 | ||
| - | |||
| | | | ||
| | | - | ||
|- | |- | ||
| DISCONNECT | | DISCONNECT | ||
Line 118: | Line 135: | ||
| Device to Broker | | Device to Broker | ||
| 0 | | 0 | ||
|- | | - | ||
| | | | ||
| - | |||
|} | |} | ||
[[MQTT]] | [[MQTT]] |
Revision as of 08:26, 4 July 2017
MQTT Encoding and Decoding
The MQTT protocol is very lightweight (does not use many bytes to get the message across).
Each MQTT packet consists of:
- Fixed header
- b7-4: MQTT Packet Typ, b3-0: Flags
- Remaining length - this one is a bit tricky and consumes between 1 and 4 bytes. Encoding is least significant bits first in b6-0 while b7 is a "continuation bit" ('1' meaning "another byte follows").
- Variable header (optional), depending on the package type
- Payload (optional), depending on the package type
Text is encoded in UTF-8 without a terminating "0" byte, after the number of bytes in the text encoded as two bytes, MSB first.
Type | Number | Direction | Flags | Variable Header | Payload | |
---|---|---|---|---|---|---|
Msg ID | ||||||
CONNECT | 1 | Device to Broker | 0 | - | "MQTT":String protocol level (4):byte connect flags:byte (b7: user, b6: password, b5: will retain, b4-3: will QoS, b2: will, b1: clean session) keep alive:word |
client ID:String will topic:String (if present) will payload:Array (if present) user name:String (if present) password:Array (if present) |
CONNACK | 2 | Broker to Device | 0 | - | connect acknowledge flags:byte (b0: session present) connect return code:byte (0: connection accepted, 1: unacceptable protocol version, 2: identifier rejected, 3: server unavailable, 4: bad user name or password, 5: not authorized) |
- |
PUBLISH | 3 | Both | b3: DUP, b2-1: QoS, b0: RETAIN | QoS > 0 ? y | topic name:String message ID:word |
payload:Array (optional) |
PUBACK | 4 | Both | 0 | y | message ID of PUBLISH packet:word | - |
PUBREC | 5 | Both | 0 | y | message ID of PUBLISH packet:word | - |
PUBREL | 6 | Both | 1 | y | message ID of PUBREC packet:word | - |
PUBCOMP | 7 | Both | 0 | y | message ID of PUBREL packet:word | - |
SUBSCRIBE | 8 | Device to Broker | 1 | y | message ID:word | topic filter:String requested QoS:byte (b1-0: QoS) (repeat sequence one or more times) |
SUBACK | 9 | Broker to Device | 0 | y | message ID of SUBSCRIBE packet:word | return code:byte (b7: failure, b1-0: maximum QoS) (repeat sequence one or more times) |
UNSUBSCRIBE | 10 | Device to Broker | 1 | y | message ID:word | topic filter:String (repeat sequence one or more times) |
UNSUBACK | 11 | Broker to Device | 0 | y | message ID of SUBSCRIBE packet:word | - |
PINGREQ | 12 | Device to Broker | 0 | - | - | |
PINGRESP | 13 | Broker to Device | 0 | - | - | |
DISCONNECT | 14 | Device to Broker | 0 | - | - |