Installation and use of MQTT server EMQX

illustrate

Select EMQX as MQTT server, download link

The installation package supports including MacOS, Windows, and Linuxvarious releases and Dockerinstallation methods. Select the version of the corresponding platform before installation. Here, the environment installation of the Windowsplatform and Linuxthe release CentOSplatform are mainly recorded, as well as pythonthe sample code for message subscription and push in language.

Windows platform installation

Go to the download page and select windowsthe icon, the files and versions downloaded in this example are:emqx-windows-4.3.5.zip

Unzip the above files to the D disk (the D disk is used in this example, other disks can be selected), enter the command line, and switch the directory to d:\emqx\bin

# 进入安装盘
C:\Users\lujx>d:
# 进入安装bin目录
D:\>cd emqx\bin
# 安装服务
D:\emqx\bin>emqx install
# 输出:
D:\emqx\erts-11.0\bin\erlsrv.exe: Service emqx_4.3.5 added to system.
[SC] ChangeServiceConfig 成功
# 启动服务
D:\emqx\bin>emqx start

CentOS platform installation

Same as above, enter the download page, click CentOSthe icon with the mouse, select the version number of the operating system corresponding to the installation platform and the packaging method of the installation package. This example uses the installation of the CentOS 764-bit amdchip structurezipemqx-centos7-4.3.5-amd64.zip

# 解压文件到安装目录,本示例使用的是/usr/local/,此目录可自行指定
unzip emqx-centos7-4.3.5-amd64.zip -d /usr/local/
# 进入目录
cd /usr/local/emqx
# 启动服务
./bin/emqx start

console access

After startup, you can access http://localhost:18083the login page.
Default username: admin, password:public

Python language realizes message sending and receiving

The python language provides the mqtt dependency library paho-matt, which is used to provide an interface for data interaction with the mqtt server.
First install the dependency package:
pip install paho-mqtt

message push

import datetime
import time

import paho.mqtt.client as mqtt


def on_connect(client, userdata, flags, rc):
    print("Connected with result code: " + str(rc))


def on_message(client, userdata, msg):
    print(msg.topic + " " + str(msg.payload))

mqtt_server = "127.0.0.1"
client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
client.connect(mqtt_server, 1883, 600)  # 600为keepalive的时间间隔

while True:
    msg = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S:%f')
    # 消息推送 
    client.publish('test', payload=msg, qos=0)
    time.sleep(0.02)

news subscription

def on_connect(client: mqtt.Client, userdata, flags, rc):
    print("Connected with result code: " + str(rc))


def on_message(client, userdata, msg: mqtt.MQTTMessage):
    global msg_global
    msg_global = str(msg.payload, encoding='utf-8')


mqtt_server="127.0.0.1"
client = mqtt.Client()
# 设置连接成功事件回调函数
client.on_connect = on_connect
# 设置收到消息后的回调函数
client.on_message = on_message
client.connect(mqtt_server, 1883, 600)  # 600为keepalive的时间间隔
client.subscribe('test', qos=0)
client.loop_forever()  # 保持连接 阻塞式

Blocking and non-blocking message subscription

In the above example, client.loop_forever()the method used to enable message subscription is blocking, that is, any code written after this line of code will not be executed.

Similarly, paho-mqtt provides a non-blocking method client.start_loop(), after which you can use a loop to process business logic, paste the sample code, as follows:

import datetime
import time

import paho.mqtt.client as mqtt

msg_global = ""


def on_connect(client: mqtt.Client, userdata, flags, rc):
    print("Connected with result code: " + str(rc))


def on_message(client, userdata, msg: mqtt.MQTTMessage):
    global msg_global
    msg_global = str(msg.payload, encoding='utf-8')

mqtt_server="127.0.0.1"
client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
client.connect(mqtt_server, 1883, 600)  # 600为keepalive的时间间隔
client.subscribe('test', qos=0)
# client.loop_forever()  # 保持连接 阻塞式
client.loop_start()  # 启动连接 非阻塞式

# 业务处理,回调方法中,将收到的消息用过全局变量接收,在主线程中使用此全局变量。
while True:
    if msg_global is not None:
        local_time = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S:%f')
        print(local_time, msg_global)
        msg_global = None

Guess you like

Origin blog.csdn.net/LJX_ahut/article/details/119189928