当前位置:   article > 正文

python+emqx实现mqtt协议传输数据_msg.payload.decode()

msg.payload.decode()

目录

安装

发布端

订阅端

存在问题


安装

1.下载安装emqx开源框架

下载 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

如果要卸载服务,就先停止服务,在删除根目录所有内容即可。

发布端

  1. # # python 3.8
  2. #
  3. import time
  4. from paho.mqtt import client as mqtt
  5. # client在可以发布和订阅消息之前必须要先连接到broker代理
  6. # 连接由client发起
  7. # broke地址
  8. broker = "ip"
  9. # 端口
  10. port = 1883
  11. # 消息所属话题, 消息的类型,Subscribe后,就会收到payload
  12. # topic = '$oc/test'
  13. # 回调客户机实例
  14. client_id = f'user'
  15. # 发布端
  16. # 连接回调函数
  17. def on_connect(client, userdata, flags, rc):
  18. if rc == 0:
  19. print("Connected to MQTT Broker!")
  20. else:
  21. print("Failed to connect, return code %d\n", rc)
  22. # 断开链接回调
  23. def on_disconnect(client, userdata, rc):
  24. print("Unexpected disconnection rc = " + str(rc))
  25. pass
  26. def on_message(client, userdata, msg):
  27. print(msg.topic + " " + str(msg.payload))
  28. # 订阅回调
  29. def on_subscribe(client, userdata, mid, granted_qos):
  30. print("On Subscribed: qos = %d" % granted_qos)
  31. pass
  32. # 取消订阅回调
  33. def on_unsubscribe(client, userdata, mid, granted_qos):
  34. print("On unSubscribed: qos = %d" % granted_qos)
  35. pass
  36. # 发布消息回调
  37. def on_publish(client, userdata, mid):
  38. print("On onPublish: qos = %d" % mid)
  39. pass
  40. # 发布消息回调
  41. def publish(client):
  42. msg_count = 0
  43. while True:
  44. time.sleep(1)
  45. result = client.publish(topic, str(a), qos=0)
  46. status = result[0]
  47. if status == 0:
  48. print(f"Send `{a}` to topic `{topic}`")
  49. else:
  50. print(f"Failed to send message to topic {topic}")
  51. msg_count += 1
  52. def run():
  53. # 初始化客户机
  54. client = mqtt.Client()
  55. # 调用连接回调函数
  56. client.on_connect = on_connect
  57. client.on_message = on_message
  58. client.on_publish = on_publish
  59. client.on_disconnect = on_disconnect
  60. client.on_unsubscribe = on_unsubscribe
  61. client.on_subscribe = on_subscribe
  62. # 连接
  63. client.connect(broker, port)
  64. # 启用进程保持连接
  65. client.loop_start()
  66. # client.username_pw_set(username, password=None)
  67. # 调用完loop之后再调用publish
  68. publish(client)
  69. if __name__ == '__main__':
  70. run()

发布端连接代理,就是我们刚才整的emqx,topic是订阅的消息,订阅端只有订阅了该消息头才能接收到这一类的消息

订阅端

  1. broker = "ip"
  2. port = 1883
  3. # topic = '$oc/devices/{CN}/user/rsu/info/up'
  4. topic = '$oc/ccc'
  5. # generate client ID with pub prefix randomly
  6. client_id = f'user_client'
  7. # 断开链接回调
  8. def on_disconnect(client, userdata, rc):
  9. print("Unexpected disconnection rc = " + str(rc))
  10. pass
  11. def on_connect(client, userdata, flags, rc):
  12. if rc == 0:
  13. print("Connected to MQTT Broker!")
  14. else:
  15. print("Failed to connect, return code %d\n", rc)
  16. # 订阅回调
  17. def on_subscribe(client, userdata, mid, granted_qos):
  18. print("On Subscribed: qos = %d" % granted_qos)
  19. pass
  20. # 取消订阅回调
  21. def on_unsubscribe(client, userdata, mid):
  22. print("On unSubscribed: qos = %d" % mid)
  23. pass
  24. def on_message(client, userdata, msg):
  25. try:
  26. print(msg.topic)
  27. print(msg.payload.decode('utf-8'))
  28. # print(f"Received `{msg.payload.decode()}` from `{msg.topic}` topic")
  29. except:
  30. pass
  31. pass
  32. def run():
  33. client = mqtt_client.Client(client_id)
  34. client.on_connect = on_connect
  35. client.on_unsubscribe = on_unsubscribe
  36. client.on_subscribe = on_subscribe
  37. client.connect(broker, port)
  38. client.subscribe(topic, qos=0)
  39. client.on_message = on_message # 返回订阅消息的回调函数
  40. client.loop_forever()
  41. if __name__ == '__main__':
  42. run()

存在问题

理论上就可以使用了,但是经过实际测试,只有在本地上使用的时候是正确的,如果我把订阅端放到另一个电脑上,双方电脑能ping通,但是在订阅端会报socket超时错误。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/花生_TL007/article/detail/188545
推荐阅读
相关标签
  

闽ICP备14008679号