This forum uses cookies
This forum makes use of cookies to store your login information if you are registered, and your last visit if you are not. Cookies are small text documents stored on your computer; the cookies set by this forum can only be used on this website and pose no security risk. Cookies on this forum also track the specific topics you have read and when you last read them. Please confirm whether you accept or reject these cookies being set.

A cookie will be stored in your browser regardless of choice to prevent you being asked this question again. You will be able to change your cookie settings at any time using the link in the footer.

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
button + led for wifi on/off
#1
Sorry, I don't mean to bombard folks with questions. I'm just trying to get everything setup.

To conserve power I am trying to create a button that will turn off and on the wifiAP.

Someone with python 3 knowledge showed me how to and created this code:

Code:
import subprocess
from gpiozero import LED, Button
from signal import pause

led1 = LED(18)
button1 = Button(23)

def wifionoff():
 retcode = subprocess.call([`sudo`, `systemctl`, `-q`, `is-active`, `hostap`])
 if retcode == 0:
   # switch LED off
   led1.off()
   # switch wifi off
   subprocess.call([`sudo`, `systemctl`, `stop`, `isc-dhcp-server`])
   subprocess.call([`sudo`, `systemctl`, `stop`, `hostap`])
   subprocess.call([`sudo`, `ifdown`, `wlan0`])
   subprocess.call([`sudo`, `rfkill`, `block`, `0`])
 else:
   # switch wifi on
   subprocess.call([`sudo`, `rfkill`, `unblock`, `0`])
   subprocess.call([`sudo`, `ifup`, `wlan0`])
   subprocess.call([`sudo`, `systemctl`, `start`, `hostap`])
   subprocess.call([`sudo`, `systemctl`, `start`, `isc-dhcp-server`])
   # switch LED on
   led1.on()

button1.when_pressed = wifionoff
pause()

However it seems to always think the AP is off and thus the button always tries to turn it on. I assume this is something to do with how OpenPlotter turns on AP. Though I am not ruling out a different error in the code. Any help to get this working would be great.
Reply
#2
(2017-08-19, 08:24 PM)Jonny Wrote: Sorry, I don't mean to bombard folks with questions. I'm just trying to get everything setup.

To conserve power I am trying to create a button that will turn off and on the wifiAP.

Someone with python 3 knowledge showed me how to and created this code:

Code:
import subprocess
from gpiozero import LED, Button
from signal import pause

led1 = LED(18)
button1 = Button(23)

def wifionoff():
 retcode = subprocess.call([`sudo`, `systemctl`, `-q`, `is-active`, `hostap`])
 if retcode == 0:
   # switch LED off
   led1.off()
   # switch wifi off
   subprocess.call([`sudo`, `systemctl`, `stop`, `isc-dhcp-server`])
   subprocess.call([`sudo`, `systemctl`, `stop`, `hostap`])
   subprocess.call([`sudo`, `ifdown`, `wlan0`])
   subprocess.call([`sudo`, `rfkill`, `block`, `0`])
 else:
   # switch wifi on
   subprocess.call([`sudo`, `rfkill`, `unblock`, `0`])
   subprocess.call([`sudo`, `ifup`, `wlan0`])
   subprocess.call([`sudo`, `systemctl`, `start`, `hostap`])
   subprocess.call([`sudo`, `systemctl`, `start`, `isc-dhcp-server`])
   # switch LED on
   led1.on()

button1.when_pressed = wifionoff
pause()

However it seems to always think the AP is off and thus the button always tries to turn it on. I assume this is something to do with how OpenPlotter turns on AP. Though I am not ruling out a different error in the code. Any help to get this working would be great.

There's a "turn off Wifi access point"  in the actions options  in openplotter, you might be able to  assign a GPIO pin as a digital input and toggle the access point using actions.
Reply
#3
Sorry for bringing up an old post. I just wanted to say thank you! While I wasn't able to work out how to use it as a toggle action, I did manage to set up two different buttons to do the job.

Thanks again!

P.S. Sorry for the delay, I've been working on my boat 24/7 at the moment. I've been planing a big sail but the boat I'm rather behind and freaking out Tongue
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)