I wanted to use my raspberry 4 connected with ethernet and sometimes bring it to the tv where there is no ethernet cable and i have to use WLAN. In order to do it reliably and automatically, emulating what you get in desktop computers like a laptop, which disconnects to wifi when connected to ethernet and connects to wifi when there is no ethernet connected, i used ifplugd. Also, using this method you can have both interfaces with the same static ip, which may or may not work but it is up to your router (if you connect with one interface, and then disconnect it and connect with the second one, the router will probably reject the second connection or static ip assignation, as the first one, with the same ip, has been connected too recently and the router would probably think it is still connected. You would have to wait longer until connecting with the second one, or just reboot the router and connect with the second interface).
Note: this setup works reliably to change between one interface and the another, but i haven’t tested the autoconnect wifi system of dietpi, and so right now i have been using this setup without autoconnect, and for several days it has been connected without any interruption but theoretically, right now, if for some reason my raspberry got any wifi interruption or whatever, it should stay disconnected to the wlan. This hasn’t happened yet, but theoretically this setup doesn’t have a WLAN autoreconnection system running.
Okay so steps from a clean flash (modify as you want to apply it to a running system):
Clean flash, and as I know two active interfaces have been problematic to me, I went throught the initial setup only with ethernet
Then I installed ifplugd:
apt-get install ifplugd
to have it already installed in case internet connection doesnt work at some point throught the process, then i just set the internet using the dietpi-config for both ethernet, with static ip (you can use dynamic if you want), and then a reboot, and then the wifi with static (same, dynamic if wished) and both ssid, the country, the inboard wifi (in my case), and then a reboot, and then proceed to edit the config files of wpa_supplicant (it doesnt have to be modified, dietpi-config does it right, but i check it just in case, to see everything is set correctly; this file is where every wlan connection is configured, this is, the ssid and the passphrase, basically), /etc/network/interface (this file is where you write what kind of ip you want to get in each interface; for this setup, you must comment the hotplugging of eth0, and checking that both interfaces has correct static ips if you want static, or dynamic in any of those interfaces you want)
My /etc/network/interfaces file:
# Location: /etc/network/interfaces # Please modify network settings via: dietpi-config # Or create your own drop-ins in: /etc/network/interfaces.d/ # Drop-in configs source interfaces.d/* # Local auto lo iface lo inet loopback # Ethernet #allow-hotplug eth0 iface eth0 inet static address 192.168.1.10 netmask 255.255.255.0 gateway 192.168.1.1 #dns-nameservers 22.214.171.124 126.96.36.199 # WiFi allow-hotplug wlan0 iface wlan0 inet static address 192.168.1.10 netmask 255.255.255.0 gateway 192.168.1.1 #dns-nameservers 188.8.131.52 184.108.40.206 wireless-power off wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf
Check that /etc/default/crda has REGDOMAIN=ES (for Spain, for other countries you have to check http://www.summitdata.com/Documents/Regulatory_Domains.pdf ), and finally edit /etc/ifplugd/ifplugd.action to contain:
#!/bin/sh set -e if [ -z "$1" ] || [ -z "$2" ] ; then echo "Wrong arguments" > /dev/stderr exit 1 fi if [ "$1" != "eth0" ]; then echo "Wrong interface!" > /dev/stderr exit 1 fi if [ "$2" = "up" ] ; then echo "bring down WiFi" /sbin/ifdown wlan0 --force /sbin/ifup eth0 --force exit 0 elif [ "$2" = "down" ] ; then echo "bring up WiFi" /sbin/ifdown eth0 --force /sbin/ifup wlan0 --force exit 0 fi exit 1
INTERFACES="eth0" HOTPLUG_INTERFACES="auto" ARGS="-q -f -u0 -d5 -w -I" SUSPEND_ACTION="stop"
systemctl enable ifplugd.service
systemctl start ifplugd.service
and voilá, it is working