I'm working on an IOT home automation app using raspberry pi.

I'm using a 3G modem dongle as a fail-safe internet connection with a primary wifi connection.

If the primary connection fails, the system disconnects from the wifi, connects to the backup (3g) and then reconnects to the wifi lan.. This way It can access the internet and on the same time I can access the system through lan.

The problem is, once the system switches to the backup (3g) connection, It cannot detect when the problem with primary internet connection goes away..

I cannot find an effective way to overcome this problem:

  1. I can disable the 3g and check the primary connection every x minutes... but this approach is too expensive.
  2. I made a script to access the router page via curl and use the diagnostic tests to check for Internet. This is somewhat a better approach however, there is too many variables involved, It depends on the router model, password, etc.. if anything ever changed, everything breaks down.
  3. I can communicate with another machine on lan to check for internet instead but I don't like the idea of my system being dependent on another machine.

So anyone has a better approach? Could I keep 2 internet connections on the system?

  • I would just use pfSense dedicated isntead of trying to reinvent the wheel. Unless your goal is to reinvent the wheel then peruse the pfSense code to see how they monitor WAN connections for up down time. Other than that,, your question is not really suited for this Pi community
    – Piotr Kula
    Commented Apr 26, 2017 at 7:42
  • This is a bit ugly, but you could set up a static route to your ping target (/32) via the wifi network. It will fail as long as the wifi Internet connection is down. Does your wifi router do SNMP?
    – bobstro
    Commented Jul 25, 2017 at 18:27
  • My 2nd comment looks to have gotten lost. Ignore if a duplicate. Do you really need to down the 3G network in order to test the wifi network? Try just changing your default gateway on the fly for checks, then restore it to the 3G network if wifi checks fail. Still not particularly graceful, but it might work well enough.
    – bobstro
    Commented Jul 25, 2017 at 18:40

1 Answer 1


I suggest to configure your tools, to use a specific network interface.

Let's say you are currently using curl to make some HTTP GET/POST to a web API endpoint. Just add the --interface option to curl, to make it use a specific interface (wlan0 or eth0 or usb0 -- use ifconfig to list all available interfaces) for the network request.

curl --interface usb0 http://checkip.amazonaws.com/

Now you could just test your main connection with --interface eth0 (assuming you are using eth0 and not WiFi) and whenever it becomes available, you start using --interface eth0 in all of your network requests. If eth0 still doesn't have connectivity, then you keep using usb0 (or the name of your 3G-dongle interface).

For wget a similar option would be --bind-address= where you have to pass the IP address of your raspberry pi for that interface. Usually you have something like this eth0 and usb0

If you are using PHP with php-curl you might want to look at CURLOPT_INTERFACE which is explained here.


curl_easy_setopt(curl, CURLOPT_URL, "http://example.com/foo.bin");
curl_easy_setopt(curl, CURLOPT_INTERFACE, "eth0"); 
ret = curl_easy_perform(curl); 

So in general: I'd avoid switching an interface on/off (using ifup / ifdown) and rather select the network interface you want to use in your code. And have some extra code which periodically tests any given interface for connectivity, you can find some ideas for that here.

  • Won't you still be dependent on your default gateway for non-local traffic? You can force it out via the destination network interface, but unless you're handing it off to a gateway (router) with a route to the destination, it won't leave your locally attached network. So long as the default gateway is not the wifi network, I'm not sure how this would work. Or am I missing something?
    – bobstro
    Commented Jul 25, 2017 at 18:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.