Ashida
Ashida

Reputation: 1

MQTT connection connected and disconnected repeatedly

I am writing Watson IoT application which subscribes device status messages, like below.

def on_connect(client, userdata, flags, respons_code):
    client.on_message = on_message
    client.subscribe('iot-2/type/+/id/+/mon')
    print(datetime.now().strftime("%Y/%m/%d %H:%M:%S") + ": mqtt connected")

def on_disconnect(client, userdata, rc):
    print(datetime.now().strftime("%Y/%m/%d %H:%M:%S") + ": mqtt disconnected")

def on_message(client, userdata, msg):
    print(datetime.now().strftime("%Y/%m/%d %H:%M:%S") + ": mqtt message arrived")
    response_json = msg.payload.decode("utf-8")
    response_dict = json.loads(response_json)
    if(response_dict["id"] == "g:xxxxx:xxxxx:xxx"):
        status = response_dict["connectionStatus"]
        print("status: "+status)

client = mqtt.Client(client_id='a:xxxxx:appl1', protocol=mqtt.MQTTv311)
client.username_pw_set('a-xxxxx-xxxxxxxx', password='xxxxxxxxxx')
client.on_connect = on_connect
client.on_disconnect = on_disconnect
client.connect('de.messaging.internetofthings.ibmcloud.com', 1883, 60)
client.loop_start()

The above code establish the connection with Watson IoT, however the connection was lost after a few seconds. Paho Mqtt automatically reconnected then, but that connection also disconnected soon.

2020/05/01 22:35:16: mqtt connected
2020/05/01 22:35:17: mqtt message arrived
2020/05/01 22:35:19: mqtt disconnected
2020/05/01 22:35:19: mqtt connected
2020/05/01 22:35:19: mqtt message arrived
2020/05/01 22:35:20: mqtt disconnected
2020/05/01 22:35:22: mqtt connected
2020/05/01 22:35:22: mqtt connected
2020/05/01 22:35:22: mqtt disconnected
2020/05/01 22:35:22: mqtt message arrived
2020/05/01 22:35:23: mqtt disconnected
2020/05/01 22:35:24: mqtt connected

I also tried using wiotp-sdk, but the symptom was same, the connection repeatedly established and terminated.

Also I changed keep alive interval setting passed to connect() method. But this did not improve my issue.


Added on 2020/05/07

Considering potential bug in on_message(), I removed all of on_message() code and try again. The symptom was not changed even removing codes from on_message()...

def on_message(client, userdata, msg):
    pass

The logs are below.

2020/05/07 14:43:17: mqtt connected
2020/05/07 14:43:20: mqtt disconnected
2020/05/07 14:43:20: mqtt connected
2020/05/07 14:43:21: mqtt connected
2020/05/07 14:43:21: mqtt disconnected
2020/05/07 14:43:23: mqtt disconnected
2020/05/07 14:43:23: mqtt connected
2020/05/07 14:43:25: mqtt disconnected
2020/05/07 14:43:25: mqtt connected
2020/05/07 14:43:26: mqtt connected
2020/05/07 14:43:26: mqtt disconnected
2020/05/07 14:43:28: mqtt disconnected

Added on 2020/05/07

I changed client ID like below, but the symptom was not changed. I do not think my issue is due to duplicated client ID ....

client = mqtt.Client(client_id='a:syi5mz:appl2', protocol=mqtt.MQTTv311)

Added on 2020/05/11

I finally got the root cause of this issue. I cut & pasted MQTT related code, but there are unexpected behavior in the remaining part.

I am using flask to run my codes as a part of web application, and in that library child process is invoked. MQTT related code I pasted called both from parent and child processes, and each of process attempted to establish MQTT connection using same client ID.

As the result, child process MQTT connection was once connected and soon disconnected due to duplicate client ID.

Upvotes: 0

Views: 68

Answers (0)

Related Questions