I want to connect (via Wi-Fi) my Raspberry in a electronic equipment, in which I can access (via TCP) to data sensors. After I received that data, this electronic equipment doesn't have an Internet connection, so I can't send these data to a web server. For this, I need to connect to another Wi-Fi network (with Internet connection) to save those values in a database server. Anybody knows how can I solve this problem with a Raspberry Pi?

Solution A: If I connect two usb wifi in a Raspberry Pi, can I connect and reconnect to this different networks?

Solution B: Is an option to create a script which allows me to connect to the first networkand then connect to the network with the internet, in a "listening loop"?

  • The answer is yes to both questions -- or at least, you can add one usb wifi and use that and the onboard one simultaneously. You may notice that one or both connections slow somewhat (I have a Pi that does this regularly for a few hours, but the slow down is just a suspicion I have not investigated to confirm). WRT the script, it of course depends somewhat on on your ability to write such.
    – goldilocks
    Commented Jul 1, 2023 at 14:17
  • @goldilocks Can you please share with me the script that you developed to access to the two networks?
    – Dicarva
    Commented Jul 2, 2023 at 18:17
  • I don't think that is a good idea because: 1) I put the two interfaces in different network namespaces, which would be counter-productive in your context; part of why I did this was so that they could both provide access to the same network, ie., the interfaces are used as if they were on separate systems. You want to be able to retrieve information from one network and send it to another.
    – goldilocks
    Commented Jul 3, 2023 at 15:10
  • 2) I disabled "normal" networking on both (that is, the relevant systemd services), which was not necessary but what I normally do anyway, I use my own custom services. All that stuff is a bottomless rabbit hole. I've actually lost track of what the default networking apparatus is now on RpiOS. Is it still dhcpcd? Pretty sure I read here that they'd switched over to systemd-networkd...
    – goldilocks
    Commented Jul 3, 2023 at 15:10
  • In any case, you'd be much served by finding examples of how to configure multiple interfaces using that -- just don't include "raspberry pi" in your search terms. Search "linux dhcpcd multiple interfaces" (or "linux systemd-networkd multiple interfaces"). What you want to do is not very unusual, there should be lots of examples around.
    – goldilocks
    Commented Jul 3, 2023 at 15:11

2 Answers 2


For Wi-Fi, you could add a USB Wi-Fi adapter as you mentioned You could also enable/disable and control which Wi-Fi you are connected to via command line by using the wpa_supplicant file. Crontab could also be useful as a way of scheduling when an adapter is connected, or which SSID it is connected to and when.

A quick search for "Switching between known Wi-Fi networks with raspberry pi" brought me to a well documented page in the raspberry pi forums which could also be helpful for you.


As per my comments, this is not an unusual thing to do and there should be lots of examples around. I'm not sure what the current default networking apparatus is on RpiOS; I still use dhcpcd but via a customized service and hook scripts, and I've never used systemd-networkd, so I can't provide a concrete example. However, for dhcpcd I am sure it is as simple as adding an entry to dhcpcd.conf and I would guess something similar for networkd.

If the current networking doesn't include one of the interfaces and won't interfere (I'd guess it probably will, unfortunately, unless you configure it to ignore that interface), you can go through the process of setting up the network stack for the other one manually. This helps to illustrate the steps involved -- I'll refer to it as wlan2. This all must be done with superuser privilleges, so you need to append sudo:

  1. ip link set wlan2 up

  2. You'll need a wpa_supplicant.conf for the second network. It is easiest to do this with a separate file. Once the interface is up you can start that: wpa_supplicant -B -c /path/to/your/alt-network.conf -i wlan2.

  3. Presuming the second network uses DHCP, you'll need a DHCP client to handle this, such as dhclient or dhcpcd. The complication here is how routing is configured. You can do that via "hook scripts" for either, these are invoked when interfaces come up or otherwise change state, see the relevant man pages. If you do that you can (and probably should) start wpa_supplicant that way too.

    BUT the easier way would be to just configure a second interface in a config file, and indicate which one should provide the default route (that is, the one where general internet traffic should be handled); ip route will show you what there is currently. Glancing at man dhcpcd.conf, there's a nogateway option you can use to prevent that default route from being assigned to a specific interface.

The point about the routing is you want to want to end up with something that includes this; would be the non-default network, would be the router address for wlan0:

default via dev wlan0 dev wlan2 scope link

If this is autoconfigured the second line may be a little more complicated; that one would be produced by ip route add dev wlan2. What this means is traffic for will go through wlan2 and everything else will use wlan0.

The best thing for you to do is do a little reading/searching around WRT dhcpcd or systemd-networkd, then make a concrete attempt. If it doesn't work, you can then come here (or Unix & Linux) and ask for more specific help with it, which is much easier to get.

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.