User avatar
Posts: 3
Joined: Mon Mar 16, 2020 6:07 pm

turn on wifi if ethernet is not plugged in

Tue Mar 17, 2020 8:03 pm

Greetings all,

I want wifi turned on only if there is no ethernet plugged in because I want to use Bluetooth mouse and keyboard.
On the other hand, when I`m on the go I want the raspberry pi to connect to my phone`s wifi hotspot in headless mode (I want to use VNC remote desktop).

If wifi is turned on and connected the mouse and the keyboard stops working. (when not connected the mouse is usable).

I can turn on the wifi on every shutdown and off after starts manually, but have to be a better solution.

Thanks for any and all help!

Posts: 1640
Joined: Thu Apr 13, 2017 3:55 pm
Location: Oxford UK

Re: turn on wifi if ethernet is not plugged in

Tue Mar 17, 2020 9:17 pm

so something like

Code: Select all

if lan up
	enable bluetooth and pair keyboard/mouse
	wifi off
	disable bluetooth
	wifi on

testing if the lan is up could be done a number of ways, probably easiest to test if a static ip is assigned by the dhcp server by device mac address and just search the return string from an ifconfig command for the known IP

I would start with something like

Code: Select all

 ifconfig | grep "x.x.x.x"
wjere x.x.x.x is the static/expected lan ip address
and then check if that isnull() ir an empty string...?? ... -is-empty/

if using systemd to manage services on your puter

b/tooth on/off is

Code: Select all

sudo systemctl stop bluetooth.service

Code: Select all

sudo systemctl start bluetooth.service
add your commands to turn on/off wifi and use one of the methods to run a script at startup to execute your shell script. (crontab/rc.local..)
(remembering to make it executable and change its permissions accordingly)

agotcha to watch out for is that networking and other services can take some time to 'establish themselves' fully even though command has been released to the shell so a simple solution would be to sit a 5? second delay in your script start to give all necessary services time to start up ok..

Return to “Beginners”