OPi Zero - Hybrid wifi mode or switched mode


Recommended Posts

Hello,

 

I'm try to setup my OPiZero as mediaplyer, and I need to use it sometimes as WiFi Client - Managed mode and sometimes as WiFi AP - Master mode.

 

Scenario 1

Work in "dual mode", I mean, when my wifi network within reach, then OPiZ start as "client", and connect to my preferred wifi network

when wifi network not in range, it start as AP+DHCP (hostapd and dnsmasq)

 

Scenario 2

Switched mode, Make any push button on gpio, and then make scripts "to do the job" ..

disconnect from wifi, stop wpa_supplicant, run hostapd an dnsmasq.

 

Has anybody something like "scenrio1" or "scenario2" .. ??

 

(or my ideas are totally bad ... :) )

Link to post
Share on other sites
Armbian is a community driven open source project. Do you like to contribute your code?

So ... for some time ...

 

Here is my python script, for switching one wifi intreface  ...

It is first "one-way" version ...

The OPi.GPIO library for python is needed.   https://opi-gpio.readthedocs.io/en/latest/install.html

 

#!/usr/bin/env python

import os
#import sys
import OPi.GPIO as GPIO
import time
# Set your pins
button = (7)
led = (26)

cmd_dnsmasq_stop = "systemctl stop dnsmasq.service"
cmd_dnsmasq_start = "systemctl start dnsmasq.service"
cmd_wpa_supplicant_stop = "systemctl stop wpa_supplicant.service"
cmd_wpa_supplicant_start = "systemctl start wpa_supplicant.service"
cmd_wifi_off = "nmcli con down WiFi"
cmd_wifi_on = "nmcli con up WiFi"
cmd_hostap = "hostapd /etc/hostapd.conf -B"
cmd_bridge_on = "nmcli con up Bridge"
cmd_bridge_off = "nmcli con down Bridge"
cmd_dnsmasq_ap = "dnsmasq -C /etc/dnsmasq_ap.conf"


# Placeholder variable
GPIO.setmode(GPIO.BOARD)
GPIO.setup(led, GPIO.OUT, initial=GPIO.LOW, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(button, GPIO.IN, initial=GPIO.LOW, pull_up_down=GPIO.PUD_DOWN)

#def my_callback():
#    print('This is a edge event callback function!')
#    print('Edge detected on channel %s'%button)
#    print('This is run in a different thread to your main program')

#GPIO.add_event_detect(button, GPIO.RISING, callback=my_callback, bouncetime=200)
#GPIO.add_event_callback(button, my_callback)
GPIO.wait_for_edge(button, GPIO.RISING)  # add rising edge detection on a channel
print('Button pressed')

print('Wifi Off')
os.system(cmd_wifi_off)
time.sleep(2)
print('Stop DNSMASQ')
os.system(cmd_dnsmasq_stop)
time.sleep(5)
print('Stop WPA Supplicant')
os.system(cmd_wpa_supplicant_stop)
time.sleep(2)
print('Start Bridge')
os.system(cmd_bridge_on)
time.sleep(2)
print('Start DNSMASQ AP')
os.system(cmd_dnsmasq_ap)
time.sleep(5)
print('Start HostAP')
os.system(cmd_hostap)
time.sleep(5)

GPIO.output(led, GPIO.HIGH)

#GPIO.cleanup()

 

Link to post
Share on other sites

Final version.....

 

Anybody can use their own commands for start-stop processes or for bring up-down interfaces...

#!/usr/bin/env python3

# Set your pins
button = (5)
led = (26)

#Set your commands
cmd_dnsmasq_stop = "systemctl stop dnsmasq.service"
cmd_dnsmasq_start = "systemctl start dnsmasq.service"
cmd_wpa_supplicant_stop = "systemctl stop wpa_supplicant.service"
cmd_wpa_supplicant_start = "systemctl start wpa_supplicant.service"
cmd_wifi_off = "nmcli con down WiFi"
cmd_wifi_on = "nmcli con up WiFi"
cmd_hostap_on = "hostapd /etc/hostapd.conf -B"
cmd_hostap_off = "killall hostapd"
cmd_bridge_on = "nmcli con up Bridge"
cmd_bridge_off = "nmcli con down Bridge"
cmd_dnsmasq_ap_on = "dnsmasq -C /etc/dnsmasq_ap.conf"
cmd_dnsmasq_ap_off = "killall dnsmasq"

# Main script
import os
import sys
import OPi.GPIO as GPIO
import time

client = 0
ap = 1

# Placeholder variable
GPIO.setwarnings(False) # Ignore warning for now
GPIO.setmode(GPIO.BOARD)
GPIO.setup(led, GPIO.OUT, initial=GPIO.LOW, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(button, GPIO.IN, initial=GPIO.LOW, pull_up_down=GPIO.PUD_DOWN)

state = client
while True:
   GPIO.wait_for_edge(button, GPIO.RISING) # This is a function
   os.system(cmd_wifi_off)
   time.sleep(3)
   os.system(cmd_dnsmasq_stop)
   time.sleep(5)
   os.system(cmd_wpa_supplicant_stop)
   time.sleep(5)
   os.system(cmd_bridge_on)
   time.sleep(15)
   os.system(cmd_dnsmasq_ap_on)
   time.sleep(10)
   os.system(cmd_hostap_on)
   time.sleep(5)
   GPIO.output(led, GPIO.HIGH)
   print("AP mode is started")
   state = ap
   time.sleep(5)

   if state == ap:
      GPIO.wait_for_edge(button, GPIO.RISING) # This is a function
      os.system(cmd_hostap_off)
      time.sleep(3)
      os.system(cmd_dnsmasq_ap_off)
      time.sleep(3)
      os.system(cmd_bridge_off)
      time.sleep(5)
      os.system(cmd_wpa_supplicant_start)
      time.sleep(5)
      os.system(cmd_dnsmasq_start)
      time.sleep(7)
      os.system(cmd_wifi_on)
      time.sleep(3)
      GPIO.output(led, GPIO.LOW)
      print("Client mode is strted")
      state = client
      time.sleep(5)

 

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...