├── .gitignore ├── LICENSE ├── README.md ├── WiFi_Manager.png ├── main.py ├── package.json └── wifimgr.py /.gitignore: -------------------------------------------------------------------------------- 1 | .idea/ 2 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 Tayfun ULU 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # WiFi Manager 2 | 3 | Lang : Micropython 4 | Tested : 1.8 and 1.9.3 5 | 6 | Description : WiFi manager for ESP8266 - ESP12 - ESP32 for micropython 7 | 8 | Main Features: 9 | 10 | - Web based connection manager 11 | - Save wifi password in "wifi.dat" (csv format) 12 | - Easy to apply 13 | 14 | Usage: 15 | 16 | Upload main.py and wifimgr.py to ESP. 17 | Write your code into main.py or import it from main.py. 18 | 19 | Logic: 20 | 1. step: Check "wifi.dat" file and try saved networks/passwords. 21 | 2. step: Publish web page to configure new wifi. 22 | 3. step: Save network/password to "wifi.dat" file. 23 | 4. step: Run user code. 24 | 25 |  26 | 27 | **web server based on code of CPOPP - https://github.com/cpopp/MicroPythonSamples 28 | -------------------------------------------------------------------------------- /WiFi_Manager.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tayfunulu/WiFiManager/552bfecd457a4cb4e2be93b7c698baf1751261ae/WiFi_Manager.png -------------------------------------------------------------------------------- /main.py: -------------------------------------------------------------------------------- 1 | import wifimgr 2 | 3 | 4 | wlan = wifimgr.get_connection() 5 | if wlan is None: 6 | print("Could not initialize the network connection.") 7 | while True: 8 | pass # you shall not pass :D 9 | 10 | 11 | # Main Code goes here, wlan is a working network.WLAN(STA_IF) instance. 12 | print("ESP OK") 13 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "urls": [ 3 | ["wifimgr.py", "github:tayfunulu/WiFiManager/wifimgr.py"] 4 | ], 5 | "version": "1.0.0", 6 | "deps": [] 7 | } -------------------------------------------------------------------------------- /wifimgr.py: -------------------------------------------------------------------------------- 1 | import network 2 | import socket 3 | import ure 4 | import time 5 | 6 | ap_ssid = "WifiManager" 7 | ap_password = "tayfunulu" 8 | ap_authmode = 3 # WPA2 9 | 10 | NETWORK_PROFILES = 'wifi.dat' 11 | 12 | wlan_ap = network.WLAN(network.AP_IF) 13 | wlan_sta = network.WLAN(network.STA_IF) 14 | 15 | server_socket = None 16 | 17 | 18 | def get_connection(): 19 | """return a working WLAN(STA_IF) instance or None""" 20 | 21 | # First check if there already is any connection: 22 | if wlan_sta.isconnected(): 23 | return wlan_sta 24 | 25 | connected = False 26 | try: 27 | # ESP connecting to WiFi takes time, wait a bit and try again: 28 | time.sleep(3) 29 | if wlan_sta.isconnected(): 30 | return wlan_sta 31 | 32 | # Read known network profiles from file 33 | profiles = read_profiles() 34 | 35 | # Search WiFis in range 36 | wlan_sta.active(True) 37 | networks = wlan_sta.scan() 38 | 39 | AUTHMODE = {0: "open", 1: "WEP", 2: "WPA-PSK", 3: "WPA2-PSK", 4: "WPA/WPA2-PSK"} 40 | for ssid, bssid, channel, rssi, authmode, hidden in sorted(networks, key=lambda x: x[3], reverse=True): 41 | ssid = ssid.decode('utf-8') 42 | encrypted = authmode > 0 43 | print("ssid: %s chan: %d rssi: %d authmode: %s" % (ssid, channel, rssi, AUTHMODE.get(authmode, '?'))) 44 | if encrypted: 45 | if ssid in profiles: 46 | password = profiles[ssid] 47 | connected = do_connect(ssid, password) 48 | else: 49 | print("skipping unknown encrypted network") 50 | else: # open 51 | connected = do_connect(ssid, None) 52 | if connected: 53 | break 54 | 55 | except OSError as e: 56 | print("exception", str(e)) 57 | 58 | # start web server for connection manager: 59 | if not connected: 60 | connected = start() 61 | 62 | return wlan_sta if connected else None 63 | 64 | 65 | def read_profiles(): 66 | with open(NETWORK_PROFILES) as f: 67 | lines = f.readlines() 68 | profiles = {} 69 | for line in lines: 70 | ssid, password = line.strip("\n").split(";") 71 | profiles[ssid] = password 72 | return profiles 73 | 74 | 75 | def write_profiles(profiles): 76 | lines = [] 77 | for ssid, password in profiles.items(): 78 | lines.append("%s;%s\n" % (ssid, password)) 79 | with open(NETWORK_PROFILES, "w") as f: 80 | f.write(''.join(lines)) 81 | 82 | 83 | def do_connect(ssid, password): 84 | wlan_sta.active(True) 85 | if wlan_sta.isconnected(): 86 | return None 87 | print('Trying to connect to %s...' % ssid) 88 | wlan_sta.connect(ssid, password) 89 | for retry in range(200): 90 | connected = wlan_sta.isconnected() 91 | if connected: 92 | break 93 | time.sleep(0.1) 94 | print('.', end='') 95 | if connected: 96 | print('\nConnected. Network config: ', wlan_sta.ifconfig()) 97 | 98 | else: 99 | print('\nFailed. Not Connected to: ' + ssid) 100 | return connected 101 | 102 | 103 | def send_header(client, status_code=200, content_length=None ): 104 | client.sendall("HTTP/1.0 {} OK\r\n".format(status_code)) 105 | client.sendall("Content-Type: text/html\r\n") 106 | if content_length is not None: 107 | client.sendall("Content-Length: {}\r\n".format(content_length)) 108 | client.sendall("\r\n") 109 | 110 | 111 | def send_response(client, payload, status_code=200): 112 | content_length = len(payload) 113 | send_header(client, status_code, content_length) 114 | if content_length > 0: 115 | client.sendall(payload) 116 | client.close() 117 | 118 | 119 | def handle_root(client): 120 | wlan_sta.active(True) 121 | ssids = sorted(ssid.decode('utf-8') for ssid, *_ in wlan_sta.scan()) 122 | send_header(client) 123 | client.sendall("""\ 124 | 125 |
155 |