0

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.

Ashida
  • 1
  • 1
  • I think there is a bug in `on_message`, could you please add two more prints, one right before the `if` and one after. – Joe May 01 '20 at 14:57
  • also `print(response_json)` and `print(response_dict)` – Joe May 01 '20 at 14:58
  • You could also use some `try...except` in that function. – Joe May 01 '20 at 14:59
  • Thanks for your comments. I updated my question description based on your suggestions. Instead of adding some lines, I removed whole of codes from on_message() function. – Ashida May 07 '20 at 05:55
  • https://github.com/eclipse/paho.mqtt.android/issues/302 – Joe May 07 '20 at 06:00
  • Maybe https://stackoverflow.com/questions/36184490/mqtt-client-disconnects-when-another-client-connects-to-the-server – Joe May 07 '20 at 06:02
  • Did you look in the MQTT server log? It might mention a reason why the connection is disconnected. Like mentioned in the above link, a non-unique client ID could very well be the reason of the disconnection. Could you try with a different ID? – wovano May 07 '20 at 06:04
  • I changed client ID and tried again. Please I updated my question too. Still not fixed my issue. – Ashida May 07 '20 at 07:19
  • Unfortunately I do not know the way to check MQTT server log, since I am connecting Watson IoT platform - IBM's cloud service. – Ashida May 07 '20 at 07:20
  • @Joe I got the cause of my issue. As you mentioned the connection was terminated due to duplicated client ID. I omitted lines other than MQTT handling code, but I implemented my trial code as Flask application. Flask with debug mode enabled sometimes may reload codes somehow. I do not know why but if I disable debug mode, my issue was gone. – Ashida May 08 '20 at 12:03
  • @Joe By the way, I would like to close my question, but I have no idea to close my question by myself. Can you advise me ? – Ashida May 08 '20 at 12:06
  • Sorry, no idea. I will flag it as duplicate, too. – Joe May 08 '20 at 12:08
  • Yes, I think so. – Ashida May 08 '20 at 13:57

0 Answers0