diff --git a/main.py b/main.py new file mode 100644 index 0000000..dbcaee8 --- /dev/null +++ b/main.py @@ -0,0 +1,78 @@ +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.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 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 odalar_mqtt.py + +else: + print ("There is something wrong") + + + + + diff --git a/networkconfig.py b/networkconfig.py new file mode 100644 index 0000000..95de437 --- /dev/null +++ b/networkconfig.py @@ -0,0 +1,176 @@ +import network +import socket +import ure +import time + +wlan_ap = network.WLAN(network.AP_IF) +wlan_sta = network.WLAN(network.STA_IF) + +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) + +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) + +def handle_root(client): + response_header = """ +

Wi-Fi Client Setup

+
+ + + + + + +
Wifi Name
Password
+

+
+

 

+
+
!!! your ssid and password information will save "passwd.dat" file inside of your esp module to use next time... be careful for security !!!
+
+

Some useful infos:

+ + + """ + send_response(client, response_header + response_variable + response_footer) + +def handle_configure(client, request): + match = ure.search("ssid=([^&]*)&password=(.*)", request) + + if match is None: + send_response(client, "Parameters not found", status_code=400) + return (False) + + ssid = match.group(1).replace("%3F","?").replace("%21","!") + password = match.group(2).replace("%3F","?").replace("%21","!") + if len(ssid) == 0: + send_response(client, "SSID must be provided", status_code=400) + return (False) + + if do_connect(ssid, password): + response_footer = """ + +


+

:) YES, Wi-Fi Configured to """+ssid+"""

+

""" + send_response(client, response_footer) + try: + fo = open("passwd.dat","r") + ex_data = fo.read() + fo.close() + fo = open("passwd.dat","w") + ex_data = ssid+";"+password+"\n"+ex_data + fo.write(ex_data) + fo.close() + except: + fo = open("passwd.dat","w") + fo.write(ssid+";"+password+"\n") + fo.close() + + return (True) + else: + response_footer = """ + +
+

Wi-Fi Not Configured to """+ssid+"""

+

+
+ +
+ """ + send_response(client, response_footer ) + return (False) + +def handle_not_found(client, url): + send_response(client, "Path not found: {}".format(url), status_code=404) + +def stop(): + global server_socket + + if server_socket: + server_socket.close() + +def start(port=80): + addr = socket.getaddrinfo('0.0.0.0', port)[0][-1] + + global server_socket + global wlan_sta + stop() + + server_socket = socket.socket() + server_socket.bind(addr) + server_socket.listen(1) + + print('listening on', addr) + + while True: + + if wlan_sta.isconnected(): + client.close + return (True) + + client, addr = server_socket.accept() + client.settimeout(5.0) + + print('client connected from', addr) + + request = b"" + try: + while not "\r\n\r\n" in request: + request += client.recv(512) + except OSError: + pass + + print("Request is: {}".format(request)) + if "HTTP" not in request: + # skip invalid requests + client.close() + continue + + url = ure.search("(?:GET|POST) /(.*?)(?:\\?.*?)? HTTP", request).group(1).rstrip("/") + print("URL is {}".format(url)) + + if url == "": + handle_root(client) + elif url == "configure": + handle_configure(client, request) + else: + handle_not_found(client, url) + + client.close() \ No newline at end of file