利来囯际W66

关注官方微信

中文.

Usage of MQTT Gateway

Published Time:2019-08-27 Author:Shanghai ZLAN Views:12685
1. Summary

MQTT is a Publish/subscribe protocol, which adopts the publish/subscribe mode. All terminals of the Internet of Things are connected to the cloud through TCP. The cloud manages the communication content concerned by each device in the way of theme and is responsible for forwarding messages between devices. The MQTT protocol addresses one of the most fundamental issues in the Internet of Things: how data is communicated between devices and between devices and between devices and cloud services.

JSON数据采集网关工作原理示意图

MQTT gateway plays the role of transforming ordinary serial port data and Modbus RTU data into MQTT protocol, and realizes the connection between traditional devices and MQTT cloud through connecting to servers, subscribing to and publishing topics. For example, both laptops and mobiles phoness subscribe to the MQTT server for messages with a topic of temperature, and when the MQTT gateway publishes a message with a temperature sensor to this server (the current temperature is 21℃), both laptops and mobiles phoness receive the published temperature. ZLAN's MQTT product can serve as a bridge between the local device and the MQTT cloud server.

There are various TYPES of MQTT gateways in ZLAN, including the following types:

Type Model
4G DTU ZLAN8303-7 ↴
Ethernet ZLAN5143 ↴
Wifi + Ethernet ZLAN7144 ↴
Wifi ZLAN7146 ↴
Embedded MQTT gateway module (Ethernet) ZLSN2043S ↴
Embedded MQTT gateway module (wifi) ZLSN7046T ↴
Embedded MQTT gateway module (wifi + Ethernet) ZLSN7044 ↴
MQTT gateway single chip ZLAN1043 ↴
MQTT data acquisition gateway (8 channel analog input, digital quantity input/output) ZLAN6844 ↴

In addition to the data acquisition gateway through analog and digital external interface, other MQTT gateways use serial ports (RS485, RS232, RS422, TTL) to dock with devices. Support MQTT cloud platform baidu Cloud, Ali cloud, etc.

The ZLAN MQTT gateway features not only passthrough but also the ability to convert various serial port protocols to JSON format. And support the automatic acquisition of equipment. For example, if the temperature sensor USES a serial port protocol in Modbus RTU format, two problems need to be solved:

1. Automatic acquisition: The Modbus RTU protocol adopted by the temperature sensor requires the equipment to be inquired at regular intervals to obtain the current temperature. But the MQTT protocol is part of the message-on-publish mechanism and the MQTT server cannot do this; it must have the MQTT gateway automatically query the temperature sensor, store the acquired data, and regularly send it to the server. The ZLAN MQTT gateway can be configured with Modbus polling instructions or non-standard serial port device instructions.

2. Format conversion: The device side USES hexadecimal instructions, such as 01, 03, 02, 12, 34, B5, 33 data, which is difficult to be unified and recognized in the cloud. The ZLAN MQTT gateway enables Modbus RTU data to be converted into JSON format and then sent up, such as the above hexadecimal data into: {" Power ":" 1234 "} format.

To summarize, the main features of the ZLAN MQTT Gateway are:

1. Use MQTT-based protocol to establish connections with servers, and use the form of subscription and publication for data communication.

2. Support the independent design and automatic acquisition of Modbus RTU register.

3. Support the conversion of specific Modbus register contents into JSON format timed active upload.

4. Device ID is supported in JSON format to facilitate device identification in the cloud.

5. Support unsigned data and signed data, support decimal point representation, support 4-byte length data.

6. All configuration can be completed through interface configuration, and there is no need to customize the user's independent configuration.




2. MQTT usage example

MQTT can be used alone or in conjunction with JSON functionality. The MQTT function transmits serial port data through the MQTT server when used alone. That is, the data received by the serial port is used as the payload of the MQTT. At the same time, the LOAD of MQTT will be output from the serial port in the way of pass-through. Realize serial port to MQTT.

Device configuration

First search for the device, then click edit Device:

Click "Firmware and Configuration", and the configuration Download and Design dialog box will pop up:

