赞
踩
- #要安装最新版本的MQTT服务器,需配置 Mosquitto 源
- apt-add-repository ppa:mosquitto-dev/mosquitto-ppa
- apt-get update
- #安装
- apt install mosquitto
- #安装 mosquitto-clients 才会有 mosquitto_sub 和 mosquitto_pub 命令
- apt install mosquitto mosquitto-clients
- #安装 MQTT 开发库
- apt install mosquitto-dev
-
- #启动
- mosquitto -v -d -c /etc/mosquitto/mosquitto.conf
- #启动
- osquitto -v
- #启动
- mosquitto -v -d -c /etc/mosquitto/mosquitto.conf
-
- #重启
- /etc/init.d/mosquitto restart
订阅一个消息:
>>mosquitto_sub -t 'myopic' -v -d
发布一个消息:
>>mosquitto_pub -t 'mytopic' -m 'hello world' -d
1、创建用户
- #方式1
- #这里是创建一个新的 mqtt 用户 scott,接下来就会问密码与确认密码,目前是设置为 123456
- mosquitto_passwd -c /etc/mosquitto/passwd scott
-
- #方式2
- #这里是创建一个新的 mqtt 用户 zhangsan ,设置为 123456
- mosquitto_passwd -b /etc/mosquitto/passwd zhangsan 123456
2、修改配置文件
- #添加配置,禁止匿名用户以及指定密码文件
- vim /etc/mosquitto/conf.d/default.conf
allow_anonymous false
password_file /etc/mosquitto/passwd
3、重启 MQTT 服务
/etc/init.d/mosquitto restart
发布
- mosquitto_pub -t 'mytopic' -m 'hello world2' -d -u scott -P 123456 -q 2
- Client null sending CONNECT
- Client null received CONNACK (0)
- Client null sending PUBLISH (d0, q2, r0, m1, 'mytopic', ... (12 bytes))
- Client null received PUBREC (Mid: 1)
- Client null sending PUBREL (m1)
- Client null received PUBCOMP (Mid: 1, RC:0)
- Client null sending DISCONNECT
订阅
- /etc/mosquitto# mosquitto_sub -t 'mytopic' -v -d -u scott -P 123456 -q 2
- Client null sending CONNECT
- Client null received CONNACK (0)
- Client null sending SUBSCRIBE (Mid: 1, Topic: mytopic, QoS: 2, Options: 0x00)
- Client null received SUBACK
- Subscribed (mid: 1): 2
- Client null received PUBLISH (d0, q2, r0, m1, 'mytopic', ... (12 bytes))
- Client null sending PUBREC (m1, rc0)
- Client null received PUBREL (Mid: 1)
- Client null sending PUBCOMP (m1)
- mytopic hello world2
python代码
首先,先修改配置文件,增加ip和端口号,否则只能用127.0.0.1.
bind_address 192.168.31.129
port 1883
然后重启。
python代码如下
publish.py
- import random
- import time
-
- from paho.mqtt import client as mqtt_client
-
-
- broker = '192.168.31.129'
- port = 1883
- topic = "mytopic"
- user = "scott"
- password = "123456"
- # generate client ID with pub prefix randomly
- client_id = f'python-mqtt-{random.randint(0, 1000)}'
-
- def connect_mqtt():
- def on_connect(client, userdata, flags, rc):
- if rc == 0:
- print("Connected to MQTT Broker!")
- else:
- print("Failed to connect, return code %d\n", rc)
-
- client = mqtt_client.Client(client_id, transport='tcp')
- client.username_pw_set(user, password=password)
- client.on_connect = on_connect
- client.connect(broker, port)
- return client
-
-
- def publish(client):
- msg_count = 0
- while True:
- time.sleep(1)
- msg = f"messages: {msg_count}"
- result = client.publish(topic, msg,qos=2)
- # result: [0, 1]
- status = result[0]
- if status == 0:
- print(f"Send `{msg}` to topic `{topic}`")
- else:
- print(f"Failed to send message to topic {topic}")
- msg_count += 1
-
-
- def run():
- client = connect_mqtt()
-
- client.loop_start()
- publish(client)
-
-
- if __name__ == '__main__':
- run()
subscribe.py
- import random
-
- from paho.mqtt import client as mqtt_client
-
-
- broker = '192.168.31.129'
- port = 1883
- user = "scott"
- password = "123456"
- topic = "mytopic"
- # generate client ID with pub prefix randomly
- client_id = f'python-mqtt-{random.randint(0, 100)}'
-
-
- def connect_mqtt() -> mqtt_client:
- def on_connect(client, userdata, flags, rc):
- if rc == 0:
- print("Connected to MQTT Broker!")
- else:
- print("Failed to connect, return code %d\n", rc)
-
- client = mqtt_client.Client(client_id, transport='tcp')
- client.username_pw_set(user, password=password)
- client.on_connect = on_connect
- client.connect(broker, port)
- return client
-
-
- def subscribe(client: mqtt_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
-
-
- def run():
- client = connect_mqtt()
-
- subscribe(client)
- client.loop_forever()
-
-
- if __name__ == '__main__':
- run()
运行效果
命令订阅
命令发布
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。