mqtt2.py 632 Bytes
Newer Older
Spencer Du's avatar
Spencer Du committed
1 2
import logging
import paho.mqtt.client as mqtt
Spencer Du's avatar
Spencer Du committed
3
import json
Spencer Du's avatar
Spencer Du committed
4

Spencer Du's avatar
Spencer Du committed
5
class embedded(mqtt.Client):
Spencer Du's avatar
Spencer Du committed
6 7
    def on_connect(self, mqtt, obj, flags, rc):
        pass
Spencer Du's avatar
Spencer Du committed
8

Spencer Du's avatar
Spencer Du committed
9 10
    def on_message(self, mqtt, userdata, message):
        m_decode = str(message.payload.decode("utf-8"))
Spencer Du's avatar
Spencer Du committed
11
        print("\n" + "message recieved= " + m_decode)
Spencer Du's avatar
Spencer Du committed
12 13 14 15 16
        # print("File which you want to import(with .py extension)")
        print("message topic=", message.topic)
        print("message qos=", message.qos)
        print("message retain flag=", message.retain)
        m_in = json.loads(m_decode)
Spencer Du's avatar
Spencer Du committed
17

Spencer Du's avatar
Spencer Du committed
18
    def run(self):
Spencer Du's avatar
Spencer Du committed
19
        self.connect("broker.hivemq.com", 1883, 60)