Projekt

Obecné

Profil

« Předchozí | Další » 

Revize 92a4128a

Přidáno uživatelem Martin Forejt před téměř 4 roky(ů)

Re #8489 - (basic) MQTTSubscriber

Zobrazit rozdíly:

aswi2021vochomurka/service/subscriber.py
1
import paho.mqtt.client as mqtt
2

  
3

  
4
class MQTTSubscriber:
5

  
6
    # The callback for when the client receives a CONNACK response from the server.
7
    def on_connect(self, client, userdata, flags, rc, properties=None):
8
        print("Connected with result code " + str(rc))
9
        # Subscribing in on_connect() means that if we lose the connection and
10
        # reconnect then subscriptions will be renewed.
11
        client.subscribe("#")
12

  
13
    # The callback for when a PUBLISH message is received from the server.
14
    def on_message(self, client, userdata, message):
15
        print(message.topic + " " + str(message.payload.decode("utf-8")))
16

  
17
    def start(self):
18
        client = mqtt.Client()
19
        client.on_connect = self.on_connect
20
        client.on_message = self.on_message
21

  
22
        client.connect("localhost", 1883, 60)
23

  
24
        # Blocking call that processes network traffic, dispatches callbacks and
25
        # handles reconnecting.
26
        # Other loop*() functions are available that give a threaded interface and a
27
        # manual interface.
28
        client.loop_forever()

Také k dispozici: Unified diff