1 import paho_mqtt3a
as mqttv3, time, random
7 clientid =
"myclientid" 11 print "deliveryComplete", msgid
14 print "connectionLost", cause
16 responseOptions = {
"context": context,
"onSuccess": onSuccess,
"onFailure" : onFailure}
17 print "rc from publish while disconnected is", mqttv3.send(client, topic,
"message while disconnected", 2, 0, responseOptions)
20 print "messageArrived", message
26 print "connected", cause
29 print "onSuccess for", context[
"clientid"], context[
"state"], successData
30 responseOptions = {
"context": context,
"onSuccess": onSuccess,
"onFailure" : onFailure}
31 if context[
"state"] ==
"connecting":
32 context[
"state"] =
"subscribing" 33 print "rc from subscribe is", mqttv3.subscribe(client, topic, 2, responseOptions)
34 elif context[
"state"] ==
"subscribing":
35 context[
"state"] =
"publishing qos 0" 36 print "rc from publish is", mqttv3.send(client, topic,
"a QoS 0 message", 0, 0, responseOptions)
37 elif context[
"state"] ==
"publishing qos 0":
38 context[
"state"] =
"publishing qos 1" 39 print "rc from publish is", mqttv3.send(client, topic,
"a QoS 1 message", 1, 0, responseOptions)
40 elif context[
"state"] ==
"publishing qos 1":
41 context[
"state"] =
"publishing qos 2" 42 print "rc from publish is", mqttv3.send(client, topic,
"a QoS 2 message", 2, 0, responseOptions)
43 elif context[
"state"] ==
"publishing qos 2":
44 context[
"state"] =
"finished" 45 print "leaving onSuccess" 48 print "onFailure for", context[
"clientid"]
49 context[
"state"] =
"finished" 54 for i
in range(noclients):
55 myclientid = clientid+str(i)
56 rc, client = mqttv3.create(
"tcp://"+hostname+
":1883", myclientid, mqttv3.PERSISTENCE_DEFAULT, {
"sendWhileDisconnected" : 1})
58 print "rc from create is", rc
59 print "rc from setcallbacks is", mqttv3.setcallbacks(client, client, connectionLost, messageArrived, deliveryComplete)
61 print "rc from setconnected is", mqttv3.setconnected(client, client, connected)
64 context = {
"client" : client,
"clientid" : clientid,
"state" :
"connecting"}
66 print "rc from connect is", mqttv3.connect(client, {
"cleansession" : 0,
"automaticReconnect": 1,
"context": context,
"onSuccess": onSuccess,
"onFailure": onFailure})
68 clients.append(context)
70 while [x
for x
in clients
if x[
"state"] !=
"finished"]:
71 print [x
for x
in clients
if x[
"state"] !=
"finished"]
74 print "waiting for 60 seconds" 77 for client
in clients:
78 if mqttv3.isConnected(client[
"client"]):
79 print "rc from disconnect is", mqttv3.disconnect(client[
"client"], 1000)
81 mqttv3.destroy(client[
"client"])
def onSuccess(context, successData)
def connected(context, cause)
def onFailure(context, failureData)
def deliveryComplete(context, msgid)
def messageArrived(context, topicName, message)
def connectionLost(context, cause)