how to check if internet is available

serkam
Posts: 7
Joined: Thu Nov 10, 2016 1:31 pm

Re: how to check if internet is available

Postby serkam » Wed Dec 11, 2019 2:29 pm

Hi everyone.

I am facing some software reset issues with the APROXIMATELLY same code Hossein listed:

void dns_found_cb(const char *name, const ip_addr_t *ipaddr, void *callback_arg)
{
ip_address = *ipaddr;
ESP_LOGE(TAG , "google.com ip is = %d" , ip_address.u_addr.ip4.addr);
ESP_LOGI(TAG , "internet connection : %s and ip : %d" , error ? "false" : "true" , ip_address.u_addr.ip4.addr);
}

void connection_status_task(void * parm){
while(1){
xEventGroupWaitBits(event_group, CONNECTED_BIT, pdFALSE, pdFALSE, portMAX_DELAY);
error = dns_gethostbyname(url , &ip_address , dns_found_cb , NULL);
ESP_LOGE(TAG , "dns_gethostbyname return value = %d" , error);
while( !DNSFound ) ;
vTaskDelay(30000/portTICK_PERIOD_MS);
}
}

If the wifi AP is operating normally BUT without internet connection, the function dns_gethostbyname() got stucked (do not return) and after 10 seconds (more or less), ESP32 is reseted by software (esp-idf/components/lwip/lwip/src/core/timeouts.c:175). If the internet connection returns, the same code above works normally.

Is there a version of dns_gethostbyname() that is NON-BLOCKING?

Sergio

Sprite
Espressif staff
Espressif staff
Posts: 10593
Joined: Thu Nov 26, 2015 4:08 am

Re: how to check if internet is available

Postby Sprite » Thu Dec 12, 2019 3:09 am

The software reset is odd. Can you tell a bit more about this?

In general: no, gethostbyname *cannot* be non-blocking as it uses DNS to resolve a hostname, which involves sending UDP packets to a DNS server and waiting for a reply. UDP packets can get lost and DNS servers can be slow to respond, so there has to be some amount of retransmits and waiting to see if there is a response as part of the process.

In general, there is no instant way to check for 'internet access' as a matter of how the Internet protocols work. Anything that *seems* instant (i.e. has low timeouts) runs a high risk of failing when there is a high-latency or congested link or a slow server involved.

mcmega
Posts: 16
Joined: Fri Oct 23, 2020 5:32 pm

Re: how to check if internet is available

Postby mcmega » Thu Dec 24, 2020 11:09 am

Did anyone manage to make a working version?
Share example of checking your internet connection!

excitedbox
Posts: 2
Joined: Sun Dec 27, 2020 8:15 pm

Re: how to check if internet is available

Postby excitedbox » Sun Dec 27, 2020 9:08 pm

Ping is not enough to know if there is an actual viable internet connection. To be sure there is a connection you need to open a TCP connection with a known host. You can generate a 204 response from google.com and be sure you can actually connect. Or you would need to build a full connection with some host and load some data. You may be able to check AP connection settings if you have a cooperative access point but if you are connecting through some computer you may not be able to check beyond knowing if it has a network connection.

A connection that is not configured or is filtered by the router/network operator (data capped) can still ping an address. So it really depends what you need it for to see what level of connection checking you need. Android has an API to check service availability you may want to checkout.

Maybe it would even be worth setting up a server that sends a custom response. like sending your own version of a ping. Similar to a 204 response with only a couple bytes/bits instead of a full header. As long as you control it you don't need to use the standardized formats to signal a connection. Responding at all can be your signal so 1 bit would be plenty.

Ritesh
Posts: 1395
Joined: Tue Sep 06, 2016 9:37 am
Location: India
Contact:

Re: how to check if internet is available

Postby Ritesh » Sat Jan 02, 2021 10:57 am

Ping is not enough to know if there is an actual viable internet connection. To be sure there is a connection you need to open a TCP connection with a known host. You can generate a 204 response from google.com and be sure you can actually connect. Or you would need to build a full connection with some host and load some data. You may be able to check AP connection settings if you have a cooperative access point but if you are connecting through some computer you may not be able to check beyond knowing if it has a network connection.

A connection that is not configured or is filtered by the router/network operator (data capped) can still ping an address. So it really depends what you need it for to see what level of connection checking you need. Android has an API to check service availability you may want to checkout.

Maybe it would even be worth setting up a server that sends a custom response. like sending your own version of a ping. Similar to a 204 response with only a couple bytes/bits instead of a full header. As long as you control it you don't need to use the standardized formats to signal a connection. Responding at all can be your signal so 1 bit would be plenty.
Yes. You are correct and also it can be helpful if you connect with your own external cloud network and send/receive some data to make sure that external network connectivity is there.
Regards,
Ritesh Prajapati

Victoria Nope
Posts: 75
Joined: Fri Dec 04, 2020 9:56 pm

Re: how to check if internet is available

Postby Victoria Nope » Sun Jan 03, 2021 8:45 am

In most of the applications one needs to connect to their own host. If that is the case, I would simply try to connect and exchange data with that target host. The reason is that something on the way to your own host can e.g. block or redirect the connection (antivirus, firewall, etc.). It's also a proof that at this particular moment your own host server is running.

Ritesh
Posts: 1395
Joined: Tue Sep 06, 2016 9:37 am
Location: India
Contact:

Re: how to check if internet is available

Postby Ritesh » Sun Jan 03, 2021 9:34 am

In most of the applications one needs to connect to their own host. If that is the case, I would simply try to connect and exchange data with that target host. The reason is that something on the way to your own host can e.g. block or redirect the connection (antivirus, firewall, etc.). It's also a proof that at this particular moment your own host server is running.
Yes. Exactly...
Regards,
Ritesh Prajapati

