diff --git a/main.py b/main.py index ff0711f..9181aa8 100644 --- a/main.py +++ b/main.py @@ -1,77 +1,76 @@ import network import networkconfig import time + 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('try to connect : '+ntwrk_ssid+' network...') - sta_if.connect(ntwrk_ssid, netwrk_pass) - a=0 - while not sta_if.isconnected() | (a > 99) : - time.sleep(0.1) - a+=1 - print('.', end='') - if sta_if.isconnected(): - print('\nConnected. Network config:', sta_if.ifconfig()) - return (True) - else : - print('\nProblem. Not Connected to :'+ntwrk_ssid) - return (False) -def check_connection (): - global wlan_sta - # Firstly check is there any connection - if wlan_sta.isconnected(): - return (True) - try: - # connection of ESP to WiFi takes time - # wait 3 sec. and try again. - time.sleep(3) - if not wlan_sta.isconnected(): - # inside passwd file. there are list of WiFi network CSV file - f = open("passwd.dat") - data = f.readlines() - f.close() - # Search WiFi's in range - ssids_found = wlan_sta.scan() - - # matching... - for ipass in data: - ssid_list= ipass.strip("\n").split(";") - - for i in ssids_found: - if ssid_list[0] in i[0]: - print ("OK. WiFi is Founded") - if do_connect(ssid_list[0],ssid_list[1]): - return (True) - - if not wlan_sta.isconnected(): - if networkconfig.start(): - return (True) - else: - return (True) - - except OSError: - # Web server for connection manager - if networkconfig.start(): - return (True) - - return (False) - -if check_connection (): - - # Main Code is here - print ("ESP OK") - # to import your code; - # import sample_mqtt.py +def do_connect(ntwrk_ssid, netwrk_pass): + sta_if = network.WLAN(network.STA_IF) + sta_if.active(True) + if not sta_if.isconnected(): + print('try to connect : ' + ntwrk_ssid + ' network...') + sta_if.connect(ntwrk_ssid, netwrk_pass) + a = 0 + while not sta_if.isconnected() | (a > 99): + time.sleep(0.1) + a += 1 + print('.', end='') + if sta_if.isconnected(): + print('\nConnected. Network config:', sta_if.ifconfig()) + return (True) + else: + print('\nProblem. Not Connected to :' + ntwrk_ssid) + return (False) + + +def check_connection(): + global wlan_sta + # Firstly check is there any connection + if wlan_sta.isconnected(): + return (True) + try: + # connection of ESP to WiFi takes time + # wait 3 sec. and try again. + time.sleep(3) + if not wlan_sta.isconnected(): + # inside passwd file. there are list of WiFi network CSV file + f = open("passwd.dat") + data = f.readlines() + f.close() + # Search WiFi's in range + ssids_found = wlan_sta.scan() + + # matching... + for ipass in data: + ssid_list = ipass.strip("\n").split(";") + + for i in ssids_found: + if ssid_list[0] in i[0]: + print("OK. WiFi is Founded") + if do_connect(ssid_list[0], ssid_list[1]): + return (True) + + if not wlan_sta.isconnected(): + if networkconfig.start(): + return (True) + else: + return (True) + + except OSError: + # Web server for connection manager + if networkconfig.start(): + return (True) + + return (False) + + +if check_connection(): + + # Main Code is here + print("ESP OK") +# to import your code; +# import sample_mqtt.py else: - print ("There is something wrong") - - - - - + print("There is something wrong") diff --git a/networkconfig.py b/networkconfig.py index 411d577..34738f2 100644 --- a/networkconfig.py +++ b/networkconfig.py @@ -7,55 +7,58 @@ wlan_ap = network.WLAN(network.AP_IF) wlan_sta = network.WLAN(network.STA_IF) # setup for ssid -ssid_name= "WifiManager" +ssid_name = "WifiManager" ssid_password = "tayfunulu" server_socket = None + def do_connect(ntwrk_ssid, netwrk_pass): - sta_if = network.WLAN(network.STA_IF) - sta_if.active(True) - if not sta_if.isconnected(): - print('try to connect : '+ntwrk_ssid+' network...') - sta_if.active(True) - sta_if.connect(ntwrk_ssid, netwrk_pass) - a=0 - while not sta_if.isconnected() | (a > 99) : - time.sleep(0.1) - a+=1 - print('.', end='') - if sta_if.isconnected(): - print('\nConnected. Network config:', sta_if.ifconfig()) - return (True) - else : - print('\nProblem. Not Connected to :'+ntwrk_ssid) - return (False) + sta_if = network.WLAN(network.STA_IF) + sta_if.active(True) + if not sta_if.isconnected(): + print('try to connect : ' + ntwrk_ssid + ' network...') + sta_if.active(True) + sta_if.connect(ntwrk_ssid, netwrk_pass) + a = 0 + while not sta_if.isconnected() | (a > 99): + time.sleep(0.1) + a += 1 + print('.', end='') + if sta_if.isconnected(): + print('\nConnected. Network config:', sta_if.ifconfig()) + return (True) + else: + print('\nProblem. Not Connected to :' + ntwrk_ssid) + return (False) + def send_response(client, payload, status_code=200): - client.sendall("HTTP/1.0 {} OK\r\n".format(status_code)) - client.sendall("Content-Type: text/html\r\n") - client.sendall("Content-Length: {}\r\n".format(len(payload))) - client.sendall("\r\n") - - if len(payload) > 0: - client.sendall(payload) + client.sendall("HTTP/1.0 {} OK\r\n".format(status_code)) + client.sendall("Content-Type: text/html\r\n") + client.sendall("Content-Length: {}\r\n".format(len(payload))) + client.sendall("\r\n") + + if len(payload) > 0: + client.sendall(payload) + def handle_root(client): - global wlan_sta - response_header = """ + global wlan_sta + response_header = """