def get_wifi_status(): nm.init() wifi_connected = nm.getStatus() nm.disconnect() print("start to connect " , wifiSsid) nm.connect(wifiSsid, wifiPassword) # 连接到指定的路由器(路由器名称为wifiSsid, 密码为:wifiPassword) while True : if wifi_connected == 5: # nm.getStatus()返回5代表连线成功 break else: wifi_connected = nm.getStatus() # 获取Wi-Fi连接路由器的状态信息 utime.sleep(0.5) print("wifi_connected:", wifi_connected) # utime.sleep(5) print("Wi-Fi connected") print('DeviceIP:' + nm.getInfo()['ip']) # 打印Wi-Fi的IP地址信息
def getInfo(self): """ 获取当前网络信息 :param 空: :returns: 返回一个字典,字典信息如下 .. list-table:: * - key名称 - value类型 * - SSID - 字符串 * - IP - 字符串 * - MAC - 字符串 * - RSSI - Int """ return nm.getInfo()
import sys nm.init() connected = nm.getStatus() def on_wifi_connected(status): global connected print('*******wifi connected*********') connected = True if not connected: nm.register_call_back(1,on_wifi_connected) if(len(sys.argv) == 3): nm.connect(sys.argv[1],sys.argv[2]) else: nm.connect("KIDS","12345678") while True : if connected: break else: print('Wait for wifi connected') time.sleep(1) if nm.getStatus(): print('DeviceIP:' + nm.getInfo()['IP']) else: print('DeviceIP:get failed') print("ConnectWifi finished")