Lost internet connectivity / reconnect options

Hi All,

I am thinking about adding OpenHab on a RPi to our summer cottage. Not that I need to automate much, but why not?
The place has no internet connection, but I have a TP-Link router (TL-MR3420) with a 3G stick plugged into it. When I stay there sometimes I notice that the stick looses connection and there is not much I can do make it reconnect. TP-Link just have a simple setting keep connection alive or not. I always unplug the router and plug it back again. Of course I can do that with OpenHab by disconnecting the power to the router with a relay on the GPIO. But what is the best way to check the network connection? Is this the HealthCheck binding which keeps checking something like google.com every 5 minutes? Any other methods you recommend?


Using network to ping a site is one way of doing it, but what if google goes down temporarily or for some reason you’re unable to resolve it’s domain? You’ll disconnect the power to your router a little unnecessarily.

Can ping your default gateway using an exec command instead? i.e.

ping -q -w 1 -c 1 `ip r | grep default | cut -d ' ' -f 3` > /dev/null && echo ON || echo OFF

But if my stick looses internet connection, my local network and the router is still running just fine. But I would not be able to connect to OpenHab from the outside which is my main concern.

Whoops, sorry completely misunderstood that. My bad…

I think what you’re doing is fine though, since it’s likely that the website would go down. You can try their DNS server ip directly ( in case your current DNS server faults. That or using a couple of additional locations outside of google will minimise the risk of a false alarm.

I second that. I have a similar setup to monitor my internet connection (albeit without any actions apart from logging periods of no internet connectivity).

I selected 5 different sites that I believe to be having quite good uptimes, amongst them Google, my ISP, a popular weather provider, a popular news provider, etc.

The I have a simple rule saying that if all are unavailable, then my internet connection is down. This has worked quite well for me.

Thanks guys for all the feedback.

Connecting to an IP directly and having multiple sites are both excellent recommendations. You should follow these.
I just want to add that I am monitoring several devices solely by checking and there was not once a false negative.

How does your one-liner have to change in order to ping the Google DNS server, instead og the local gateway?

in theory: simply insert the IP of Google’s DNS in the line, replacing his statement within the backticks (`) which is grabbing the IP of the default gateway:

ping -q -w 1 -c 1 > /dev/null && echo ON || echo OFF
1 Like

Exactly right,

-q    only report at the end
-w 1  stops the process if nothing responds within one second
-c 1  only send one packet.

For openHAB though, I feel that the network binding achieves this well.


Just bumped into this old topic. Can I ask a follow on question pls?
OK, so if I poll 5 different sites and they’re all down, my internet connection is gone. I get that.
My question is this - what is the most violent thing I can put in a script for a raspberry pi to correct this situation WITHOUT resorting to a reboot?
I have tried using ifdown/ifup but that didn’t solve it.
I tried sudo service networking restart but that seemed to overwrite my /etc/resolv.conf to the extent that the rpi couldn’t resolve any domain names (it emptied out the previously existing nameserver entry which is what I think happens on a reboot but it wasn’t clear to me what process is supposed to re-create it in a workable form)
So, to reiterate - what can I do short of a reboot that I haven’t already tried?

Thanks everyone


My connection issues are not with the pi, but with the 3g router providing internet access. So I want to reboot that instead. One the router restores connection the pi is back online.

Did you figure out a proper way for your issue?