kopia lustrzana https://github.com/tayfunulu/WiFiManager
move most code to networkconfig module, simplify main
also: only initialize 2 WLAN instances (one AP, one STA)pull/3/head
rodzic
ff4ad2524a
commit
3ae68033e6
63
main.py
63
main.py
|
@ -1,61 +1,12 @@
|
||||||
import network
|
|
||||||
import networkconfig
|
import networkconfig
|
||||||
import time
|
|
||||||
|
|
||||||
wlan_sta = network.WLAN(network.STA_IF)
|
|
||||||
|
|
||||||
|
|
||||||
def check_connection():
|
wlan = networkconfig.check_connection()
|
||||||
global wlan_sta
|
if wlan is None:
|
||||||
# First check if there already is any connection:
|
print("Could not initialize the network connection.")
|
||||||
if wlan_sta.isconnected():
|
while True:
|
||||||
return True
|
pass # you shall not pass :D
|
||||||
|
|
||||||
connected = False
|
|
||||||
try:
|
|
||||||
# ESP connecting to WiFi takes time, wait a bit and try again:
|
|
||||||
time.sleep(3)
|
|
||||||
if wlan_sta.isconnected():
|
|
||||||
return True
|
|
||||||
|
|
||||||
# Read known network profiles from file
|
|
||||||
profiles = networkconfig.read_profiles()
|
|
||||||
|
|
||||||
# Search WiFis in range
|
|
||||||
networks = wlan_sta.scan()
|
|
||||||
|
|
||||||
AUTHMODE = {0: "open", 1: "WEP", 2: "WPA-PSK", 3: "WPA2-PSK", 4: "WPA/WPA2-PSK"}
|
|
||||||
for ssid, bssid, channel, rssi, authmode, hidden in sorted(networks, key=lambda x: x[3], reverse=True):
|
|
||||||
ssid = ssid.decode('utf-8')
|
|
||||||
encrypted = authmode > 0
|
|
||||||
print("ssid: %s chan: %d rssi: %d authmode: %s" % (ssid, channel, rssi, AUTHMODE.get(authmode, '?')))
|
|
||||||
if encrypted:
|
|
||||||
if ssid in profiles:
|
|
||||||
password = profiles[ssid]
|
|
||||||
connected = networkconfig.do_connect(ssid, password)
|
|
||||||
else:
|
|
||||||
print("skipping unknown encrypted network")
|
|
||||||
else: # open
|
|
||||||
connected = networkconfig.do_connect(ssid, None)
|
|
||||||
if connected:
|
|
||||||
break
|
|
||||||
|
|
||||||
except OSError:
|
|
||||||
pass
|
|
||||||
|
|
||||||
# start web server for connection manager:
|
|
||||||
if not connected:
|
|
||||||
connected = networkconfig.start()
|
|
||||||
|
|
||||||
return connected
|
|
||||||
|
|
||||||
|
|
||||||
if check_connection():
|
# Main Code goes here, wlan is a working network.WLAN(STA_IF) instance.
|
||||||
|
print("ESP OK")
|
||||||
# Main Code is here
|
|
||||||
print("ESP OK")
|
|
||||||
# to import your code;
|
|
||||||
# import sample_mqtt.py
|
|
||||||
|
|
||||||
else:
|
|
||||||
print("There is something wrong.")
|
|
||||||
|
|
|
@ -3,18 +3,64 @@ import socket
|
||||||
import ure
|
import ure
|
||||||
import time
|
import time
|
||||||
|
|
||||||
wlan_ap = network.WLAN(network.AP_IF)
|
|
||||||
wlan_sta = network.WLAN(network.STA_IF)
|
|
||||||
|
|
||||||
# SSID/Password for setup
|
# SSID/Password for setup
|
||||||
ssid_name = "WifiManager"
|
ssid_name = "WifiManager"
|
||||||
ssid_password = "tayfunulu"
|
ssid_password = "tayfunulu"
|
||||||
|
|
||||||
server_socket = None
|
|
||||||
|
|
||||||
# list of WiFi networks (CSV format: ssid,password)
|
# list of WiFi networks (CSV format: ssid,password)
|
||||||
NETWORK_PROFILES = 'passwd.dat'
|
NETWORK_PROFILES = 'passwd.dat'
|
||||||
|
|
||||||
|
wlan_ap = network.WLAN(network.AP_IF)
|
||||||
|
wlan_sta = network.WLAN(network.STA_IF)
|
||||||
|
|
||||||
|
server_socket = None
|
||||||
|
|
||||||
|
|
||||||
|
def check_connection():
|
||||||
|
global wlan_sta
|
||||||
|
|
||||||
|
# First check if there already is any connection:
|
||||||
|
if wlan_sta.isconnected():
|
||||||
|
return wlan_sta
|
||||||
|
|
||||||
|
connected = False
|
||||||
|
try:
|
||||||
|
# ESP connecting to WiFi takes time, wait a bit and try again:
|
||||||
|
time.sleep(3)
|
||||||
|
if wlan_sta.isconnected():
|
||||||
|
return wlan_sta
|
||||||
|
|
||||||
|
# Read known network profiles from file
|
||||||
|
profiles = read_profiles()
|
||||||
|
|
||||||
|
# Search WiFis in range
|
||||||
|
networks = wlan_sta.scan()
|
||||||
|
|
||||||
|
AUTHMODE = {0: "open", 1: "WEP", 2: "WPA-PSK", 3: "WPA2-PSK", 4: "WPA/WPA2-PSK"}
|
||||||
|
for ssid, bssid, channel, rssi, authmode, hidden in sorted(networks, key=lambda x: x[3], reverse=True):
|
||||||
|
ssid = ssid.decode('utf-8')
|
||||||
|
encrypted = authmode > 0
|
||||||
|
print("ssid: %s chan: %d rssi: %d authmode: %s" % (ssid, channel, rssi, AUTHMODE.get(authmode, '?')))
|
||||||
|
if encrypted:
|
||||||
|
if ssid in profiles:
|
||||||
|
password = profiles[ssid]
|
||||||
|
connected = do_connect(ssid, password)
|
||||||
|
else:
|
||||||
|
print("skipping unknown encrypted network")
|
||||||
|
else: # open
|
||||||
|
connected = do_connect(ssid, None)
|
||||||
|
if connected:
|
||||||
|
break
|
||||||
|
|
||||||
|
except OSError:
|
||||||
|
pass
|
||||||
|
|
||||||
|
# start web server for connection manager:
|
||||||
|
if not connected:
|
||||||
|
connected = start()
|
||||||
|
|
||||||
|
return wlan_sta if connected else None
|
||||||
|
|
||||||
|
|
||||||
def read_profiles():
|
def read_profiles():
|
||||||
with open(NETWORK_PROFILES) as f:
|
with open(NETWORK_PROFILES) as f:
|
||||||
|
@ -35,20 +81,20 @@ def write_profiles(profiles):
|
||||||
|
|
||||||
|
|
||||||
def do_connect(ssid, password):
|
def do_connect(ssid, password):
|
||||||
sta_if = network.WLAN(network.STA_IF)
|
global wlan_sta
|
||||||
sta_if.active(True)
|
wlan_sta.active(True)
|
||||||
if sta_if.isconnected():
|
if wlan_sta.isconnected():
|
||||||
return None
|
return None
|
||||||
print('Trying to connect to %s...' % ssid)
|
print('Trying to connect to %s...' % ssid)
|
||||||
sta_if.connect(ssid, password)
|
wlan_sta.connect(ssid, password)
|
||||||
for retry in range(100):
|
for retry in range(100):
|
||||||
connected = sta_if.isconnected()
|
connected = wlan_sta.isconnected()
|
||||||
if connected:
|
if connected:
|
||||||
break
|
break
|
||||||
time.sleep(0.1)
|
time.sleep(0.1)
|
||||||
print('.', end='')
|
print('.', end='')
|
||||||
if connected:
|
if connected:
|
||||||
print('\nConnected. Network config: ', sta_if.ifconfig())
|
print('\nConnected. Network config: ', wlan_sta.ifconfig())
|
||||||
else:
|
else:
|
||||||
print('\nFailed. Not Connected to: ' + ssid)
|
print('\nFailed. Not Connected to: ' + ssid)
|
||||||
return connected
|
return connected
|
||||||
|
|
Ładowanie…
Reference in New Issue