This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Mqtt connection timeout setting

Hi, 

We have observed that in areas of poor lte connectivity, the client_inti() works well but the mqtt_connect() takes a couple of minutes and times out.

I need to change the mqtt connection timeout duration make it 20-30 seconds, since if it does not connect in this duration, for our use-case, it wont connect after either and the firmware can move on.

In decent lte connectivity regions, mqtt_connect returns within a second or less.

Thanks

Noaman

Parents
  • Hi,

     

    Unfortunately, there is no option to set the timeout at this time, but I will add this as a feature request internally.

     

    Kind regards,

    Håkon

  • thank you Haken.

    Can you help me add a timeout for http connection requests?

    Regards

    Noaman

  • Hi,

     

    If you enable non-blocking socket operations, the connect() call will return EINPROGRESS until its done.

    That can be done like this:

    #include <fcntl.h>
    
        ....
    	printk("enable non-blocking connect\n");
    	printk("Will return EINPROGRESS (%d) when in progress\n", EINPROGRESS);
    	int flags = fcntl(fd, F_GETFL, 0);
    	int ret = fcntl(fd, F_SETFL, flags | O_NONBLOCK);
    	int rflags = fcntl(fd, F_GETFL, 0);	
    	
    	
    	printk("connect time: %lld\n", k_uptime_get());
    	ret = connect(...)
    	printk("connect time: %lld\n", k_uptime_get());
    	....

     

    I added the logic to the https_client sample in ncs v1.6.0 just so that you can see that the call returns almost immediately when you enable nonblocking socket operation.

    diff --git a/samples/nrf9160/https_client/src/main.c b/samples/nrf9160/https_client/src/main.c
    index 509aacb96..18f01a410 100644
    --- a/samples/nrf9160/https_client/src/main.c
    +++ b/samples/nrf9160/https_client/src/main.c
    @@ -5,6 +5,7 @@
      */
     
     #include <string.h>
    +#include <fcntl.h>
     #include <zephyr.h>
     #include <stdlib.h>
     #include <net/socket.h>
    @@ -202,7 +203,25 @@ void main(void)
     	}
     
     	printk("Connecting to %s\n", "example.com");
    +	#if 1
    +	printk("enable non-blocking connect\n");
    +	printk("Will return EINPROGRESS (%d) when in progress\n", EINPROGRESS);
    +	int flags = fcntl(fd, F_GETFL, 0);
    +	int ret = fcntl(fd, F_SETFL, flags | O_NONBLOCK);
    +	int rflags = fcntl(fd, F_GETFL, 0);	
    +	#endif
    +	printk("connect time: %lld\n", k_uptime_get());
    +
    +	/* define this to handle EINPROGRESS scenario */
    +	#if ENABLE_NONBLOCK
    +	do {
    +	#endif
     	err = connect(fd, res->ai_addr, sizeof(struct sockaddr_in));
    +	#if ENABLE_NONBLOCK
    +	} while (err == EINPROGRESS);
    +	#endif
    +	
    +	printk("connect time: %lld\n", k_uptime_get());
     	if (err) {
     		printk("connect() failed, err: %d\n", errno);
     		goto clean_up;
    

     

    Kind regards,

    Håkon

  • Thank you so much Haken.

    Will test this out and let you know.\

    so basically, the do-while loop will keep calling connect repetitively until EINPROGRESS is being returned. 
    If I wanted to exit, say after 20 seconds, i can just break out of the while loop and go to clean up? Will that crash anything when the connect request finally returns a few minutes later but the socket has been closed?

  • Hi,

     

    That is up to you how you time out, you can do it this way by breaking out of the loop (and handling the closing scenario), but if the connect() succeeds; you need to handle the scenario of having non-blocking socket operations going on forward (or go back to blocking)

     

    Kind regards,

    Håkon

Reply Children
No Data
Related