IoT protocol comparison: MQTT vs MQTT-SN[Copy link]
MQTT-SN (Sensor Networks) is the sensor version of the MQTT protocol. It was first used in zigBee wireless networks and is mainly used for battery-powered devices with limited processor power and storage capacity. MQTT based on the TCP protocol is still too heavy for some sensors. These sensors may only have dozens of bytes of memory and cannot run the TCP protocol. Differences between MQTT-SN and MQTT 1. The protocols run at different levels. MQTT runs on the TCP layer, while MQTT-SN can run on the MAC layer, IP layer, and UDP. The application scenarios of MQTT-SN are mainly various low-power sensor networks, which require battery power and are very sensitive to power consumption. The TCP virtual long connection needs to maintain a timed heartbeat, which is not a good solution for the battery. In the sensor network, the link may be interrupted at any time, and the TCP connection must be constantly re-established, which is a large consumption of power consumption.
2 QOS adds -1 level or 3 QOS -1 level. When setting, QOS flag still needs to be set to 3 ("0b11"). -1 is just a title. This level is only applicable to very simple applications. There is no need to establish or disconnect a connection, register or subscribe. At this time, the client only needs to send a public message to the gateway. The published message does not need to be confirmed. 3 Subscription topic mode MQTT-SN can use the subscription topic format of MQTT, a long name topic such as "/beijing/sen1", a short name topic with only two characters such as "s1", and a pre-defined topic ID such as "1". If a long name topic is not used, wildcards cannot be used. 4 Publishing messages is the same as subscribing to topics. You can use a short name topic with two characters or a defined topic ID. Note: The content of the topic ID may be the same or different for different clients. For example, sensor A subscribes to topic ID 1 with the content of /beijing/sen1, and sensor B also subscribes to topic ID1 with the content of /beijing/sen2. Their contents are different. 5 Network Architecture IoT Protocol Comparison: MQTT VS MQTT-SN MQTT-SN has one more node, which is the MQTT-SN gateway. The gateway's main function is protocol conversion, converting the underlying MQTT-SN protocol to the MQTT protocol format.
As shown in the figure above, there are two types of gateways. One is a transparent gateway, which only plays a role in transparent transmission and does simple unpacking and assembly. The other is an aggregation gateway, which aggregates multiple nodes together and transmits them to the message broker. 6 Gateway Discovery In MQTT-SN, the client does not need to remember the address of the gateway. The gateway will regularly broadcast ADVERTISE information with its own address, and the client receives the address. When there are multiple gateways, the gateway needs to bring its own gateway ID, and the client decides which one to connect to. At any time, a client can only connect to one gateway. The client can also send a gateway query (SEARCHGW information), and the gateway will respond with a GWINFO information. There are two parameters to prevent broadcast storms. The sending of gateway query messages requires a random time to wait. The number of gateways that the sent gateway query passes through is generally set to 1, which means that SEARCHGW information cannot be transmitted across gateways. In this way, the gateway can be flexibly configured without fixing a certain gateway. 7 Support device sleep If the device is asleep, the MQTT-SN gateway will store the message first and send it after the device wakes up. This is a bit like mobile phone text messages. After the phone is turned off, the text messages are stored in the operator's text message exchange center and sent after the phone is turned on.