39 lines
1.1 KiB
Python
39 lines
1.1 KiB
Python
import network
|
|
from time import sleep
|
|
|
|
def connect(ssid, password, ip, gateway):
|
|
if ssid is None or password is None:
|
|
print("Missing ssid or password")
|
|
return None
|
|
try:
|
|
sta_if = network.WLAN(network.STA_IF)
|
|
if ip is not None and gateway is not None:
|
|
sta_if.ifconfig((ip, '255.255.255.0', gateway, '1.1.1.1'))
|
|
if not sta_if.isconnected():
|
|
print('connecting to network...')
|
|
sta_if.active(True)
|
|
sta_if.connect(ssid, password)
|
|
sleep(0.1)
|
|
if sta_if.isconnected():
|
|
return sta_if.ifconfig()
|
|
return None
|
|
return sta_if.ifconfig()
|
|
except Exception as e:
|
|
print(f"Failed to connect to wifi {e}")
|
|
return None
|
|
|
|
|
|
def ap(ssid, password):
|
|
ap_if = network.WLAN(network.AP_IF)
|
|
ap_mac = ap_if.config('mac')
|
|
print(ssid)
|
|
ap_if.active(True)
|
|
ap_if.config(essid=ssid, password=password)
|
|
ap_if.active(False)
|
|
ap_if.active(True)
|
|
print(ap_if.ifconfig())
|
|
|
|
def get_mac():
|
|
ap_if = network.WLAN(network.AP_IF)
|
|
return ap_if.config('mac')
|