Disable Wifi/BT on pi0 W

General GBZ-related chat goes here. Share ideas, tips and tricks, or ask questions that don't fit into the hardware/software help forums.
Post Reply
User avatar
hueblo
Posts: 175
Joined: Mon Jun 06, 2016 2:59 pm
Has thanked: 78 times
Been thanked: 81 times

Disable Wifi/BT on pi0 W

Post by hueblo » Tue Jun 13, 2017 2:02 pm

I commissioned someone on fiverr to come up with come code to disable wifi and bt since I do not know how to write code. The code he came up with was not quite right so @hoolyhoo came to the rescue. The code allows me to turn on the WiFi and Bluetooth using tactile buttons hooked up to GPIO pins 23,24 and any ground. Wifi and Bluetooth are booth disabled at startup by default, to have them enabled at start up change both "button_bt_status = True" and "button_wifi_status = True" to False. Make sure to follow Hoolyhoo's instructions at the top of the Wifibluetooth.py code.


To create the script type the command below in the following directory /home/pi/Script/. Copy and paste the WifiBluetooth.py code below and save and exit.

Code: Select all

sudo nano WifiBluetooth.py
WifiBluetooth.py code

Code: Select all

"""
  Before running the script please:

  a) Install the following packages:
     sudo apt-get install python-gpiozero
     sudo apt-get install rfkill

  b) Define GPIOs lines in global variables

  Running the script in command line:
  python WifiBluetooth.py

  Running script automatically on boot:
  Make and add python script to this directory:  /home/pi/Script/
  sudo nano /etc/rc.local
  Add this line before exit 0:
  python /home/pi/Script/WifiBluetooth.py &
  Save, exit and reboot
"""

import subprocess
import time
from gpiozero import Button
from signal import pause

# Global viariables
# -----------------------------
# Define button GPIOs

button_bt = Button(23, bounce_time = .3)
button_wifi = Button(24, bounce_time = .3)

button_bt_status = True
button_wifi_status = True
# -----------------------------


def button_bt_pressed():

  global button_bt_status

  print("Bluetooth button is pressed")
  button_bt_status = not button_bt_status

  if (button_bt_status == True):
    print("Turning on bluetooth")
    subprocess.Popen("sudo rfkill unblock bluetooth", shell=True)

  elif (button_bt_status == False):
    print("Turning off bluetooth")
    subprocess.Popen("sudo rfkill block bluetooth", shell=True)


def button_wifi_pressed():

  global button_wifi_status

  print("WiFi button is pressed")
  button_wifi_status = not button_wifi_status

  if (button_wifi_status == True):
    print("Turning on wifi")
    subprocess.Popen("sudo rfkill unblock wifi", shell=True)

  elif (button_wifi_status == False):
    print("Turning off wifi")
    subprocess.Popen("sudo rfkill block wifi", shell=True)

button_wifi_pressed()
button_bt_pressed()

# Attach user defined functions to buttons press event
button_bt.when_pressed = button_bt_pressed
button_wifi.when_pressed = button_wifi_pressed

pause()

User avatar
wermy
Site Admin
Posts: 1346
Joined: Tue May 03, 2016 8:51 pm
Has thanked: 620 times
Been thanked: 1322 times
Contact:

Re: Disable Wifi/BT on pi0 W

Post by wermy » Tue Jun 13, 2017 2:30 pm

You can also install rfkill ('sudo apt-get install rfkill'), and plop the block/unblock commands into their own scripts. This is what I'm doing for mintyPi, and I added menu options to retropie for turning them on and off - no need to take up GPIO inputs for it. :)
ImageImageImageImage

User avatar
hueblo
Posts: 175
Joined: Mon Jun 06, 2016 2:59 pm
Has thanked: 78 times
Been thanked: 81 times

Re: Disable Wifi/BT on pi0 W

Post by hueblo » Tue Jun 13, 2017 10:48 pm

wermy wrote:
Tue Jun 13, 2017 2:30 pm
You can also install rfkill ('sudo apt-get install rfkill'), and plop the block/unblock commands into their own scripts. This is what I'm doing for mintyPi, and I added menu options to retropie for turning them on and off - no need to take up GPIO inputs for it. :)
does it remember the previous state at start up? Or does it always have it enabled or disabled at start up?

User avatar
HoolyHoo
Posts: 766
Joined: Sat Jul 09, 2016 9:34 pm
Has thanked: 206 times
Been thanked: 741 times

Re: Disable Wifi/BT on pi0 W

Post by HoolyHoo » Wed Jun 14, 2017 5:10 am

hueblo wrote:
Tue Jun 13, 2017 10:48 pm
wermy wrote:
Tue Jun 13, 2017 2:30 pm
You can also install rfkill ('sudo apt-get install rfkill'), and plop the block/unblock commands into their own scripts. This is what I'm doing for mintyPi, and I added menu options to retropie for turning them on and off - no need to take up GPIO inputs for it. :)
does it remember the previous state at start up? Or does it always have it enabled or disabled at start up?
Can be done by reading and writing the status to a local file from within the script.

User avatar
silentjames
Posts: 98
Joined: Fri Jan 27, 2017 9:02 am
Location: Russia
Has thanked: 35 times
Been thanked: 32 times

Re: Disable Wifi/BT on pi0 W

Post by silentjames » Wed Jun 14, 2017 12:18 pm

This script starts the services or stop them (if the start with system by default), am I right?
Would be great to have the script for turning BT and wifi on and off just only with one button :roll:

User avatar
HoolyHoo
Posts: 766
Joined: Sat Jul 09, 2016 9:34 pm
Has thanked: 206 times
Been thanked: 741 times

Re: Disable Wifi/BT on pi0 W

Post by HoolyHoo » Wed Jun 14, 2017 12:42 pm

The above script I fixed for @hueblo will enable and disable wifi and Bluetooth individually on two separate buttons because it's how @hueblo wanted to run it. Everything needed to adapt the script to one button or no button is there. The important discovery is the use of rfkill which blocks and unblocks the services. My preference is to add a script to the retropie menu that will enable and disable the service and remember the status. This way no extra button or hot keys are needed. ;)

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest