import paho.mqtt.client as mqtt
# The callback for when the client receives a CONNACK response from the server.
- def on_connect(client, userdata, rc):
+ def on_connect(client, userdata, flags, rc):
print("Connected with result code "+str(rc))
# Subscribing in on_connect() means that if we lose the connection and
::
- on_connect(client, userdata, rc)
+ on_connect(client, userdata, flags, rc)
Called when the broker responds to our connection request.
userdata
the private user data as set in ``Client()`` or ``userdata_set()``
+flags
+ response flags sent by the broker
rc
the connection result
+
+flags is a dict that contains response flags from the broker:
+ flags['session present'] - this flag is useful for clients that are
+ using clean session set to 0 only. If a client with clean
+ session=0, that reconnects to a broker that it has previously
+ connected to, this flag indicates whether the broker still has the
+ session information for the client. If 1, the session still exists.
+
The value of rc indicates success or not:
0: Connection successful
::
- def on_connect(client, userdata, rc):
+ def on_connect(client, userdata, flags, rc):
print("Connection returned result: "+connack_string(rc))
mqttc.on_connect = on_connect
final_mid = 0
-def on_connect(mqttc, userdata, rc):
+def on_connect(mqttc, userdata, flags, rc):
if userdata == True:
print("rc: "+str(rc))
--- /dev/null
+#!/usr/bin/python
+
+# Copyright (c) 2014 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
+# Copyright (c) 2014 Roger Light <roger@atchoo.org>
+# All rights reserved.
+
+# 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
+
+def on_connect(mqttc, obj, flags, rc):
+ if obj == 0:
+ print("First connection:")
+ elif obj == 1:
+ 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))
+ mqttc.disconnect()
+
+def on_disconnect(mqttc, obj, rc):
+ 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.user_data_set(0)
+mqttc.connect("test.mosquitto.org", 1883, 60)
+
+mqttc.loop_forever()
+
+# Clear session
+mqttc = mqtt.Client(client_id="asdfj", clean_session=True)
+mqttc.on_connect = on_connect
+mqttc.user_data_set(2)
+mqttc.connect("test.mosquitto.org", 1883, 60)
+mqttc.loop_forever()
self._mqttc.on_publish = self.mqtt_on_publish
self._mqttc.on_subscribe = self.mqtt_on_subscribe
- def mqtt_on_connect(self, mqttc, obj, rc):
+ def mqtt_on_connect(self, mqttc, obj, flags, rc):
print("rc: "+str(rc))
def mqtt_on_message(self, mqttc, obj, msg):
sys.path.insert(0, cmd_subfolder)
import paho.mqtt.client as mqtt
-def on_connect(mqttc, obj, rc):
+def on_connect(mqttc, obj, flags, rc):
print "Connected to %s:%s" % (mqttc._host, mqttc._port)
def on_message(mqttc, obj, msg):
sys.path.insert(0, cmd_subfolder)
import paho.mqtt.client as mqtt
-def on_connect(mqttc, obj, rc):
+def on_connect(mqttc, obj, flags, rc):
print("rc: "+str(rc))
def on_message(mqttc, obj, msg):
broker. To use a callback, define a function and then assign it to the
client:
- def on_connect(client, userdata, rc):
+ def on_connect(client, userdata, flags, rc):
print("Connection returned " + str(rc))
client.on_connect = on_connect
The callbacks:
- on_connect(client, userdata, rc): called when the broker responds to our connection
- request. The value of rc determines success or not:
- 0: Connection successful
- 1: Connection refused - incorrect protocol version
- 2: Connection refused - invalid client identifier
- 3: Connection refused - server unavailable
- 4: Connection refused - bad username or password
- 5: Connection refused - not authorised
- 6-255: Currently unused.
+ on_connect(client, userdata, flags, rc): called when the broker responds to our connection
+ request.
+ flags is a dict that contains response flags from the broker:
+ flags['session present'] - this flag is useful for clients that are
+ using clean session set to 0 only. If a client with clean
+ session=0, that reconnects to a broker that it has previously
+ connected to, this flag indicates whether the broker still has the
+ session information for the client. If 1, the session still exists.
+ The value of rc determines success or not:
+ 0: Connection successful
+ 1: Connection refused - incorrect protocol version
+ 2: Connection refused - invalid client identifier
+ 3: Connection refused - server unavailable
+ 4: Connection refused - bad username or password
+ 5: Connection refused - not authorised
+ 6-255: Currently unused.
on_disconnect(client, userdata, rc): called when the client disconnects from the broker.
The rc parameter indicates the disconnection state. If MQTT_ERR_SUCCESS
if len(self._in_packet['packet']) != 2:
return MQTT_ERR_PROTOCOL
- (resvd, result) = struct.unpack("!BB", self._in_packet['packet'])
+ (flags, result) = struct.unpack("!BB", self._in_packet['packet'])
if result == CONNACK_REFUSED_PROTOCOL_VERSION and self._protocol == MQTTv311:
- self._easy_log(MQTT_LOG_DEBUG, "Received CONNACK ("+str(resvd)+", "+str(result)+"), attempting downgrade to MQTT v3.1.")
+ self._easy_log(MQTT_LOG_DEBUG, "Received CONNACK ("+str(flags)+", "+str(result)+"), attempting downgrade to MQTT v3.1.")
# Downgrade to MQTT v3.1
self._protocol = MQTTv31
return self.reconnect()
- self._easy_log(MQTT_LOG_DEBUG, "Received CONNACK ("+str(resvd)+", "+str(result)+")")
+ if result == 0:
+ self._state = mqtt_cs_connected
+
+ self._easy_log(MQTT_LOG_DEBUG, "Received CONNACK ("+str(flags)+", "+str(result)+")")
self._callback_mutex.acquire()
if self.on_connect:
self._in_callback = True
- self.on_connect(self, self._userdata, result)
+
+ if sys.version_info[0] < 3:
+ argcount = self.on_connect.func_code.co_argcount
+ else:
+ argcount = self.on_connect.__code__.co_argcount
+
+ if argcount == 3:
+ self.on_connect(self, self._userdata, result)
+ else:
+ flags_dict = dict()
+ flags_dict['session present'] = flags & 0x01
+ self.on_connect(self, self._userdata, flags_dict, result)
self._in_callback = False
self._callback_mutex.release()
if result == 0:
- self._state = mqtt_cs_connected
return MQTT_ERR_SUCCESS
elif result > 0 and result < 6:
return MQTT_ERR_CONN_REFUSED
c.publish(topic, payload, qos, retain)
-def _on_connect(c, userdata, rc):
+def _on_connect(c, userdata, flags, rc):
"""Internal callback"""
_do_publish(c)
import paho.mqtt.client as mqtt
-def on_connect(mqttc, obj, rc):
+def on_connect(mqttc, obj, flags, rc):
if rc != 0:
exit(rc)
else:
import paho.mqtt.client as mqtt
-def on_connect(mqttc, obj, rc):
+def on_connect(mqttc, obj, flags, rc):
if rc != 0:
exit(rc)
import paho.mqtt.client as mqtt
-def on_connect(mqttc, obj, rc):
+def on_connect(mqttc, obj, flags, rc):
if rc != 0:
exit(rc)
else:
import paho.mqtt.client as mqtt
-def on_connect(mqttc, obj, rc):
+def on_connect(mqttc, obj, flags, rc):
if rc != 0:
exit(rc)
else:
import paho.mqtt.client as mqtt
-def on_connect(mqttc, obj, rc):
+def on_connect(mqttc, obj, flags, rc):
if rc != 0:
exit(rc)
else:
import paho.mqtt.client as mqtt
-def on_connect(mqttc, obj, rc):
+def on_connect(mqttc, obj, flags, rc):
if rc != 0:
exit(rc)
else:
exit(1)
exit(0)
-def on_connect(mqttc, obj, rc):
+def on_connect(mqttc, obj, flags, rc):
if rc != 0:
print("Connect failed ("+str(rc)+")")
exit(rc)
run = 0
-def on_connect(mqttc, obj, rc):
+def on_connect(mqttc, obj, flags, rc):
if rc != 0:
exit(rc)
sent_mid = -1
-def on_connect(mqttc, obj, rc):
+def on_connect(mqttc, obj, flags, rc):
global sent_mid
if rc != 0:
exit(rc)
sent_mid = -1
-def on_connect(mqttc, obj, rc):
+def on_connect(mqttc, obj, flags, rc):
global sent_mid
if rc != 0:
exit(rc)
first_connection = 1
-def on_connect(mqttc, obj, rc):
+def on_connect(mqttc, obj, flags, rc):
global first_connection
if rc != 0:
exit(rc)
import paho.mqtt.client as mqtt
-def on_connect(mqttc, obj, rc):
+def on_connect(mqttc, obj, flags, rc):
if rc != 0:
exit(rc)
else:
sent_mid = -1
-def on_connect(mqttc, obj, rc):
+def on_connect(mqttc, obj, flags, rc):
global sent_mid
if rc != 0:
exit(rc)
sent_mid = -1
-def on_connect(mqttc, obj, rc):
+def on_connect(mqttc, obj, flags, rc):
global sent_mid
if rc != 0:
exit(rc)
import paho.mqtt.client as mqtt
-def on_connect(mqttc, obj, rc):
+def on_connect(mqttc, obj, flags, rc):
if rc != 0:
exit(rc)
else:
print("WARNING: SSL/TLS not supported on Python 2.6")
exit(0)
-def on_connect(mqttc, obj, rc):
+def on_connect(mqttc, obj, flags, rc):
if rc != 0:
exit(rc)
else:
print("WARNING: SSL/TLS not supported on Python 2.6")
exit(0)
-def on_connect(mqttc, obj, rc):
+def on_connect(mqttc, obj, flags, rc):
if rc != 0:
exit(rc)
else:
print("WARNING: SSL/TLS not supported on Python 2.6")
exit(0)
-def on_connect(mqttc, obj, rc):
+def on_connect(mqttc, obj, flags, rc):
exit(1)
mqttc = mqtt.Client("08-ssl-fake-cacert")