Copyright ©

Mongoose OS Forum



I am trying to implement some reliable MQTT QOS=1.

mgos_mqtt_pub is documented as follows:

 * Publish message to the configured MQTT server, to the given MQTT topic.
 * Return value will be true if there is a connection to the server and the
 * message has been queued for sending. In case of QoS 1 return value does
 * not indicate that PUBACK has been received; there is currently no way to
 * check for that.

I did take the tack of implementing a mqtt callback (installed with mgos_mqtt_add_global_handler(sti_mqtt_cb, NULL);). I am interesting in getting the message_id when the message is sent, and seeing the corresponding message_id in the PUBACK messages so I can be positive the message got where it's going.

I can see callbacks when the AWS shadow code publishes messages (the log shows mgos_aws_shadow_ev logging when it publishes, followed by my callback getting the MG_EV_MQTT_PUBLISH event, with a message id).

[Jan 10 10:30:13.277] mgos_aws_shadow_ev   203 [{"state":{"reported":{"ota":{"message":"idle","status":0,"is_committed":true,"commit_timeout":0,"partition":0,"progress_percent":0}}},"metadata":{"reported":{"ota":{"message":{"timestamp":1547134212},"status":{"timestamp":1547134212},"is_committed":{"timestamp":1547134212},"commit_timeout":{"timestamp":1547134212},"partition":{"timestamp":1547134212},"progress_percent":{"timestamp":1547134212}}}},"version":354,"timestamp":1547134212,"clientToken":"ac032392"}]
[Jan 10 10:30:13.319] sti_mqtt_cb          MQTT_EV_MQTT_PUBLISH (msg_id: 3, payload: "{"state":{"reported":{"ota":{"message":"idle","status":0,"is_committed":true,"commit_timeout":0,"partition":0,"progress_percent":0}}},"metadata":{"reported":{"ota":{"message":{"timestamp":1547134212},"status":{"timestamp":1547134212},"is_committed":{"timestamp":1547134212},"commit_timeout":{"timestamp":1547134212},"partition":{"timestamp":1547134212},"progress_percent":{"timestamp":1547134212}}}},"version":354,"timestamp":1547134212,"clientToken":"ac032392"}")

I never see any MG_EV_MQTT_PUBACK events for the AWS shadow messages (I haven't checked, possibly being sent qos=0?)

For messages that my code sends with QOS=1, my callback never sees the MG_EV_MQTT_PUBLISH event, but I do see MG_EV_MQTT_PUBACK events at about the right time with a message id, and the message ids increment as I send messages):

[Jan 10 10:34:21.331] mgos_mqtt_pub        Publishing to sensor/sti_30AEA44D4D3C/01-35580 @ 1 (161): [{"ti": 1547134462.013297, "alarm_v": 0, "alarm_d": -1, "po_v": 0, "lb_v": 0, "lb_d": 0, "ta_v": 0, "ta_d": 0, "q": 0, "alive_v": 1, "alive_d": 0, "status": "00"}]
[Jan 10 10:34:21.473] mgos_mqtt_ev         MQTT event: 204
[Jan 10 10:34:21.476] sti_mqtt_cb          MQTT_EV_MQTT_PUBACK (msg_id: 9)

Why doesn't my callback see any MG_EV_MQTT_PUBLISH events for messages I send?


  • nliviunliviu Romania
    edited January 11

    MG_EV_MQTT_PUBLISH is handled by call_topic_handler which calls a callback set by mgos_mqtt_global_subscribe


    [Jan 11 19:40:38.427] do_subscribe         Subscribing to 'test_sub'
    [Jan 11 19:40:38.450] mqtt_print_message   mqtt message : {"cmd": SUBACK, "qos": 0, "len": 5, "connack_ret_code": 0, "message_id": 1, "protocol_version": 0, "connect_flags": 0, "keep_alive_timer": 0}
    [Jan 11 19:40:38.456] mqtt_ev_handler      ev=209 MQTT_SUBACK: 1 acknowledged
    [Jan 11 19:41:40.547] mqtt_print_message   mqtt message : {"cmd": PUBLISH, "qos": 0, "len": 16, topic: "test_sub", payload: "1234", "connack_ret_code": 0, "message_id": 0, "protocol_version": 0, "connect_flags": 0, "keep_alive_timer": 0}
    [Jan 11 19:41:40.555] mqtt_ev_handler      ev=203 MQTT_PUBLISH: paylod=[1234]
    [Jan 11 19:42:14.113] mqtt_print_message   mqtt message : {"cmd": PUBLISH, "qos": 0, "len": 22, topic: "test_sub", payload: "1234, 5678", "connack_ret_code": 0, "message_id": 0, "protocol_version": 0, "connect_flags": 0, "keep_alive_timer": 0}
    [Jan 11 19:42:14.120] mqtt_ev_handler      ev=203 MQTT_PUBLISH: paylod=[1234, 5678]
    [Jan 11 19:42:35.423] mqtt_print_message   mqtt message : {"cmd": PUBLISH, "qos": 0, "len": 28, topic: "test_sub", payload: "{a=1234, b=5678}", "connack_ret_code": 0, "message_id": 0, "protocol_version": 0, "connect_flags": 0, "keep_alive_timer": 0}
    [Jan 11 19:42:35.431] mqtt_ev_handler      ev=203 MQTT_PUBLISH: paylod=[{a=1234, b=5678}]
    [Jan 11 19:43:40.335] mqtt_print_message   mqtt message : {"cmd": PUBLISH, "qos": 1, "len": 30, topic: "test_sub", payload: "{a=1234, b=5678}", "connack_ret_code": 0, "message_id": 1, "protocol_version": 0, "connect_flags": 0, "keep_alive_timer": 0}
    [Jan 11 19:43:40.342] mqtt_ev_handler      ev=203 MQTT_PUBLISH: paylod=[{a=1234, b=5678}]

    using mosquitto-pub

    mosquitto_pub -t "test_sub" -m "1234"
    mosquitto_pub -t "test_sub" -m "1234, 5678"
    mosquitto_pub -t "test_sub" -m "{a=1234, b=5678}"
    mosquitto_pub -t "test_sub" -q 1 -m "{a=1234, b=5678}"

    PS. The brackets in MQTT_PUBLISH are part of the log formatting.

  • @nliviu Hi. Is there any way to handle the PUBACK after a message has been published? My design is:
    1. Publish a message
    2. After receive PUBACK, put the device into deep sleep mode for 60 seconds
    3. Repeat 1.

  • nliviunliviu Romania

    Install a global handler with mgos_mqtt_add_global_handler and process MG_EV_MQTT_PUBACK.

Sign In or Register to comment.