Select "Web Directory Download", then select an empty directory, such as MQTTHTTPD directory, and click "Clear all" to clear the previous design (note that if you have previously designed with JSON, do not clear all, otherwise you will clear the previous JSON design). Then click on the MQTT configuration.

The configuration instructions here are as follows:
1. Server domain name or IP: The IP of MQTT server is filled in here, and the maximum length is 30 characters.
2. User name: is the user name of MQTT server.
3. Password: is the user's login password.
4. Client ID: Is the client ID of MQTT.
5. Subscribe to topics: This is a topic subscribed to by this device. When other devices publish this topic, the server sends it to this device. If you're just publishing, you generally don't need to fill out this field.
6. Publish topic: The topic of the data that the device sends to the server when it serial ports to MQTT.
7. MQTT Advanced parameters: Used to configure advanced parameters.
8. Save MQTT Settings: Click this button to save after the design is finished, and then click the "Download button" in the webpage download directory to download.




Now click "MQTT Advanced Parameters" (generally no advanced parameters are configured):

The instructions are as follows:
1. Protocol version: The current mainstream is version 3.1.1. If you need to select version 3.1, please select it here.
2. Survival time: The heartbeat time of MQTT is at least 10 seconds, and the default is 60 seconds.
3. Server cleanup subscription: Whether the server cleans up subscription information after the client is disconnected.
4. Whether or not to enable a will: Whether or not to have a will.
5. Last wish Theme: Last wish theme.
6. Last wish Message: A last wish message.
7. Whether to save the will: Does the server need to keep the will message sent to the client when the client is offline abnormally?
8. Will quality: the delivery quality level of the will message sent by the server.
9. Subscription quality: the delivery quality level of the subscription. In some cases, it needs to be set to 0 to prevent retransmission from causing disconnection.
10. Release quality: the delivery quality level of the client release message. In some cases, it needs to be set to 0 to prevent retransmission from causing disconnection.
11. Whether to save publication: Whether the server keeps the last message (sent to the client if there is a new client subscription).




We don't change the advanced parameters here. Directly click "Save MQTT Settings". And then hit Download

Click "OK" when the download is finished, it will return to the device management dialog box and see that the device's destination IP, working mode and destination port have been automatically modified to MQTT Settings:

If there is no automatic modification, you need to set the destination IP, working mode and destination port under the device edit dialog box. Then click Change Settings.

This completes the configuration.

Data test

After the connection is complete, the device's LINK light (usually the blue light in the middle) lights up. Indicates that the device is properly connected to the MQTT server.

Now open the serial port tool:

Open the serial port with the same baud rate as the device, and send the data "dev send", and then see the returned data "dev send" in the receive window. This is because we publish dev send to the MQTT server under the subject of Zlansub. But at the same time our device is also subscribed to the ZlanSub theme, so the server immediately sends us a subscription message, which is dev Send. This information is sent for download as the payload of the MQTT and is exported from the serial port via passthrough.

This device can also receive data if other devices send messages.

In general, users can directly transmit serial port instructions (such as Modbus RTU) directly to the MQTT server. In addition, JSON function can also be used to automatically collect data in Modbus RTU format and send data in JSON format regularly. In addition, you can also find Shanghai ZLAN to customize some non-standard instrument and upper computer protocol format.

MQTT+JSON

If you want to implement a JSON format nested within an MQTT, click the JSON Configuration button (next to the MQTT configuration button) in the Web/Program Download Tools dialog box in ZLVircom. Refer to the JSON-related configuration documentation for configuration methods.

Combining JSON and MQTT as described above achieves the following:
1. Use MQTT-based protocol to establish connections with servers, and use the form of subscription and publication for data communication. But you encapsulate the DATA in JSON format.
2. Support the independent design and automatic acquisition of Modbus RTU register.
3. Support the conversion of specific Modbus register contents into JSON format timed active upload.
4. Device ID is supported in JSON format to facilitate device identification in the cloud.

If you need MQTT+JSON to Modbus RTU, you can design MQTT and JSON in no particular order. Instead of clicking the "Clear Design" button, click the "Download" button to download the device content.

Generally, you can manually restart the device after downloading and load the Settings.

利来囯际W66