.. _esp32_quickref: ESP32 快速参考手册 ============================= .. only:: not latex .. image:: https://docs.01studio.cc/data/picture/pyWiFi-ESP32_pinout.png :alt: pyWiFi-ESP32 pinout :width: 700px .. only:: latex .. image:: https://docs.01studio.cc/data/picture/pyWiFi-ESP32_pinout.png :alt: pyWiFi-ESP32 pinout ESP32开发板 (图片来源: 01Studio). 以下内容是ESP32开发板的快速入门内容。如果这是你第一次使用ESP32开发板,那么建议你先阅读以下两个章节熟悉一下: .. toctree:: :maxdepth: 1 general.rst tutorial/index.rst 安装MicroPython ---------------------- 请参考教程的相应部分: :ref:`esp32_intro`. 它还包括故障排除小节。 通用控制 --------------------- MicroPython 的串口交互调试(REPL)在 UART0 (GPIO1=TX, GPIO3=RX),波特率为:115200。 Tab按键补全功能对于找到每个对象的使用方法非常有用。 粘贴模式 (ctrl-E) 对需要复制比较多 的python代码到REPL是非常有用。 The :mod:`machine` module:: import machine machine.freq() # 获取CPU当前工作频率 machine.freq(240000000) # 设置CPU的工作频率为 240 MHz The :mod:`esp` module:: import esp esp.osdebug(None) # 关闭原厂 O/S 调试信息 esp.osdebug(0) # 将原厂 O/S 调试信息重定向到 UART(0) 输出 # 与flash交互的低级方法 esp.flash_size() esp.flash_user_start() esp.flash_erase(sector_no) esp.flash_write(byte_offset, buffer) esp.flash_read(byte_offset, buffer) The :mod:`esp32` module:: import esp32 esp32.hall_sensor() # 读取内部霍尔传感器 esp32.raw_temperature() # 读取内部温度传感器,在MCU上, 单位:华氏度F esp32.ULP() # 使用超低功耗协处理器(ULP) 请注意ESP32内部温度读取数值会比实际要高,因为芯片工作时候回发热。 从睡眠状态唤醒后立即读取温度传感器可以最大限度地减少这种影响。 Networking ---------- The :mod:`network` module:: import network wlan = network.WLAN(network.STA_IF) # 创建 station 接口 wlan.active(True) # 激活接口 wlan.scan() # 扫描允许访问的SSID wlan.isconnected() # 检查创建的station是否连已经接到AP wlan.connect('essid', 'password') # 连接到指定ESSID网络 wlan.config('mac') # 获取接口的MAC地址 wlan.ifconfig() # 获取接口的 IP/netmask(子网掩码)/gw(网关)/DNS 地址 ap = network.WLAN(network.AP_IF) # 创捷一个AP热点接口 ap.config(essid='ESP-AP') # 激活接口 ap.config(max_clients=10) # 设置热点允许连接数量 ap.active(True) # 设置AP的ESSID名称 连接到本地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('essid', 'password') while not wlan.isconnected(): pass print('network config:', wlan.ifconfig()) 一旦网络建立成功,你就可以通过 :mod:`socket ` 模块创建和使用 TCP/UDP sockets 通讯, 以及通过 ``urequests`` 模块非常方便地发送 HTTP 请求。 After a call to ``wlan.connect()``, the device will by default retry to connect **forever**, even when the authentication failed or no AP is in range. ``wlan.status()`` will return ``network.STAT_CONNECTING`` in this state until a connection succeeds or the interface gets disabled. This can be changed by calling ``wlan.config(reconnects=n)``, where n are the number of desired reconnect attempts (0 means it won't retry, -1 will restore the default behaviour of trying to reconnect forever). 延时和时间 ----------- Use the :mod:`time