注意事项:
1.WIFI 名字写错会报错“OSError: Wifi Internal Error”
2.WIFI不要使用5G频率,否则运行报错,要使用2.4G频率。
3.运行代码前,请将WiFi连接信息替换为你自己的WiFi配置。
from machine import Pin, SPI
import time
import network
import machine
def do_connect(): # 连接无线
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
if not wlan.isconnected():
print('connecting to network...')
# WIFI名字和密码
# WIFI 名字写错会报错“OSError: Wifi Internal Error”
# 电脑WIFI不要使用5G频率,否则运行报错
wlan.connect('******', '********') # 请换成你的WIFI信息
i = 1
while not wlan.isconnected():
print("正在链接中...{}".format(i))
i += 1
time.sleep(1)
print('network config:', wlan.ifconfig())
return wlan.ifconfig()[0]
def main():
# 1. 链接wifi
ip = do_connect()
print("ip地址是:", ip)
# 2.连接成功后,点亮蓝色LED灯
pin2 = Pin(2, Pin.OUT)
pin2.value(1)
if __name__ == "__main__":
main()
官方文档地址:Quick reference for the ESP32 — MicroPython latest documentation