How can I solve the problem that MQTT connection is lost?

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
AkKo_4458241
Level 2
Level 2

Hello.

I refer to publisher.c and subscriber.c of demo,
I am creating MQTT transmission / reception processing.
The main processing flow is as follows.

(1) Connection to AWS
    get_aws_credentials_from_resources ()
    wiced_aws_init ()
    wiced_aws_create_endpoint ()
    do_connect_and_acknowledge ()
(2) Subscribing
    wiced_aws_subscribe ()
(3) Publish transmission / Publish reception
    wiced_aws_publish ()
    my_publisher_aws_callback ()
(4) Disconnect from AWS
    wiced_aws_disconnect ()
    wiced_aws_deinit ()

I have a question.
While waiting for the processing of (2), (3),
If the WiFi connection is lost, the following message will appear.
    [AWS / MQTT] Event received 2
    [Application / AWS] Disconnection Received

Also, the last "else" of mqtt_manager (),
Stop at "WPRINT_LIB_ERROR (("[MQTT LIB] Not connected \ r \ n"));

Is there a workaround?
How do I restore the connection?
For example, after (3) and (4), should I start over from (1)?

Thank you.

0 Likes
1 Solution

In the event of a link loss such as switching off the AP, the Wi-Fi device would indicate a Link event (type, status, reason, flags) 12 0 3 0 and a "Wireless Link Down!" message and application disconnection event WICED_AWS_EVENT_DISCONNECTED. If there is no wireless link, there can be no MQTT connection. The device would subsequently send 802.11 probe requests containing the AP SSID to re-associate with the AP. After the AP is turned on again, the Wi-Fi device would re-associate with the AP. After re-association, a "Wireless Link Up" message is shown along with Link event (type, status, reason, flags) 16 0 0 1. After this, you can press Button 1 on your EVAL board to re-establish MQTT connection and then you can publish messages.

I tested the app with removing the statements

WPRINT_LIB_ERROR( ("[MQTT LIB] Not connected\r\n ") );

        return WICED_ERROR;

I did not observe any issue. The MQTT connection could still be re-established. But since it enters the else condition in mqtt_manager(), it means that conn->network_status = MQTT_NETWORK_DISCONNECTED. It will be incorrect that the mqtt_manager() returns WICED_SUCCESS if there has been a disconnection especially when the return value of mqtt_manager() is checked during heartbeat mqtt_manager_tick().

View solution in original post

3 Replies
GauravS_31
Moderator
Moderator
Moderator
10 questions asked 250 solutions authored 250 sign-ins

If connection is lost, the variable is_connected will be WICED_FALSE and the application will attempt to reconnect with retry attempts defined in WICED_AWS_DEFAULT_CONNECTION_RETRIES as 3. You can check the if(!is_connected) condition under application_start() where this is checked. The application should take care of reconnection due to link loss. You can consider increasing the value of WICED_AWS_DEFAULT_CONNECTION_RETRIES to increase the number of retry attempts.

Hello.

Thank you very much for your answer.

I will increase the value of WICED_AWS_DEFAULT_CONNECTION_RETRIES.

Let me ask you again.

While waiting for the processing of (2), (3),

I turn off the router, the program stops at "mqtt_manager ()*.

     *WPRINT_LIB_ERROR (("[MQTT LIB] Not connected \ r \ n));
Is there any way to avoid program stoppage?

Is there a problem removing this code?

Thank you.

0 Likes

In the event of a link loss such as switching off the AP, the Wi-Fi device would indicate a Link event (type, status, reason, flags) 12 0 3 0 and a "Wireless Link Down!" message and application disconnection event WICED_AWS_EVENT_DISCONNECTED. If there is no wireless link, there can be no MQTT connection. The device would subsequently send 802.11 probe requests containing the AP SSID to re-associate with the AP. After the AP is turned on again, the Wi-Fi device would re-associate with the AP. After re-association, a "Wireless Link Up" message is shown along with Link event (type, status, reason, flags) 16 0 0 1. After this, you can press Button 1 on your EVAL board to re-establish MQTT connection and then you can publish messages.

I tested the app with removing the statements

WPRINT_LIB_ERROR( ("[MQTT LIB] Not connected\r\n ") );

        return WICED_ERROR;

I did not observe any issue. The MQTT connection could still be re-established. But since it enters the else condition in mqtt_manager(), it means that conn->network_status = MQTT_NETWORK_DISCONNECTED. It will be incorrect that the mqtt_manager() returns WICED_SUCCESS if there has been a disconnection especially when the return value of mqtt_manager() is checked during heartbeat mqtt_manager_tick().