print "onFailure for", context["clientid"] context["state"] = "finished" noclients = 1 myclientid = None clients = [] for i in range(noclients): myclientid = clientid+str(i) rc, client = mqttv3.create("tcp://"+hostname+":1883", myclientid) #print "client is", hex(client) print "rc from create is", rc print "rc from setcallbacks is", mqttv3.setcallbacks(client, client, connectionLost, messageArrived, deliveryComplete) context = {"client" : client, "clientid" : clientid, "state" : "connecting"} print "rc from connect is", mqttv3.connect(client, {"context": context, "onSuccess": onSuccess, "onFailure": onFailure}) clients.append(context) while [x for x in clients if x["state"] != "finished"]: print [x for x in clients if x["state"] != "finished"] time.sleep(1) for client in clients: if mqttv3.isConnected(client["client"]): print "rc from disconnect is", mqttv3.disconnect(client["client"], 1000) time.sleep(1) mqttv3.destroy(client["client"]) print "after destroy"
print('mqttv3.setcallbacks(), rc = %d' % rc) rc = mqttv3.connect( client, { 'keepAliveInterval': int(os.getenv('MQTT_KEEP_ALIVE')), 'cleansession': 0, 'automaticReconnect': 1, 'maxRetryInterval': 10, 'context': context, 'on_success': on_success, 'on_failure': on_failure }) print('mqttv3.connect(), rc = %d' % rc) count = 0 while True: payload = str(count) print("OUT => " + payload) rc, _ = mqttv3.send(client, os.environ['MQTT_TOPIC'], payload, int(os.environ['MQTT_QOS']), 0, {}) print('mqttv3.send(), rc = %d' % rc) time.sleep(1) count += 1 rc = mqttv3.disconnect(client, 1000) print('mqttv3.disconnect(), rc = %d' % rc) rc = mqttv3.destroy(client) print('mqttv3.destroy, rc = %d' % rc)