mqtt publish and subscribe with Python

Assume you have read some introduction papers about MQTT(like here: https://pypi.org/project/paho-mqtt/)

I write 2 Python scripts with python2.7: mqtt_talk.py as publisher, mqtt_listen.py as subscriber, here are the details

mqtt_talker.py

#!/usr/bin/env python

import os.path
import paho.mqtt.client as mqtt

class mqtt_talker():
    def __init__(self, broker_ip):
        self.ip = broker_ip
        self.client = mqtt.Client(client_id = "test_mqtt_talker")
        self.client.on_connect = self.on_connect
        self.client.on_disconnect = self.on_disconnect
        self.client.on_publish = self.on_publish

    def connect(self):
        self.client.connect(self.ip, 1883, 60)

    def on_connect(self, client, userdata, flags, rc):
        self.connected = True
        print("Connected with broker ......")

    def disconnect(self):
        self.client.disconnect()

    def on_disconnect(self, client, userdata, rc):
        print("Disconnected with broker ......")

    def publish(self):
        data = "Hello Lily"
        self.client.publish("mqtt_talker", data, 2)

    def on_publish(self, client, userdata, mid):
        print("Data publish success ...")

    def loop_forever(self):
        self.client.loop_forever()

if __name__ == '__main__':
    broker_IP = "192.168.1.2"   # change broker ip same with mqtt server
    client = mqtt_talker(broker_IP)
    client.connect()
    client.publish()
    #client.disconnect()
    client.loop_forever()

mqtt_listen.py

#!/usr/bin/env python

import os.path
import paho.mqtt.client as mqtt

class mqtt_listener():
    def __init__(self, broker_ip):
        self.ip = broker_ip
        self.client = mqtt.Client(client_id = "test_mqtt_listener")
        self.client.on_connect = self.on_connect
        self.client.on_disconnect = self.on_disconnect
        self.client.on_message = self.on_message

    def connect(self):
        self.client.connect(self.ip, 1883, 60)

    def on_connect(self, client, userdata, flags, rx):
        print("Connected with broker ......")
        self.subscribe("mqtt_talker")
        return True

    def disconnect(self):
        self.client.disconnect()

    def on_disconnect(self, client, userdata, rc):
        print("Disconnected with broker ......")

    def subscribe(self, topic):
        self.client.subscribe(topic)

    def on_message(self, client, userdata, msg):
        print("topic:{} payload: {}".format(msg.topic, msg.payload))

    def loop_forever(self):
        self.client.loop_forever()

if __name__ == '__main__':
    broker_IP = "192.168.1.2"   # change broker ip same with mqtt server
    client = mqtt_listener(broker_IP)
    client.connect()
    client.subscribe("mqtt_talker")
    client.disconnect()
    client.loop_forever()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值