how to check if internet is available

davdav
Posts: 208
Joined: Thu Nov 17, 2016 2:33 pm

how to check if internet is available

Postby davdav » Wed Mar 14, 2018 9:14 am

Hi,

In our project we have both WiFi and GSM module. GSM is used as a fallback in case WiFi is not available or internet connection is lost.

Determine if WiFi is not available is quite simple.How can I check if internet connection is present on WiFi side?
For sure I can ping an external server periodically, but I would like to avoid this solution.

Is there a way in esp32 to check this condition directly? Is the

Code: Select all

SYSTEM_EVENT_STA_LOST_IP
event a good information to say that internet connection is lost?

Thanks

User avatar
kolban
Posts: 1683
Joined: Mon Nov 16, 2015 4:43 pm
Location: Texas, USA

Re: how to check if internet is available

Postby kolban » Wed Mar 14, 2018 2:18 pm

Looking at that option (which I'd never seen before ... nice pointer ... thank you), it seems that there is a configurable option called IP_LOST_TIMER_INTERVAL which is a period in seconds (0 = off). Although not explicitly stated, my guess is that if your ESP32 is being a station to an access point, each interval an attempt is made to ping the access point. If that fails, then then local ESP32 is considered to have lost its IP address and this event will be thrown. Again ... this only a guess.

In your original post, you used the phrase "Is the internet available". Do you mean by that your ESP32 connected to your local access point or do you mean your ESP32 connected to your local access point AND the access point having a usable connection to the Internet as a whole. For example, if you have a home Access Point and your ESP32 connects to it ... and then you pull the modem plug on that access point, the ESP32 is still network connected, it can still connect to local devices but it can't connect to the Internet. If the later is your goal, I think your only option is to try and ping an Internet host to check "liveness".
Free book on ESP32 available here: https://leanpub.com/kolban-ESP32

davdav
Posts: 208
Joined: Thu Nov 17, 2016 2:33 pm

Re: how to check if internet is available

Postby davdav » Wed Mar 14, 2018 2:54 pm

Thanks Kolban for highlight "IP_LOST_TIMER_INTERVAL" parameter. I will check it.

When I state "internet is available" I means that ESP32 is connected to a local access point which is connected to external internet. As you wrote I'm looking for a way to discover if the modem cable has been disconnected.

If there is no easy solution inside ESP32 to check that condition i will probably go with "ping" solution (even if I'm not so happy because it will consume resources..)

Thanks

thethinker
Posts: 58
Joined: Thu Mar 01, 2018 1:26 am

Re: how to check if internet is available

Postby thethinker » Fri Mar 16, 2018 3:50 am

davdav wrote:Thanks Kolban for highlight "IP_LOST_TIMER_INTERVAL" parameter. I will check it.

When I state "internet is available" I means that ESP32 is connected to a local access point which is connected to external internet. As you wrote I'm looking for a way to discover if the modem cable has been disconnected.

If there is no easy solution inside ESP32 to check that condition i will probably go with "ping" solution (even if I'm not so happy because it will consume resources..)

Thanks
You can also use the DNS resolver to try to resolve google.com, if it doesn't resolve it means you have no internet.

davdav
Posts: 208
Joined: Thu Nov 17, 2016 2:33 pm

Re: how to check if internet is available

Postby davdav » Fri Mar 16, 2018 1:10 pm

Thank you @thethinker for suggestion. This could be an alternative.

As I mentioned I have also a GSM module for fallback and I was looking for a method included in esp32 to check absence of internet without starting any TCP/IP job.

Anyway, it seems this is not available so I will rely on different solution.

acsreedharreddy
Posts: 2
Joined: Sat Nov 10, 2018 7:28 am

Re: how to check if internet is available

Postby acsreedharreddy » Fri Apr 19, 2019 5:46 am

Hey how did you solve this?

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

Re: how to check if internet is available

Postby Ritesh » Sun Apr 21, 2019 10:40 am

davdav wrote:
Fri Mar 16, 2018 1:10 pm
Thank you @thethinker for suggestion. This could be an alternative.

As I mentioned I have also a GSM module for fallback and I was looking for a method included in esp32 to check absence of internet without starting any TCP/IP job.

Anyway, it seems this is not available so I will rely on different solution.
I believe DNS resolution will be best option instead of pinging to external network. We have used that way into one if our project to check external connectivity is there or not.

I think there is no any built in option into ESP32 IDF to verify external connectivity.
Regards,
Ritesh Prajapati

davdav
Posts: 208
Joined: Thu Nov 17, 2016 2:33 pm

Re: how to check if internet is available

Postby davdav » Tue Apr 23, 2019 3:55 pm

Me too I used DNS resolver as a mean to understand if external connection is available or not.
Do not forget to "free" the DNS result or you will have leak memory issue..

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

Re: how to check if internet is available

Postby Ritesh » Tue Apr 23, 2019 4:59 pm

davdav wrote:
Tue Apr 23, 2019 3:55 pm
Me too I used DNS resolver as a mean to understand if external connection is available or not.
Do not forget to "free" the DNS result or you will have leak memory issue..
Yes. You are correct.. Need to free address info once DNS Request Service is served..
Regards,
Ritesh Prajapati

Hossein
Posts: 4
Joined: Tue Aug 20, 2019 8:14 pm

Re: how to check if internet is available

Postby Hossein » Sun Sep 01, 2019 8:24 pm

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);
	}
}

Who is online

Users browsing this forum: No registered users and 87 guests