MicroPython 链接WiFi ESP32连WiFi

这里---->>>> MicroPython 教程写的非常好强烈推荐

import network
import socket
import time

SSID = "abc"  #修改为你的WiFi名称
PASSWORD = "123456789"  #修改为你WiFi密码
port = 10000  #端口号
wlan = None  #wlan
listenSocket = None  #套接字

#连接WiFi
def connectWifi(ssid,passwd):   
  global wlan
  wlan = network.WLAN(network.STA_IF) 
  wlan.active(True)   #激活网络
  wlan.disconnect()   #断开WiFi连接
  wlan.connect(ssid, passwd)   #连接WiFi
  while(wlan.ifconfig()[0] == '0.0.0.0'):   #等待连接
    time.sleep(1)
  return True

#Catch exceptions,stop program if interrupted accidentally in the 'try'
connectWifi(SSID,PASSWORD)
ip = wlan.ifconfig()[0]   #获取IP地址
print(ip)
wlan.disconnect()   #断开WiFi

MicroPython 链接WiFi ESP32连WiFi_第1张图片

你可能感兴趣的:(Micropython,esp)