OpenMarine

Full Version: button + led for wifi on/off
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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.
(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.
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