Exemple #1
0
def onSuccess(context, successData):
    print "onSuccess for", context["clientid"], context["state"], successData
    responseOptions = {
        "context": context,
        "onSuccess": onSuccess,
        "onFailure": onFailure
    }
    if context["state"] == "connecting":
        context["state"] = "subscribing"
        print "rc from subscribe is", mqttv3.subscribe(client, topic, 2,
                                                       responseOptions)
    elif context["state"] == "subscribing":
        context["state"] = "publishing qos 0"
        print "rc from publish is", mqttv3.send(client, topic,
                                                "a QoS 0 message", 0, 0,
                                                responseOptions)
    elif context["state"] == "publishing qos 0":
        context["state"] = "publishing qos 1"
        print "rc from publish is", mqttv3.send(client, topic,
                                                "a QoS 1 message", 1, 0,
                                                responseOptions)
    elif context["state"] == "publishing qos 1":
        context["state"] = "publishing qos 2"
        print "rc from publish is", mqttv3.send(client, topic,
                                                "a QoS 2 message", 2, 0,
                                                responseOptions)
    elif context["state"] == "publishing qos 2":
        context["state"] = "finished"
    print "leaving onSuccess"
Exemple #2
0
def onSuccess(context, successData):
	print "onSuccess for", context["clientid"], context["state"], successData
	responseOptions = {"context": context, "onSuccess": onSuccess, "onFailure" : onFailure}
	if context["state"] == "connecting":
		context["state"] = "subscribing"
		print "rc from subscribe is", mqttv3.subscribe(client, topic, 2, responseOptions)
	elif context["state"] == "subscribing":
		context["state"] = "publishing qos 0"
		print "rc from publish is", mqttv3.send(client, topic, "a QoS 0 message", 0, 0, responseOptions)
	elif context["state"] == "publishing qos 0":
		context["state"] = "publishing qos 1"
		print "rc from publish is", mqttv3.send(client, topic, "a QoS 1 message", 1, 0, responseOptions)
	elif context["state"] == "publishing qos 1":
		context["state"] = "publishing qos 2"
		print "rc from publish is", mqttv3.send(client, topic, "a QoS 2 message", 2, 0, responseOptions)
	elif context["state"] == "publishing qos 2":
		context["state"] = "finished"
	print "leaving onSuccess"
Exemple #3
0
def connectionLost(context, cause):
    print "connectionLost", cause
    client = context
    responseOptions = {
        "context": context,
        "onSuccess": onSuccess,
        "onFailure": onFailure
    }
    print "rc from publish while disconnected is", mqttv3.send(
        client, topic, "message while disconnected", 2, 0, responseOptions)
Exemple #4
0
def connectionLost(context, cause):
	print "connectionLost", cause
	client = context
	responseOptions = {"context": context, "onSuccess": onSuccess, "onFailure" : onFailure}
	print "rc from publish while disconnected is", mqttv3.send(client, topic, "message while disconnected", 2, 0, responseOptions)
Exemple #5
0
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)