4

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?

Piotr Kula
  • 17,336
  • 6
  • 66
  • 105

1 Answers1

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=192.168.42.20 where you have to pass the IP address of your raspberry pi for that interface. Usually you have something like this eth0 192.168.0.5 and usb0 192.168.42.20

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

Example:

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.

Eugen
  • 488
  • 3
  • 13