CONTENTS
Description
MQTT (Message Queue Telemetry Transport) is a lightweight publish-subscribe-based messaging protocol. It is designed for connections with remote locations where a "small code footprint" is required or the network bandwidth is limited, such as in the IoT.
This article provides a guide on how to configure MQTT settings to work as a MQTT client to publish data and how to get router information and data.
Requirement
- Milesight Router: UR3x (Firmware version 3x.3.0.7 and later), UR41 (Firmware version 41.0.0.3 and later)
- MQTT Server/Broker
- MQTT Client tool: take MQTT Explorer as example
Configuration
Step1. Connect router to MQTT broker.
1. Ensure the device has accessed the network.
2. Go to Service > MQTT page to add a MQTT connection.
Fill in the MQTT broker information and click save.
Item | Description |
General | |
Name | Customize a unique connection name. It is not allowed to change after save. |
Enable | Enable or disable this MQTT connection. |
Broker Address | IP address or domain name of MQTT broker to receive data. |
Broker Port | Communication port of MQTT broker to receive data. |
Client ID | Client ID is the unique identity of the client to the server. It must be unique when all clients are connected to the same server. |
Connection Timeout/s | If the client does not get a response after the connection timeout, the connection will be considered as broken. The Range: 1-65535. |
Keep Alive Interval/s | After the client is connected to the server, the client will send heartbeat packet to the server regularly to keep alive. Range: 1-65535. |
Auto Reconnect | When connection is broken, try to reconnect the server automatically. |
Reconnect Period | When connection is broken, the period to reconnect the server periodically. |
Clean Session | When enabled, the connection will create a temporary session and all information will lose when the client is disconnected from broker; when disabled, the connection will create a persistent session that will remain and save offline messages until the session logs out overtime. |
User Credentials | |
Enable | Enable user credentials as required. |
Username | The username used for connecting to the MQTT broker. |
Password | The password used for connecting to the MQTT broker. |
TLS | |
Enable | Enable the TLS encryption in MQTT communication as required. |
Mode | Select from Self signed certificates, CA signed server certificate. CA signed server certificate: verify with the certificate issued by Certificate Authority (CA) that pre-loaded on the device. Self signed certificates: upload the custom CA certificates, client certificates and secret key for verification. |
Note: if MQTT broker is HiveMQ, please do enable TLS and set the option as CA signed server certificate.
After MQTT configuration complete, you can check connection status here:
Step2. Connect MQTT client to MQTT broker.
MQTT Explorer is a comprehensive MQTT client and it can be replaced to other kinds of MQTT client tools(MQTT.fx, MQTTX, etc.).
Open the MQTT Explorer software, fill in the MQTT server information and connect the client to the MQTT broker the same as Milesight router. This client is used to receive the router information and data.
Step3. Customize the topics to subscribe router data.
Milesight router supports to work as a MQTT client to forward data and router information to MQTT broker in two ways:
1. Active Report: The router publishes the data actively and automatically.
2. Passive Report: send requests to the router to enquire the router information.
Active Report
1. Customize the MQTT topic of active report and the interval to publish/report data to MQTT broker. The router will publish data to MQTT broker as json format:
{ "code": "1", //1: Active report, 2: Passive report "id": "6218A3414659", //Device SN "time": "2024-04-15T07:32:17Z", //UTC report time "status": "systeminfo", //Report type "data": { ......//See Appendix } }
Milesight routers support below types of active reports:
- System Status: including system info, system status, cellular, Ethernet and GPS(GPS model only)
- Serial port DTU data
- Modbus Client data: when serial mode is Modbus client, create Modbus channels to send Modbus RTU/TCP read commands to enquire the data and forward to MQTT broker.
- GPS data: forward detailed information of GPS as required, this only works with GPS model.
2. Subscribe the topics to receive router information and data on MQTT Explorer software:
The router will send the data as json format.
Passive Report
1. Customize the Status Request topic to send MQTT request and Status Response topic to receive replies.
2. Subscribe the both Status Request and Status Response topics on MQTT Explorer software:
3. Send request to Status Request topic, the request format should be json format.
Status request format:
{ "id": "1", //Random value "status": "systeminfo" //Report type }
Status response format:
{ "id": "1", //The same as request "code": "2", //1: Active report, 2: Passive report "time": "2024-04-15T07:32:17Z", //UTC report time "status": "systeminfo", //Report type "data": { ......//See Appendix } }
Milesight routers support 5 types of passive reports: systeminfo, systemstatus, cellular, ethernet, gps.
4. After sending, check the replies under Status Response topic.
Appendix - MQTT Report Content
Take active report format as example to show the report content.
System Status-System Info
{ "code": "1", "id": "6218A3414659", "time": "2024-04-15T07:32:17Z", "status": "systeminfo", "data": { "model": "UR32-L00E-P-W", "sn": "6218A3414659", "firmware_ver": "32.3.0.7-r3", "hardware_ver": "V2.1" } }
System Status-System Status
{ "code": "1", "id": "6218A3414659", "time": "2024-04-15T07:27:19Z", "status": "systemstatus", "data": { "uptime": 115956, "cpu_load": "44%", "cpu_temp": 62, //Unit: °C "memory_total": 128, //Unit: MB "memory_free": 25, "flash_total": 128, "flash_free": 80, "tfcard_state": 0, "tfcard_total": 0, "tfcard_free": 0 } }
System Status-Cellular
{ "code": "1", "id": "6218A3414659", "time": "2024-04-15T07:27:19Z", "status": "cellular", "data": { "modem": { "modem_status": "No SIM Card", "model": "EC25", "version": "EC25ECGAR06A06M1G", "cur_sim": "SIM1", "signal": "0asu (-113dBm)", "register": "Not registered", "imsi": "", "iccid": "", "net_provider": "", "net_type": "", "plmnid": "", "lac": "0", "cellid": "0", "imei": "860425048159956" }, "network": { "status": "Disconnected", "ip": "0.0.0.0", "netmask": "0.0.0.0", "gate": "0.0.0.0", "dns": "0.0.0.0", "time": "0 days, 00:00:00", "ipv6": "::", "gatev6": "::", "dnsv6": "::" }, "statistics": { "sim1": "RX: 0.0 MiB TX: 0.0 MiB ALL: 0.0 MiB", "sim2": "RX: 0.0 MiB TX: 0.0 MiB ALL: 0.0 MiB" } } }
System Status-Ethernet
{ "code": "1", "id": "6218A3414659", "time": "2024-04-15T07:27:21Z", "status": "ethernet", "data": { "wan": { "status": 1, //1: up, 0: down "mac": "24:e1:24:f0:ef:81", "ip": "192.168.45.194/24", "ipv6": "fe80::26e1:24ff:fef0:ef7f/64", "time": "0 days, 13:38:58" }, "lan": { "ip": "192.168.2.1/24", "ipv6": "fe80::7c94:81ff:fe8d:ca4c/64", "connected": 0 } } }
System Status-GPS
{ "code": "1", "id": "6218A3414659", "time": "2024-04-15T07:27:21Z", "status": "gps", "data": { "status": 53, //51: GPS is disabled, 52: Weak signal, 53: Normal report //Below information only reports when status id is 53 "time": "2024-04-15T07:27:21Z", "sats": 8, //Satellites in Use "sativ": 12, //Satellites in View "latitude": "24.44344 N", "longitude": "118.10360 E", "height": 5.8m, //The unit changes depends on real location "speed": 10.101206 km/h } }
Serial Port DTU Data
{ "data": "xxxxxxx", }
Modbus Client Data
{ "Channel1": "0603020010", //Channel1 is Modbus channel name }
GPS Data
$GPRMC,,V,,,,,,,,,,N*53 $GPGGA,,,,,,0,,,,,,,,*66 $GPGSA,A,1,,,,,,,,,,,,,,,*1E
----END----