]> git.michaelhowe.org Git - packages/p/paho-mqtt.git/commitdiff
Add example of using the client in a class.
authorRoger Light <roger@atchoo.org>
Mon, 24 Jun 2013 20:10:03 +0000 (21:10 +0100)
committerRoger Light <roger@atchoo.org>
Mon, 3 Feb 2014 21:17:08 +0000 (21:17 +0000)
examples/sub-class.py [new file with mode: 0755]
examples/sub.py

diff --git a/examples/sub-class.py b/examples/sub-class.py
new file mode 100755 (executable)
index 0000000..20e6fe4
--- /dev/null
@@ -0,0 +1,72 @@
+#!/usr/bin/python
+
+# Copyright (c) 2013 Roger Light <roger@atchoo.org>
+#
+# All rights reserved. This program and the accompanying materials
+# are made available under the terms of the Eclipse Distribution License v1.0
+# which accompanies this distribution. 
+#
+# The Eclipse Distribution License is available at 
+#   http://www.eclipse.org/org/documents/edl-v10.php.
+#
+# Contributors:
+#    Roger Light - initial implementation
+
+# This example shows how you can use the MQTT client in a class.
+
+import sys
+try:
+    import paho.mqtt.client as mqtt
+except ImportError:
+    # This part is only required to run the example from within the examples
+    # directory when the module itself is not installed.
+    #
+    # If you have the module installed, just use "import paho.mqtt.client"
+    import os
+    import inspect
+    cmd_subfolder = os.path.realpath(os.path.abspath(os.path.join(os.path.split(inspect.getfile( inspect.currentframe() ))[0],"../src")))
+    if cmd_subfolder not in sys.path:
+        sys.path.insert(0, cmd_subfolder)
+    import paho.mqtt.client as mqtt
+
+class MyMQTTClass:
+    def __init__(self, clientid=None):
+        self._mqttc = mqtt.Client(clientid)
+        self._mqttc.on_message = self.mqtt_on_message
+        self._mqttc.on_connect = self.mqtt_on_connect
+        self._mqttc.on_publish = self.mqtt_on_publish
+        self._mqttc.on_subscribe = self.mqtt_on_subscribe
+
+    def mqtt_on_connect(self, mqttc, obj, rc):
+        print("rc: "+str(rc))
+
+    def mqtt_on_message(self, mqttc, obj, msg):
+        print(msg.topic+" "+str(msg.qos)+" "+str(msg.payload))
+
+    def mqtt_on_publish(self, mqttc, obj, mid):
+        print("mid: "+str(mid))
+
+    def mqtt_on_subscribe(self, mqttc, obj, mid, granted_qos):
+        print("Subscribed: "+str(mid)+" "+str(granted_qos))
+
+    def mqtt_on_log(self, mqttc, obj, level, string):
+        print(string)
+
+    def run(self):
+        self._mqttc.connect("m2m.eclipse.org", 1883, 60)
+        self._mqttc.subscribe("$SYS/#", 0)
+
+        rc = 0
+        while rc == 0:
+            rc = self._mqttc.loop()
+        return rc
+
+
+# If you want to use a specific client id, use
+# mqttc = MyMQTTClass("client-id")
+# but note that the client id must be unique on the broker. Leaving the client
+# id parameter empty will generate a random id for you.
+mqttc = MyMQTTClass()
+rc = mqttc.run()
+
+print("rc: "+str(rc))
index 55fcebd4d8f121f9877d51af8ccc8ab491765e45..81900d5c9b549811874ecfbe36e90ce07824e1c5 100755 (executable)
 # Copyright (c) 2010,2011 Roger Light <roger@atchoo.org>
 # All rights reserved.
 
+# This shows a simple example of an MQTT subscriber.
+
 import sys
 try:
     import paho.mqtt.client as mqtt
 except ImportError:
     # This part is only required to run the example from within the examples
     # directory when the module itself is not installed.
+    #
+    # If you have the module installed, just use "import paho.mqtt.client"
     import os
     import inspect
     cmd_subfolder = os.path.realpath(os.path.abspath(os.path.join(os.path.split(inspect.getfile( inspect.currentframe() ))[0],"../src")))