赞
踩
目录
1.下载安装emqx开源框架
这个是windows版本的,我用的4.4版本
2.下载后,解压到你想要的文件夹就行
3.把bin目录加入环境变量,很多人都不用加,但是我不加不能用
4.用管理员权限进入bin目录
5.安装emqx
emqx install
安装成功会报成功
6.开始执行
emqx start
4.4版本执行不会报成功
7.进入后台
http://localhost:18083/
默认用户名和密码:admin public进去就能改
这就好了,可以编写python程序去运行了
8.如果要停止服务
emqx stop
如果要卸载服务,就先停止服务,在删除根目录所有内容即可。
- # # python 3.8
- #
- import time
- from paho.mqtt import client as mqtt
- # client在可以发布和订阅消息之前必须要先连接到broker代理
- # 连接由client发起
- # broke地址
- broker = "ip"
- # 端口
- port = 1883
-
- # 消息所属话题, 消息的类型,Subscribe后,就会收到payload
- # topic = '$oc/test'
- # 回调客户机实例
- client_id = f'user'
-
- # 发布端
- # 连接回调函数
-
- 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)
-
- # 断开链接回调
- def on_disconnect(client, userdata, rc):
- print("Unexpected disconnection rc = " + str(rc))
- pass
-
- def on_message(client, userdata, msg):
- print(msg.topic + " " + str(msg.payload))
-
- # 订阅回调
- def on_subscribe(client, userdata, mid, granted_qos):
- print("On Subscribed: qos = %d" % granted_qos)
- pass
-
-
- # 取消订阅回调
- def on_unsubscribe(client, userdata, mid, granted_qos):
- print("On unSubscribed: qos = %d" % granted_qos)
- pass
-
-
- # 发布消息回调
- def on_publish(client, userdata, mid):
- print("On onPublish: qos = %d" % mid)
- pass
-
- # 发布消息回调
- def publish(client):
- msg_count = 0
- while True:
- time.sleep(1)
- result = client.publish(topic, str(a), qos=0)
- status = result[0]
- if status == 0:
- print(f"Send `{a}` to topic `{topic}`")
- else:
- print(f"Failed to send message to topic {topic}")
- msg_count += 1
-
-
- def run():
- # 初始化客户机
- client = mqtt.Client()
- # 调用连接回调函数
- client.on_connect = on_connect
- client.on_message = on_message
- client.on_publish = on_publish
- client.on_disconnect = on_disconnect
- client.on_unsubscribe = on_unsubscribe
- client.on_subscribe = on_subscribe
- # 连接
- client.connect(broker, port)
- # 启用进程保持连接
- client.loop_start()
- # client.username_pw_set(username, password=None)
- # 调用完loop之后再调用publish
- publish(client)
-
-
- if __name__ == '__main__':
- run()
发布端连接代理,就是我们刚才整的emqx,topic是订阅的消息,订阅端只有订阅了该消息头才能接收到这一类的消息
-
- broker = "ip"
- port = 1883
- # topic = '$oc/devices/{CN}/user/rsu/info/up'
- topic = '$oc/ccc'
- # generate client ID with pub prefix randomly
- client_id = f'user_client'
-
- # 断开链接回调
- def on_disconnect(client, userdata, rc):
- print("Unexpected disconnection rc = " + str(rc))
- pass
-
- 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)
-
-
- # 订阅回调
- def on_subscribe(client, userdata, mid, granted_qos):
- print("On Subscribed: qos = %d" % granted_qos)
- pass
-
-
- # 取消订阅回调
- def on_unsubscribe(client, userdata, mid):
- print("On unSubscribed: qos = %d" % mid)
- pass
-
- def on_message(client, userdata, msg):
- try:
- print(msg.topic)
- print(msg.payload.decode('utf-8'))
- # print(f"Received `{msg.payload.decode()}` from `{msg.topic}` topic")
- except:
- pass
- pass
-
-
-
- def run():
- client = mqtt_client.Client(client_id)
- client.on_connect = on_connect
- client.on_unsubscribe = on_unsubscribe
- client.on_subscribe = on_subscribe
- client.connect(broker, port)
- client.subscribe(topic, qos=0)
- client.on_message = on_message # 返回订阅消息的回调函数
- client.loop_forever()
-
-
- if __name__ == '__main__':
- run()
理论上就可以使用了,但是经过实际测试,只有在本地上使用的时候是正确的,如果我把订阅端放到另一个电脑上,双方电脑能ping通,但是在订阅端会报socket超时错误。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。