当前位置:   article > 正文

MQTT服务端[emqx]+Python3+MQTT的Python3客户端[paho-mqtt]+可视化客户端MQTTX_python3 paho-mqtt客户端

python3 paho-mqtt客户端

1.简介

MQTT协议是一种基于发布/订阅模式的轻量级物联网消息传输协议,可以用极少代码和带宽为联网设备提供实时可靠消息服务,广泛应用于物联网,移动互联网等。

2.1MQTT服务端[emqx]安装

1.安装yum源
2.安装mqtt服务端
3.启动mqtt服务端

curl -s https://assets.emqx.com/scripts/install-emqx-rpm.sh | sudo bash

sudo yum install emqx

systemctl start emqx
  • 1
  • 2
  • 3
  • 4
  • 5

2.2可视化客户端MQTTX安装

国内下载网址:https://mqttx.app/zh
安装包是exe文件,一路next安装即可

在这里插入图片描述

2.3Python3客户端[paho-mqtt]安装

pip install paho-mqtt
  • 1

3.MQTTX使用

MQTT服务端监听的TCP端口是1883 ,界面采用的是聊天工具的形式,简单直观好用

连接MQTT服务端

在这里插入图片描述

创建Topic

在这里插入图片描述

在这里插入图片描述

向指定Topic发送消息并接收

在这里插入图片描述

4.Python3客户端[paho-mqtt]使用

1.实例化客户端,CLIENT_ID是自定义的客户端ID,用来区分连接到服务端的客户端,可以不传入,Client类会自动生成一个ID

mqtt_client.Client(CLIENT_ID)
  • 1

2.on_connect和on_connect_fail是自定义的建立连接和断开连接时要回调的函数

def on_connect(client, userdata, flags, rc):
            if rc == 0:
                print("Connect Success Return Code {}".format(rc))
            else:
                print("Failed Connect, Return Code {}".format(rc))

def on_connect_fail(client, userdata):
    print("Failed Connect")

client = mqtt_client.Client(CLIENT_ID)
client.on_connect = on_connect
client.on_connect_fail = on_connect_fail
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

3.向指定Topic发送消息

def send_msg(self, client):
    for i in range(10):
        msg = "第{}次打你丫的".format(i)
        res = client.publish(TOPIC, msg)
        if res[0] == 0:
            print('SEND {} TO {} OVER'.format(msg, TOPIC))
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

4.订阅指定Topic接收消息

    def subscribe(self, client):
        def on_message(client, userdata, msg):
            print(f"RECEIVED `{msg.payload.decode()}` FROM `{msg.topic}` TOPIC")

        client.subscribe(TOPIC)
        client.on_message = on_message
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

5.网络循环的阻塞形式运行客户端

client.loop_forever()
  • 1

以下是完整代码

# -*- coding: utf-8 -*-
# -------------------------------------------------------------------------------
# Name:         mqtt_action
# Description:  
# Author:       CHEN
# Date:         2022/7/13
# -------------------------------------------------------------------------------
from paho.mqtt import client as mqtt_client
import random

SERVER_IP = '192.168.8.140'
PORT = 1883
TOPIC = "testtopic/123"
CLIENT_ID = f'python-mqtt-{random.randint(0, 1000)}'


class MqttAction:
    """
    1.连接到MQTT服务端
    2.向服务端指定TOPIC发送消息
    3.订阅指定TOPIC接收消息
    """
    def set_connect(self):
        # @client.connect_callback()
        def on_connect(client, userdata, flags, rc):
            if rc == 0:
                print("Connect Success Return Code {}".format(rc))
            else:
                print("Failed Connect, Return Code {}".format(rc))

        def on_connect_fail(client, userdata):
            print("Failed Connect")

        client = mqtt_client.Client(CLIENT_ID)

        client.on_connect = on_connect
        client.on_connect_fail = on_connect_fail
        client.connect(SERVER_IP, PORT)

        self.subscribe(client)
        return client

    def run(self):
        client = self.set_connect()

        self.send_msg(client)
        client.loop_forever()

    def send_msg(self, client):
        for i in range(10):
            msg = "第{}次打你丫的".format(i)
            res = client.publish(TOPIC, msg)
            if res[0] == 0:
                print('SEND {} TO {} OVER'.format(msg, TOPIC))

    def subscribe(self, client):
        def on_message(client, userdata, msg):
            print(f"RECEIVED `{msg.payload.decode()}` FROM `{msg.topic}` TOPIC")

        client.subscribe(TOPIC)
        client.on_message = on_message


if __name__ == '__main__':
    MqttAction().run()

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/AllinToyou/article/detail/188536
推荐阅读
相关标签
  

闽ICP备14008679号