The full name of MQTT-SN is MQTT for Sensor Networks , and its goal is to provide an application layer communication standard for embedded devices (such as Zigbee and Bluetooth) that are not in the TCP/IP protocol stack. MQTT-SN is a Publish/Subscribe message transmission protocol designed for WSN (Wireless Sensor Networks) networks.

In addition to fully supporting the MQTT protocol, the large-scale distributed IoT MQTT message server EMQX can also handle all non-MQTT protocol connection, authentication and message sending and receiving through the gateway, and provide it with a unified user layer interface. This article will introduce how to access MQTT-SN protocol devices in EMQX, and ensure device access security through authentication and authorization.

What are the advantages of MQTT-SN?

  • Compatible with MQTT protocol: The MQTT-SN communication model completely corresponds to MQTT, such as publish, subscribe, retain sessions, testament messages, etc. A unified model is beneficial to reduce the end-to-end design complexity.
  • Lightweight: In order to cope with the limited transmission bandwidth in the WSN network, the protocol design is very streamlined. For example, the topic name in a PUBLISH message is replaced by a short, two-byte Topic ID.
  • Sleep support: MQTT-SN protocol adds sleep logic to deal with low power consumption scenarios. For example, when devices go to sleep, all messages sent to them are cached on the server and delivered to them when they wake up.

MQTT-SN common deployment structure

MQTT-SN Architecture

  1. Client and Gateway are deployed in the same local area network (such as Zigbee) to communicate through MQTT-SN protocol, and Gateway reports data to MQTT Broker in the cloud through Ethernet and MQTT protocol.
  2. MQTT Broker is integrated with MQTT-SN Gateway and both are deployed in the cloud. Client communicates directly with the MQTT-SN gateway in the cloud through UDP and MQTT-SN.
  3. The third deployment method is similar to the first one, except that the MQTT-SN protocol is used to interact with the MQTT-SN gateway in the cloud.

In comparison:

  • The first solution is the most typical MQTT-SN deployment solution. This solution is very suitable for scenarios where terminals have no public network communication requirements and need to deploy gateways for unified management, such as typical smart home scenarios.
  • The second solution is common when terminal devices are deployed outdoors. They are directly connected to the cloud through a mobile network such as (NB-IoT), and a gateway cannot be deployed in the middle to process device requests.
  • The third deployment is less common, and is only a compromise between options 1 and 2. It is only used when the server can only provide MQTT-SN access services.

Therefore, MQTT-SN is mainly used in application scenarios with short distance, limited bandwidth, and low power consumption, such as smart cities, smart furniture, water and electricity meters, etc.

Use EMQX to access the MQTT-SN protocol

EMQX's MQTT-SN gateway is implemented based on MQTT-SN version 1.2 . The MQTT-SN gateway is integrated as a component in EMQX, which can allow it to be deployed at the edge or in the cloud to implement the first and second deployment structures mentioned above.

Enable MQTT-SN gateway

In EMQX 5.0, the MQTT-SN gateway can be enabled through Dashboard, HTTP-API or configuration files.

For example, to enable and configure the MQTT-SN gateway listening on UDP port 1884:

 gateway.mqttsn {

  mountpoint = "mqttsn/"

  listeners.udp.default {
    bind = 1884
    max_connections = 10240000
    max_conn_rate = 1000
  }
}

client test

Use the MQTT-SN client written in C language to test the publish and subscribe, for example:

Client ID mqttsn1 Connect and subscribe to topic t/a ,

 $ ./mqtt-sn-sub -i mqttsn1 -t t/a -p 1884 -d

Log in to the MQTT-SN gateway using the Client ID mqttsn2 t/a and publish a message to the topic Hi, This is mqttsn2 :

 $ ./mqtt-sn-pub -i mqttsn2 -p 1884 -t t/a -m 'Hi, This is mqttsn2' -d

Finally, the message can be received on the mqtt-sn-sub end:

More advanced function configuration

Configuring Client Access Authentication

Since the connection message of the MQTT-SN v1.2 protocol only defines the Client ID, there is no Username and Password. So the MQTT-SN gateway currently only supports HTTP Server authentication

For example, add an HTTP authentication for the MQTT-SN gateway via the configuration file:

 gateway.mqttsn {
  authentication {
    enable = true
    backend = "http"
    mechanism = "password_based"
    method = "post"
    connect_timeout = "5s"
    enable_pipelining = 100
    url = "<http://127.0.0.1:8080">
    headers {
      "content-type" = "application/json"
    }
    body {
      clientid = "${clientid}"
    }
    pool_size = 8
    request_timeout = "5s"
    ssl.enable = false
  }
}

In this authentication method, the Client ID is passed to the HTTP service, and the HTTP service determines whether the client has the right to access the system.

Configure publish and subscribe permissions

In EMQX 5.0, the publish and subscribe permissions of all topics are uniformly configured in Authorization . For example, to allow everyone to publish and subscribe to topics starting with mqttsn/ :

Get online and offline events

The MQTT-SN gateway publishes all device logout and logout events to two dedicated topics:

  • On-line event subject: $SYS/brokers/<node>/gateway/mqtt-sn/clients/<clientid>/connected
  • Offline event subject: $SYS/brokers/<node>/gateway/mqtt-sn/clients/<clientid>/disconnected

For example, the content of an online event message is:

 {
  "clientid": "abc",
  "username": "undefined",
  "ts": 1660285421750,
  "sockport": 1884,
  "protocol": "mqtt-sn",
  "proto_ver": "1.2",
  "proto_name": "MQTT-SN",
  "keepalive": 10,
  "ipaddress": "127.0.0.1",
  "expiry_interval": 7200000,
  "connected_at": 1660285421750,
  "clean_start": false
}

Of course, you can also get the online and offline events of the MQTT-SN gateway through the $event/client_connected and $event/client_disconnected events in the rule engine. For details, please refer to: Event topic available for FROM clause

quote

[1] OASIS Open: MQTT-SN spec v1.2

[2] Urs Hunkeler & Hong Linh Truong: MQTT-S – A Publish/Subscribe Protocol For Wireless Sensor Networks

[3] EMQX 5.0 MQTT-SN Gateway introduction

[4] OASIS Open: MQTT-SN v2.0-wd17.docx

Copyright statement: This article is original by EMQ, please indicate the source when reprinting.

Original link: https://www.emqx.com/zh/blog/connecting-mqtt-sn-devices-using-emqx


EMQX
336 声望438 粉丝

EMQ(杭州映云科技有限公司)是一家开源物联网数据基础设施软件供应商,交付全球领先的开源 MQTT 消息服务器和流处理数据库,提供基于云原生+边缘计算技术的一站式解决方案,实现企业云边端实时数据连接、移动、...