提交 33b7c95d 编写于 作者: J James Myatt 提交者: Roger Light

Update examples added since PR#98 (#109)

Signed-off-by: NJames Myatt <james@jamesmyatt.co.uk>
上级 79331252
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Copyright (c) 2016 James Myatt <james@jamesmyatt.co.uk>
#
# 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.
# which accompanies this distribution.
#
# The Eclipse Distribution License is available at
# The Eclipse Distribution License is available at
# http://www.eclipse.org/org/documents/edl-v10.php.
#
# Contributors:
# James Myatt - initial implementation
# This shows a simple example of standard logging with an MQTT subscriber.
# This shows a simple example of standard logging with an MQTT subscriber client.
import sys
import logging
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
import context # Ensures paho is in PYTHONPATH
import paho.mqtt.client as mqtt
import logging
logging.basicConfig(level=logging.DEBUG)
# If you want to use a specific client id, use
......
......@@ -15,40 +15,33 @@
# Copyright (c) 2010,2011 Roger Light <roger@atchoo.org>
# All rights reserved.
# This shows a simple example of an MQTT subscriber.
import sys
import time
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
# This shows a simple example of waiting for a message to be published.
import context # Ensures paho is in PYTHONPATH
import paho.mqtt.client as mqtt
def on_connect(mqttc, obj, flags, rc):
print("rc: "+str(rc))
print("rc: " + str(rc))
def on_message(mqttc, obj, msg):
print(msg.topic+" "+str(msg.qos)+" "+str(msg.payload))
print(msg.topic + " " + str(msg.qos) + " " + str(msg.payload))
def on_publish(mqttc, obj, mid):
print("mid: "+str(mid))
print("mid: " + str(mid))
pass
def on_subscribe(mqttc, obj, mid, granted_qos):
print("Subscribed: "+str(mid)+" "+str(granted_qos))
print("Subscribed: " + str(mid) + " " + str(granted_qos))
def on_log(mqttc, obj, level, string):
print(string)
# If you want to use a specific client id, use
# mqttc = mqtt.Client("client-id")
# but note that the client id must be unique on the broker. Leaving the client
......@@ -59,7 +52,7 @@ mqttc.on_connect = on_connect
mqttc.on_publish = on_publish
mqttc.on_subscribe = on_subscribe
# Uncomment to enable debug messages
#mqttc.on_log = on_log
# mqttc.on_log = on_log
mqttc.connect("localhost", 1883, 60)
mqttc.loop_start()
......
......@@ -17,20 +17,9 @@
# This demonstrates the session present flag when connecting.
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
import context # Ensures paho is in PYTHONPATH
import paho.mqtt.client as mqtt
def on_connect(mqttc, obj, flags, rc):
if obj == 0:
......@@ -39,23 +28,26 @@ def on_connect(mqttc, obj, flags, rc):
print("Second connection:")
elif obj == 2:
print("Third connection (with clean session=True):")
print(" Session present: "+str(flags['session present']))
print(" Connection result: "+str(rc))
print(" Session present: " + str(flags['session present']))
print(" Connection result: " + str(rc))
mqttc.disconnect()
def on_disconnect(mqttc, obj, rc):
mqttc.user_data_set(obj+1)
mqttc.user_data_set(obj + 1)
if obj == 0:
mqttc.reconnect()
def on_log(mqttc, obj, level, string):
print(string)
mqttc = mqtt.Client(client_id="asdfj", clean_session=False)
mqttc.on_connect = on_connect
mqttc.on_disconnect = on_disconnect
# Uncomment to enable debug messages
#mqttc.on_log = on_log
# mqttc.on_log = on_log
mqttc.user_data_set(0)
mqttc.connect("test.mosquitto.org", 1883, 60)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册