# Communication Protocols of an Industrial Internet of Things Environment: A Comparative Study
## 5 Comparison between MODBUS TCP and MQTT  
In this section, a comparison between the MODBUS TCP and the MQTT protocol is conducted from three aspects. These aspects are the communication model of the protocol in the original IEEE model, the message exchange philosophy, and the required number of bytes for some message types that demonstrate the overhead of each type. However, the other protocols are not compared for the reasons listed above. As shown in Figure 5, the MQTT and the MODBUS TCP protocols are both in the same level in the IEEE model. While the MQTT protocol encodes the user data in UTF-8, the MODBUS TCP uses a byte-encoded frame format for the user data, which is intended for industrial applications. Figure 6 illustrates a comparison between MQTT philosophy and MODBUS philosophy, from the perspective of the exchange of messages. The request of the MODBUS query uses a TCP-based connection and employs a frame-format based on an application-layer message structure, which is optimized and dedicated for telecontrol and monitoring. The case is different with MQTT; while the first client (publisher) produces an event using four messages, the second client (subscriber) consumes that event in six messages. The publisher sends a connect packet (CONNECT), with username (un) and password (pwd) if required, to the server (broker) trying to establish a TCP connection. The server acknowledges the attempt with a CONNACK packet, telling the client (publisher) whether the connection is successfully established. Then, the client publishes the temperature value, for example, via a PUBLISH packet, with temp topic and a value of 22.7. The client ends the publish event with the server by sending a DISCONNECT packet. Meanwhile, and in addition to the aforementioned steps, the subscriber must SUBSCRIBE to the same topic (temp) to receive the published messages of interest. The subscription packet is acknowledged with the SUBACK packet. When the broker receives the message that handles the temperature value, via the same PUBLISH packet, it forwards it to the subscriber, which may then terminate the connection using the DISCONNECT message. As a comparison between MODBUS TCP and MQTT messages, Figure 7 shows the required number of bytes of the MODBUS messages, compared to those of MQTT. The message of the MODBUS TCP requires 27 bytes to read the values of three registers using the function code \"read input registers\" (FC = 0x04), as shown in Figures 1 and 2. However, the message of the MODBUS TCP requires 29 bytes  
Figure 5: The IEEE model (**a**); compared to the MODBUS TCP (**b**); and the MQTT (**c**). Figure 6: Comparison of protocols for the exchange of messages: (**a**) MQTT; (**b**) MODBUS TCP. to write the values of two registers using the function code \"write multiple registers\" (FC = 0x10) in a remote device. In both cases, the MODBUS TCP issues a query from the client side and a response from the server side. Meanwhile, if the function code \"read/write multiple registers\" (0x17) is used for both operations, the total consumed bytes for reading three registers and writing two registers within the same connection is 36 bytes. For MQTT, in this example, publishing a 10-byte payload by the producer and consuming it by the subscriber requires 115 bytes. It is important to mention that the total consumed bytes of MQTT publish and subscribe messages depends on the topic length and the length of the user data. Figure 8 shows the overhead portion of the three messages for a constant payload of 10 bytes. The function code \"read/write multiple registers\" (0x17) has an overhead of 26 bytes out of 36 bytes. The function codes \"read input registers\" (0x04) and \"write multiple registers\" (0x10) have a total overhead of 46 bytes out of 56-bytes (the total size). In comparison, the MQTT publish-subscribe message has an overhead of 105 bytes out of 115 bytes. Figure 8: MQTT and MODBUS overheads for a payload of 10 bytes. Figure 7: Required bytes of MQTT messages compared to those of MODBUS.