Dakshk
Posts: 23
Joined: Tue May 21, 2024 11:02 am

Re: how to check if internet is available

Postby Dakshk » Tue Aug 05, 2025 5:18 am

Hi
Could anybody tell me how to free DNS result. I'm using following code and I only get the result for the first time. Subsequent call returns from local host list and doesn't query DNS server to check internet connection and when I turn off my router's WiFi it tells me there is an internet connection wrongly. I also tried to use malloc() to assign memory to ip_address and then free it using free(), it doesn't work, too. I also plan to query DNS server every 20 seconds(because I need fast recovery after connection problem is solved). Is it advisable or DNS server protection system prevents me from doing so. I'm going to use 8.8.8.8 google's DNS server.

Code: Select all

void dns_found_cb(const char *name, const ip_addr_t *ipaddr, void *callback_arg)
{
	ip_address = *ipaddr;
	ESP_LOGE(TAG , "google.com ip is = %d" , ip_address.u_addr.ip4.addr);
	ESP_LOGI(TAG , "internet connection : %s and ip : %d" , error  ? "false" : "true" , ip_address.u_addr.ip4.addr);
}

void connection_status_task(void * parm){
	while(1){
		xEventGroupWaitBits(event_group, CONNECTED_BIT, pdFALSE, pdFALSE, portMAX_DELAY);
		error = dns_gethostbyname(url , &ip_address , dns_found_cb , NULL);
		ESP_LOGE(TAG , "dns_gethostbyname return value = %d" , error);
		while( !DNSFound ) ;
		vTaskDelay(30000/portTICK_PERIOD_MS);
	}
}


I have same use case and the same question I tried to reduce the list size but there's no benefit.
Were you able to solve the issue?

Ritesh
Posts: 1395
Joined: Tue Sep 06, 2016 9:37 am
Location: India
Contact:

Re: how to check if internet is available

Postby Ritesh » Thu Aug 14, 2025 9:19 am

Hi
Could anybody tell me how to free DNS result. I'm using following code and I only get the result for the first time. Subsequent call returns from local host list and doesn't query DNS server to check internet connection and when I turn off my router's WiFi it tells me there is an internet connection wrongly. I also tried to use malloc() to assign memory to ip_address and then free it using free(), it doesn't work, too. I also plan to query DNS server every 20 seconds(because I need fast recovery after connection problem is solved). Is it advisable or DNS server protection system prevents me from doing so. I'm going to use 8.8.8.8 google's DNS server.

Code: Select all

void dns_found_cb(const char *name, const ip_addr_t *ipaddr, void *callback_arg)
{
	ip_address = *ipaddr;
	ESP_LOGE(TAG , "google.com ip is = %d" , ip_address.u_addr.ip4.addr);
	ESP_LOGI(TAG , "internet connection : %s and ip : %d" , error  ? "false" : "true" , ip_address.u_addr.ip4.addr);
}

void connection_status_task(void * parm){
	while(1){
		xEventGroupWaitBits(event_group, CONNECTED_BIT, pdFALSE, pdFALSE, portMAX_DELAY);
		error = dns_gethostbyname(url , &ip_address , dns_found_cb , NULL);
		ESP_LOGE(TAG , "dns_gethostbyname return value = %d" , error);
		while( !DNSFound ) ;
		vTaskDelay(30000/portTICK_PERIOD_MS);
	}
}


I have same use case and the same question I tried to reduce the list size but there's no benefit.
Were you able to solve the issue?
What issue you are facing? Can you please let me know your issue with requirement so that will able to help accordingly from my end?

Regards,
Ritesh Prajapati
Regards,
Ritesh Prajapati

Ritesh
Posts: 1395
Joined: Tue Sep 06, 2016 9:37 am
Location: India
Contact:

Re: how to check if internet is available

Postby Ritesh » Thu Aug 14, 2025 9:20 am

Hi
Could anybody tell me how to free DNS result. I'm using following code and I only get the result for the first time. Subsequent call returns from local host list and doesn't query DNS server to check internet connection and when I turn off my router's WiFi it tells me there is an internet connection wrongly. I also tried to use malloc() to assign memory to ip_address and then free it using free(), it doesn't work, too. I also plan to query DNS server every 20 seconds(because I need fast recovery after connection problem is solved). Is it advisable or DNS server protection system prevents me from doing so. I'm going to use 8.8.8.8 google's DNS server.

Code: Select all

void dns_found_cb(const char *name, const ip_addr_t *ipaddr, void *callback_arg)
{
	ip_address = *ipaddr;
	ESP_LOGE(TAG , "google.com ip is = %d" , ip_address.u_addr.ip4.addr);
	ESP_LOGI(TAG , "internet connection : %s and ip : %d" , error  ? "false" : "true" , ip_address.u_addr.ip4.addr);
}

void connection_status_task(void * parm){
	while(1){
		xEventGroupWaitBits(event_group, CONNECTED_BIT, pdFALSE, pdFALSE, portMAX_DELAY);
		error = dns_gethostbyname(url , &ip_address , dns_found_cb , NULL);
		ESP_LOGE(TAG , "dns_gethostbyname return value = %d" , error);
		while( !DNSFound ) ;
		vTaskDelay(30000/portTICK_PERIOD_MS);
	}
}


I have same use case and the same question I tried to reduce the list size but there's no benefit.
Were you able to solve the issue?
What issue you are facing? Can you please let me know your issue with requirement so that will able to help accordingly from my end?

Regards,
Ritesh Prajapati
Regards,
Ritesh Prajapati

Who is online

Users browsing this forum: Amazon [Bot], ChatGPT-User, PerplexityBot, Semrush [Bot] and 18 guests