MicroPython的ESP32开发
-
Thonny安装
-
Thonny烧录MicroPython固件(Run-配置解释器-安装或更新MicroPython-选择本地固件.bin)
代码测试
点亮LED灯
# 点亮BLUE LED
from machine import Pin
pin2 = Pin(2, Pin.OUT)
pin2.value(1)
闪烁LED
import machine
import time
pin2 = machine.Pin(2, machine.Pin.OUT)
while True:
pin2.value(1)
time.sleep(1)
pin2.value(0)
time.sleep(1)
PWM亮度控制
from machine import Pin, PWM
import time
led2 = PWM(Pin(2))
led2.freq(1000)
led2.duty(100)
PWM呼吸灯
from machine import Pin, PWM
import time
led2 = PWM(Pin(2))
led2.freq(1000)
while True:
for i in range(0, 1024):
led2.duty(i)
time.sleep_ms(1)
for i in range(1023, -1, -1):
led2.duty(i)
time.sleep_ms(1)
WIFI
代码连接
def do_connect():
import network
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
if not wlan.isconnected():
print('connecting to network...')
wlan.connect('ssid', 'key')
while not wlan.isconnected():
pass
print('network config:', wlan.ifconfig())
shell 命令
# Connect Wifi
import network
wlan = network.WLAN(network.STA_IF) # create station interface
wlan.active(True) # activate the interface
wlan.scan() # scan for access points
wlan.isconnected() # check if the station is connected to an AP
wlan.connect('essid', 'pwd') # connect to an AP
wlan.config('mac') # get the interface's MAC address
wlan.ifconfig() # get the interface's IP/netmask/gw/DNS addresses
import network
wlan.isconnected()
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
True
wlan.scan()
[(b'ASUS', b'D\xf9q\x93>o', 11, -55, 4, False), (b'ASUS', b' e\x8e-I\xf4', 11, -63, 4, False), (b'CMCC-6u6c', b'H\x1ff\xaef\xd9', 1, -82, 4, False)]