I would like to write a script in which it keeps on trying to connect to a network, even if it is not available so as soon at it becomes available it will connect to it. This is what I have now
def connect_to_yeelight(ssid, iface):
sys.setrecursionlimit(2000)
iface_channel = f"sudo iwconfig {iface} channel 6"
os.system(iface_channel)
connect_yeelight_cmd = f"nmcli d wifi connect {ssid} ifname {iface} > /dev/null 2>&1"
def try_connection():
if os.system(connect_yeelight_cmd) != 0:
try_connection()
time.sleep(1)
else:
return True
try_connection()
as you can probably tell with this code I get a "RecursionError: maximum recursion depth exceeded in comparison". Is there any other way I could achieve such script, I feel like Im looking at this from a wrong angle.
First use can us the sleep() or schedule functionality to achieve the continues call of your function.
I wrote a function that executes the console commands and returns you the output:
Now you just need a function that connects you if the wifi is availiable. Note that you can only call the
nmcli rescan function
each 10 to 20 seconds - otherwise it will just refuse to scan anything for the next time. Thats why i recommend scanning withiw module
.now you just have to call it frequently.... e.g.: