Build an MQTT server under Windows

1. MQTT server software screening

MQ Telemetry Transport (MQTT) is a lightweight agent-based publish/subscribe message transport protocol, designed to be open, simple, lightweight, and easy to implement. These features make it suitable for low-bandwidth constrained environments.
Features include the following:

  • Use the publish/subscribe message mode to provide one-to-many message publishing and decouple application programs.
  • Message transmission with payload content masked.
  • Network connectivity is provided using TCP/IP.
  • Small transfers with little overhead (fixed-length header is 2 bytes), and protocol exchanges are minimized to reduce network traffic.
  • Mechanism for notifying relevant parties of abnormal client interruption using the Last Will and Testament features.
  • There are three messaging qualities of service:
  • (1) "At most once", message publishing completely depends on the underlying TCP/IP network. Message loss or duplication can occur. This level can be used in the following situations, environmental sensor data, it does not matter if a read record is lost, because there will be a second send in the near future.
  • (2) "at least once", to ensure that the message arrives, but message duplication may occur.
  • (3) "Only once", to ensure that the message arrives once. This level can be used in cases where duplicate or missing messages can lead to incorrect results in billing systems.
    insert image description here

There are many softwares that can build MQTT server, such as:

  • mosquitto, Eclipse Mosquitto is an MQTT server implemented in C language.
  • EMQX, an MQTT server developed in Erlang language, has a built-in powerful rule engine and supports many other IoT protocols such as MQTT-SN, CoAP, LwM2M, etc.
  • Mosca, an MQTT server developed with Node.JS, is easy to use.
  • VerneMQ, an MQTT server developed in Erlang.

This article takes the building process of mosquitto under windows as an example to introduce.

2. MQTT server software mosquitto download

1. Enter the mosquitto download website: https://mosquitto.org/download/, as shown in the figure below:
insert image description here
2. Click the link in the red box in the above figure to download, and download it to the local computer, as shown in the figure below:
insert image description here
3. Right-click mosquitto-2.0. 14-install-windows-x64.exe is installed as an administrator, as shown in the figure below:
insert image description here
4. Click the "Next >" button, as shown in the figure below:
insert image description here
5. Continue to click the "Next >" button, as shown in the figure below:
insert image description here
6. You can change the installation directory , I changed to the D drive, click the "Install" button, as shown in the figure below:
insert image description here
7. Wait for the installation to complete, as shown in the figure below:
insert image description here
8. Click the "Finish" button, and the installation is complete.

3. Modify the mosquitto configuration and start the mosquitto service

1. After the installation is complete, you need to modify the configuration file. The location of the configuration file is: D:\mosquitto\mosquitto.conf, as follows: 2.
insert image description here
Here we mainly change the listening port and enable user authentication (that is, set the user name and password)
2.1 Modify the listening port
Open mosquitto.conf, find linstener, uncomment the # in front, and specify the port to listen on, as follows:

listener 7788

2.2 Set username and password
2.2.1 Find the allow_anonymous node, the function of this node is whether to enable anonymous user login, the default is true. Open this configuration (remove the # in front) and change its value to true

Before: #allow_anonymous
After: allow_anonymous false

2.2.2 Find the password_file node, which tells the server where the user you want to configure will be stored. Open this configuration and specify the path of the pwfile.example file (note that it is an absolute path)

Before modification: #password_file
After modification: password_file D:\pwfile.example

3. Find Mosquitto Broker in Management Tools -> Services, and start the mosquitto service, as shown in the figure below:
insert image description here
4. Open CMD as an administrator, switch to the d:/mosquitto directory, and run the following command to add a user name and password:

mosquitto_passwd.exe -c d:/pwfile.example admin

It prompts to enter the password twice consecutively, and the creation is successful. Command explanation: -c Create a user, /etc/mosquitto/pwfile.example is to create the user into the pwfile.example file, admin is the user name. As shown below:
insert image description here

Note: After adding the user and password, the mosquitto service needs to be restarted to take effect.

4. Use the mosquitto tool to test subscription and release

Then start two CMD windows as an administrator, and enter the d:/mosquitto directory, cmd window 1 as a subscriber, and cmd window 2 as a publisher, as shown in the figure below: Execute the following command
insert image description here
in cmd window 1 to subscribe to messages

mosquitto_sub.exe -h 127.0.0.1 -p 7788 -u admin -P 123456 -v -t sensor

Execute the following command in cmd window 2 to send the message

mosquitto_pub.exe -h 127.0.0.1 -p 7788 -u admin -P 123456 -t sensor -m "消息测试"

As shown below:
insert image description here

5. Visual MQTT client tool MQTTX use

There are many MQTT client testing tools, and MQTTX is undoubtedly the best among them. The following will introduce its use process.
1. First download from the official website, MQTTX download address: https://github.com/emqx/MQTTX/releases
insert image description here
2. Choose the version suitable for your operating system to download, as shown in the figure below:
insert image description here
3. After downloading, the figure below:
insert image description here
4. To manage Install MQTTX-Setup-1.7.3.exe as an administrator. If the prompt of Microsoft Defender SmartScreen blocking appears, click "Run anyway", as shown in the figure below: 5. The
insert image description here
installation steps are as shown in the figure below:
insert image description here
6. The installation directory can be changed during the installation process, as follows Figure:
insert image description here
7. Click the "Install" button, as shown in the figure below:
insert image description here
8. After the installation is complete, a completion prompt will appear, as shown in the figure below:
insert image description here
9. Check and run MQTTX (R), and click the "Finish" button to run MQTTX, as shown in the figure below:
insert image description here
10 . Click the "New Connection" button, and a new connection dialog box will appear. According to the information of the MQTT server, enter the corresponding parameters, as shown in the figure below:
insert image description here

11. Click the Connect button, as shown in the figure below:
insert image description here
12. If you want to subscribe to a topic, click the "New Subscription" button, and a new subscription dialog box will appear, as shown in the figure below:
insert image description here
13. After entering the corresponding subject, click the "Confirm" button, as shown below Figure:
insert image description here
14. Use the mosquitto_pub.exe publishing tool to test a message in combination with the CMD window, as shown in the figure
insert image description here
below: As can be seen from the figure above, the subscribed topic has received the test message.

Guess you like

Origin blog.csdn.net/zlbdmm/article/details/125297796