-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiotwifi.py
132 lines (103 loc) · 3.49 KB
/
iotwifi.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import network
import socket
import ujson
import uos
import ure
HTTP_RESPONSE_HEADER = """HTTP/1.1 200 OK
Connection: close
Server: IoTWiFi
Content-Type: text/html
"""
HTML_OPTION_TEMPLATE = """<option value="{0}">{0}</option>"""
DEFAULT_TITLE = 'Device Name Setup'
DEFAULT_SSID = 'MicroPythonIoT'
DEFAULT_PSK = '12345678'
def serve_ssid_page(networks, nic, title):
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.bind(('', 80))
sock.listen(0)
keep_serving = True
while keep_serving:
conn, addr = sock.accept()
request = conn.recv(1024)
conn.sendall(HTTP_RESPONSE_HEADER)
request = str(request)
# See if we have any URL params
params = ure.compile('ssid=(.*?)&psk=(.*?) HTTP')
groups = params.search(request)
try:
ssid, psk = groups.group(1), groups.group(2)
wifi_details = {
'ssid': ssid,
'psk': psk
}
# Try and connect
start_network(wifi_details, nic)
with open('wifi.json', 'w') as f:
f.write(ujson.dumps(wifi_details))
conn.close()
keep_serving = False
break
except:
print('There was an error')
# And so if there is nothing else to do, we serve the page
with open('iotwifi.htm', 'r') as html:
page = html.read()
select_values = ''
for net in networks:
select_values += HTML_OPTION_TEMPLATE.format(net)
page = page.format(
title=title,
options=select_values
)
conn.send(page)
conn.sendall('\n')
conn.close()
print('Connection closed')
def start_network(wifi_details, nic):
nic = network.WLAN(network.STA_IF)
nic.active(True)
nic.connect(wifi_details['ssid'], wifi_details['psk'])
def get_nic(title=DEFAULT_TITLE, ssid=DEFAULT_SSID, psk=DEFAULT_PSK):
# First off, scan the current WiFi networks
nic = network.WLAN(network.STA_IF)
nic.active(True)
networks = nic.scan()
nic.active(False)
network_names = [x[0].decode('unicode') for x in networks]
# Then check if there is an existing network configuration
valid_config = True
if valid_config:
try:
uos.stat('wifi.json')
except:
valid_config = False
print('No existing wifi config found')
# And then check if we can load it from the file
if valid_config:
try:
with open('wifi.json', 'r') as f:
wifi_details = ujson.loads(f.read())
except:
valid_config = False
print('Wifi config file is not valid')
# And then see if that network is around for us to talk to
if valid_config:
if wifi_details['ssid'] in network_names:
# Try and connect
try:
start_network(wifi_details, nic)
print('Connected!')
except:
valid_config = False
print('Could not connect to configured network')
else:
valid_config = False
print('Configured network not currently available')
# And if there is still no network... start the AP.
if not valid_config:
nic = network.WLAN(network.AP_IF)
nic.config(essid=ssid, password=psk,
authmode=network.AUTH_WPA_WPA2_PSK)
serve_ssid_page(network_names, nic, title)
return nic