From e4cda8a95ab3397b97bf11acc01d6f0bb45243f2 Mon Sep 17 00:00:00 2001 From: Thomas Waldmann Date: Mon, 11 Dec 2017 15:28:31 +0100 Subject: [PATCH] simplify do_connect kept the behaviour it had before: returns truish/falsish if it has/has not connected to given network. returns None if it already was connected. --- main.py | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/main.py b/main.py index d2665bb..3415b3b 100644 --- a/main.py +++ b/main.py @@ -8,20 +8,21 @@ wlan_sta = network.WLAN(network.STA_IF) def do_connect(ntwrk_ssid, netwrk_pass): sta_if = network.WLAN(network.STA_IF) sta_if.active(True) - if not sta_if.isconnected(): - print('Trying to connect to %s...' % ntwrk_ssid) - sta_if.connect(ntwrk_ssid, netwrk_pass) - retry = 0 - while not sta_if.isconnected() and retry < 100: - time.sleep(0.1) - retry += 1 - print('.', end='') - if sta_if.isconnected(): - print('\nConnected. Network config: ', sta_if.ifconfig()) - return True - else: - print('\nFailed. Not Connected to: ' + ntwrk_ssid) - return False + if sta_if.isconnected(): + return None + print('Trying to connect to %s...' % ntwrk_ssid) + sta_if.connect(ntwrk_ssid, netwrk_pass) + for retry in range(100): + connected = sta_if.isconnected() + if connected: + break + time.sleep(0.1) + print('.', end='') + if connected: + print('\nConnected. Network config: ', sta_if.ifconfig()) + else: + print('\nFailed. Not Connected to: ' + ntwrk_ssid) + return connected def check_connection():