(官方文档:micropython.org)
点亮LED灯:
from machine import Pin
p0 = Pin(0, Pin.OUT) # create output pin on GPIO0
p0.on() # set pin to "on" (high) level
p0.off() # set pin to "off" (low) level
p0.value(1) # set pin to on/high
p2 = Pin(2, Pin.IN) # create input pin on GPIO2
print(p2.value()) # get value, 0 or 1
p4 = Pin(4, Pin.IN, Pin.PULL_UP) # enable internal pull-up resistor
p5 = Pin(5, Pin.OUT, value=1) # set pin high on creation
p6 = Pin(6, Pin.OUT, drive=Pin.DRIVE_3) # set maximum drive strength
通过文档知道,想要让一个引脚输出高电平,只需要找到对应的GPIO然后通过on()
或者value(1)
操作就可以,同理如果想要输出低电平让LED灯灭,只需要调用off()
或者value(0)
Pin(2, Pin.OUT)
中的2对应的是GPIO2,如果你的电路板上的LED灯不是GPIO2,那么通过查看电路原理图是哪个GPIO就写哪个数字。
电路图:
让LED灯闪烁:
原理:调用value(1)
然后延时,再调用value(0)
延时,重复上述操作
延时函数用法:
import time
time.sleep(1) # sleep for 1 second
time.sleep_ms(500) # sleep for 500 milliseconds
time.sleep_us(10) # sleep for 10 microseconds
start = time.ticks_ms() # get millisecond counter
delta = time.ticks_diff(time.ticks_ms(), start) # compute time difference
PWM呼吸灯:
基础概念:
PWM(Pulse Width Modulation):简称脉宽调制,是利用微处理器的数字输出来对模拟电路进行控制的一种非常有效的技术,广泛应用在测量、通信、工控等方面。
频率:单位时间内PWM方波重复的次数
占空比:一个周期内高电平持续时间与周期的比值
官方文档中用Python控制PWM的代码如下:
from machine import Pin, PWM
pwm0 = PWM(Pin(0), freq=5000, duty_u16=32768) # create PWM object from a pin
freq = pwm0.freq() # get current frequency
pwm0.freq(1000) # set PWM frequency from 1Hz to 40MHz
duty = pwm0.duty() # get current duty cycle, range 0-1023 (default 512, 50%)
pwm0.duty(256) # set duty cycle from 0 to 1023 as a ratio duty/1023, (now 25%)
duty_u16 = pwm0.duty_u16() # get current duty cycle, range 0-65535
pwm0.duty_u16(2**16*3//4) # set duty cycle from 0 to 65535 as a ratio duty_u16/65535, (now 75%)
duty_ns = pwm0.duty_ns() # get current pulse width in ns
pwm0.duty_ns(250_000) # set pulse width in nanoseconds from 0 to 1_000_000_000/freq, (now 25%)
pwm0.deinit() # turn off PWM on the pin
pwm2 = PWM(Pin(2), freq=20000, duty=512) # create and configure in one go
print(pwm2) # view PWM settings
注意:
- 需要可以具备输出功能的
IO
口,上述代码选择的是GPIO0
- 占空比的数值在
0~1023
之间
ESP32链接WIFI(今日重点):
WIFI工作模式:
- Access Point(
AP
) 无线接入点,提供无线接入的设备,家里的光猫就是结合WiFi和internet路由功能的AP。AP和AP可以相互连接。 - Station(
STA
) 无线终端,连接到AP的装置,手机,电脑等需要联网的设备都是出于STA模式,这个模式不允许其他设备接入
SSID概念:
BSSID就是无线路由器的MAC地址(本质是一个MAC地址)
SSID就是手机上搜索到的wifi名字(本质是一串字符)
ESSID是一个比较抽象的概念,它实际上就和ssid相同(本质也是一串字符)。如果有好几个无线路由器都叫这个名字,那么我们就相当于把这个ssid扩大了,所以这几个无线路由器共同的这个名字就叫ESSID
链接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('ssid', 'key') # connect to an AP
wlan.config('mac') # get the interface's MAC address
wlan.ifconfig() # get the interface's IP/netmask/gw/DNS addresses
ap = network.WLAN(network.AP_IF) # create access-point interface
ap.config(ssid='ESP-AP') # set the SSID of the access point
ap.config(max_clients=10) # set how many clients can connect to the network
ap.active(True) # activate the interface
A useful function for connecting to your local WiFi network is:
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())
本人(python基础一般)的链接WIFI的程序代码,也就是连接后加了个亮灯哈哈哈:
import network
import time
from machine import Pin
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
pin2 = Pin(2, Pin.OUT)
if not wlan.isconnected():
pin2.value(0)
print('connecting to network...')
wlan.connect('2605', '20011221')
while not wlan.isconnected():
pass
pin2.value(1)
time.sleep(3)
pin2.value(0)
print('network config:', wlan.ifconfig())
注意:
- 当前的
ESP32
不支持5G
路由器,所以链接的时候要注意这一点,否则连不上 - 可以将自己的手机开热点让
ESP32
链接
it项目网中的测试方法:
网络调试助手下载地址:NetAssist网络调试助手-软件工具-野人家园
测试代码:
from socket import *
# 1. 创建udp套接字
udp_socket = socket(AF_INET, SOCK_DGRAM)
# 2. 准备接收方的地址
dest_addr = ('192.168.31.56', 8080)
# 3. 从键盘获取数据
send_data = "hello world"
# 4. 发送数据到指定的电脑上
udp_socket.sendto(send_data.encode('utf-8'), dest_addr)
# 5. 关闭套接字
udp_socket.close()