def do_connect(ssid, password, tries=5): from network import WLAN, STA_IF from time import sleep import netconf print('Loading Wifi module...') sta_if = WLAN(STA_IF) if not sta_if.isconnected(): sta_if.active(True) sta_if.connect(ssid, password) for i in range(tries): print('Connecting to network (try {})...'.format(i+1)) if sta_if.isconnected(): sta_if.disconnect() sta_if.status() sta_if.ifconfig((netconf.ip, netconf.mask, netconf.gateway, netconf.dns)) sta_if.connect() curconf = sta_if.ifconfig() print('network config:', curconf) return curconf[0] sleep(1) else: print("Failed to connect in {} seconds.".format(tries)) if __name__ == '__main__': do_connect(netconf.ssid, netconf.password)