-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathwifi.py
254 lines (197 loc) · 6.29 KB
/
wifi.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
import network
import time
import asyncio
import settings
import tildagon_helpers
_STA_IF = network.WLAN(network.STA_IF)
_AP_IF = network.WLAN(network.AP_IF)
DEFAULT_CONNECT_TIMEOUT = 20
DEFAULT_TX_POWER = 80
DEFAULT_SSID = "emf2024"
DEFAULT_USERNAME = "badge"
DEFAULT_PASSWORD = "badge"
WIFI_AUTH_OPEN = 0
WIFI_AUTH_WEP = 1
WIFI_AUTH_WPA_PSK = 2
WIFI_AUTH_WPA2_PSK = 3
WIFI_AUTH_WPA_WPA2_PSK = 4
WIFI_AUTH_WPA2_ENTERPRISE = 5
WIFI_AUTH_WPA3_PSK = 6
WIFI_AUTH_WPA2_WPA3_PSK = 7
WIFI_AUTH_WAPI_PSK = 8
def get_default_ssid():
return settings.get("wifi_ssid", DEFAULT_SSID)
def get_default_username():
val = settings.get("wifi_wpa2ent_username", None)
if val is None and get_default_ssid() == DEFAULT_SSID:
# In case of settings.json that doesn't specify wifi_wpa2ent_username
val = DEFAULT_USERNAME
return val
def get_default_password():
return settings.get("wifi_password", DEFAULT_PASSWORD)
def get_sta_status():
return _STA_IF.status()
def get_ssid():
if ssid := _STA_IF.config("essid"):
return ssid
else:
return get_default_ssid()
def get_ip():
if status():
return ifconfig()[0]
else:
return None
def accesspoint_get_ip():
if accesspoint_status():
return accesspoint_ifconfig()[0]
else:
return None
def active():
return _STA_IF.active()
def get_connection_timeout():
return settings.get("wifi_connection_timeout", DEFAULT_CONNECT_TIMEOUT)
def save_defaults(ssid, password, username):
settings.set("wifi_ssid", ssid)
settings.set("wifi_wpa2ent_username", username)
settings.set("wifi_password", password)
settings.save()
# Rest of file is adapted from https://github.com/badgeteam/ESP32-platform-firmware/blob/master/firmware/python_modules/shared/wifi.py
# See license info https://github.com/badgeteam/ESP32-platform-firmware#license-and-information
# STATION MODE
# ------------
def connect(ssid=None, password=None, username=None):
"""
Connect to a WiFi network
:param ssid: optional, ssid of network to connect to
:param password: optional, password of network to connect to
:param username: optional, WPA2-Enterprise username of network to connect to
"""
_STA_IF.active(True)
# 20 = 5 dBm according to https://docs.espressif.com/projects/esp-idf/en/latest/esp32s3/api-reference/network/esp_wifi.html?highlight=esp_wifi_set_max_tx_power#_CPPv425esp_wifi_set_max_tx_power6int8_t
# Anything above 8 dBm causes too much interference in the crystal circuit
# which basically breaks all ability to transmit
tildagon_helpers.esp_wifi_set_max_tx_power(
settings.get("wifi_tx_power", DEFAULT_TX_POWER)
)
if not ssid:
ssid = get_default_ssid()
username = get_default_username()
password = get_default_password()
if username:
tildagon_helpers.esp_wifi_sta_wpa2_ent_set_identity(username)
tildagon_helpers.esp_wifi_sta_wpa2_ent_set_username(username)
tildagon_helpers.esp_wifi_sta_wpa2_ent_set_password(password)
password = None # Don't pass to WLAN.connect()
tildagon_helpers.esp_wifi_sta_wpa2_ent_enable(username is not None)
_STA_IF.connect(ssid, password)
def disconnect():
"""
Disconnect from the WiFi network
"""
if _STA_IF.status() != network.STAT_IDLE:
_STA_IF.disconnect()
def stop():
"""
Disconnect from the WiFi network and disable the station interface
"""
disconnect()
_STA_IF.active(False)
def status():
"""
Connection status of the station interface
:return: boolean, connected
"""
return _STA_IF.isconnected()
def wait(duration=None):
"""
Wait until connection has been made to a network using the station interface
:return: boolean, connected
"""
if duration is None:
duration = get_connection_timeout()
t = duration
while not status():
if t <= 0:
break
t -= 1
time.sleep(1)
return status()
async def async_wait(duration=None):
"""
Wait until connection has been made to a network using the station interface
:return: boolean, connected
"""
if duration is None:
duration = get_connection_timeout()
t = duration
while not status():
if t <= 0:
break
t -= 1
asyncio.sleep(1)
return status()
def scan():
"""
Scan for WiFi networks
:return: list, wifi networks [SSID, BSSID, CHANNEL, RSSI, AUTHMODE1, AUTHMODE2, HIDDEN]
"""
_STA_IF.active(True)
return _STA_IF.scan()
def ifconfig(newConfig=None):
"""
Get or set the interface configuration of the station interface
:return: tuple, (ip, subnet, gateway, dns)
"""
if newConfig:
return _STA_IF.ifconfig(newConfig)
else:
return _STA_IF.ifconfig()
# ACCESS POINT MODE
# -----------------
def accesspoint_start(ssid, password=None):
"""
Create a WiFi access point
:param ssid: SSID of the network
:param password: Password of the network (optional)
"""
if password and len(password) < 8:
raise Exception("Password too short: must be at least 8 characters long")
_AP_IF.active(True)
if password:
_AP_IF.config(essid=ssid, authmode=network.AUTH_WPA2_PSK, password=password)
else:
_AP_IF.config(essid=ssid, authmode=network.AUTH_OPEN)
def accesspoint_status():
"""
Accesspoint status
:return: boolean, active
"""
return _AP_IF.active()
def accesspoint_stop():
"""
Disable the accesspoint
"""
_AP_IF.active(False)
def accesspoint_ifconfig(newConfig=None):
"""
Get or set the interface configuration of the accesspoint interface
:return: tuple, (ip, subnet, gateway, dns)
"""
if newConfig:
return _AP_IF.ifconfig(newConfig)
else:
return _AP_IF.ifconfig()
# EXTRAS
# -----------------
# NOTE(tomsci): RTC.ntp_sync() not a thing in our micropython version
# def ntp(onlyIfNeeded=True, server='pool.ntp.org'):
# '''
# Synchronize the system clock with NTP
# :return: boolean, synchronized
# '''
# if onlyIfNeeded and time.time() > 1482192000:
# return True #RTC is already set, sync not needed
# rtc = machine.RTC()
# if not status():
# return False # Not connected to a WiFi network
# return rtc.ntp_sync(server)