Best way to detect if you have a valid internet connection?

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Best way to detect if you have a valid internet connection?

1,108 Views
ironsean
Contributor V

Hello,

I'm experiencing an issue with my FRDM-K64F board running MQX 4.2.2. I'm building a firmware that depends on internet connections and works through a wireless adapter over the Ethernet port(tiny routers in client mode essentially). But these adapters seem to be inconsistent, losing connection, etc. This in turn leads to my firmware appearing to lock up. I installed a bunch of watchdog timers, but they don't seem to be doing the trick.

I'll need to do some more in depth testing to see what happens when there's no internet connection and why things lock, but in the meantime:

Is there a good way to check if I have a valid connection to the outside world? I'm probably going to put a check in a task, with that if failed will set the device reset bit and let me start again.

Sean

Labels (1)
Tags (2)
0 Kudos
Reply
2 Replies

875 Views
soledad
NXP Employee
NXP Employee

Hello Sean,

There is not an algorithm already implemented for that but you can use the function getaddrinfo() to get the same to a host (i.e. freescale.com) if the function returns different than NULL then you have internet connection otherwise you don’t have it. You can put this into an infinite loop that checks every 5 seconds or every minute and you will be able to monitor the internet connectivity.

Below you can find an example using getaddrinfo() function:

struct addrinfo *addrinfo_result;

struct addrinfo *addrinfo_result_first;

int32_t retval;

char addr_str[RTCS_IP6_ADDR_STR_SIZE];

_mem_zero(&addrinfo_hints, sizeof(addrinfo_hints));

addrinfo_hints.ai_flags = AI_CANONNAME;

retval = getaddrinfo("www.example.com", NULL, NULL, &addrinfo_result);

if (retval == 0)

{

addrinfo_result_first = addrinfo_result;

/* Print all resolved IP addresses.*/

while(addrinfo_result)

{

if(inet_ntop(addrinfo_result->ai_family,

&((struct sockaddr_in6 *)((*addrinfo_result).

ai_addr))->sin6_addr,

addr_str, sizeof(addr_str)))

{

printf("\t%s\n", addr_str);

}

addrinfo_result = addrinfo_result->ai_next;

}

freeaddrinfo(addrinfo_result_first);

}

else

{

printf("Unable to resolve host\n");

}


Have a great day,
Sol

-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
-----------------------------------------------------------------------------------------------------------------------

875 Views
ironsean
Contributor V

Thanks, I'll give this a try!

0 Kudos
Reply