/
wifi.py
40 lines (37 loc) · 1.46 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
import network
import utime
def wifi_connect(essid, password):
# Connect to the wifi. Based on the example in the micropython
# documentation.
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
if not wlan.isconnected():
print('connecting to network ' + essid + '...')
wlan.connect(essid, password)
# connect() appears to be async - waiting for it to complete
while not wlan.isconnected():
print('waiting for connection...')
utime.sleep(4)
print('checking connection...')
print('Wifi connect successful, network config: %s' % repr(wlan.ifconfig()))
else:
# Note that connection info is stored in non-volatile memory. If
# you are connected to the wrong network, do an explicity disconnect()
# and then reconnect.
print('Wifi already connected, network config: %s' % repr(wlan.ifconfig()))
def wifi_disconnect():
# Disconnect from the current network. You may have to
# do this explicitly if you switch networks, as the params are stored
# in non-volatile memory.
wlan = network.WLAN(network.STA_IF)
if wlan.isconnected():
print("Disconnecting...")
wlan.disconnect()
else:
print("Wifi not connected.")
def disable_wifi_ap():
# Disable the built-in access point.
wlan = network.WLAN(network.AP_IF)
wlan.active(False)
print('Disabled access point, network status is %s' %
wlan.status())