ESP32-S3 API Reference
Files Referenced in This Article
ai_cloud.pyATGM336H_5N.pymax30102.pyxgo.pylv_utils.pyxunfei.pyespnow.pyhrcalc.py
Product Overview
This reference focuses on the APIs exclusive to the ESP32-S3 product line (mPython Board 3.0, mPython Core 2.0, etc.), with emphasis on the newly added modules and capabilities: AI cloud interaction, GNSS positioning (BeiDou/GPS), heart-rate and blood-oxygen sensing, robot-dog control, LVGL event-loop utilities, and the key differences from the ESP32 version APIs. Aimed at K12 maker education scenarios, the documentation helps developers quickly understand each module’s responsibilities, invocation methods, and considerations.
Core Business Flow
AI speech recognition and Q&A flow (Baidu Speech + ERNIE large model)
The recording file is converted to Base64 and sent to the Baidu speech recognition interface, which returns text; then the ERNIE conversation interface is called to obtain the answer.
Key nodes: authentication token retrieval, audio file reading, HTTP POST request, JSON parsing.
GNSS positioning data reading and parsing flow (ATGM336H)
Raw NMEA frames are read over UART, the RMC and BDGSA segments are filtered, and time, latitude/longitude, speed, date, and satellite information are parsed.
Key nodes: serial buffer, ASCII decoding fault tolerance, field extraction and validation, data validity flags.
Heart-rate and blood-oxygen measurement flow (MAX30102 + hrcalc)
The device is initialized over I2C, red/infrared FIFO values are read periodically, the algorithm computes HR and SpO2, and a valid result or an error code is output.
Key nodes: register configuration, FIFO sampling, peak detection, ratio computation, and threshold judgment.
Robot-dog control flow (XGO)
Commands are sent over the serial protocol to control movement, posture, actions, servo angles, battery, and firmware information, etc.
Key nodes: command encapsulation and validation, parameter range mapping, response unpacking, and timeout handling.
LVGL event-loop flow
LVGL ticks and task handling are driven by a timer or uasyncio, supporting synchronous/asynchronous refresh callbacks.
Key nodes: lv.init, tick_inc, task_handler, exception capture, and automatic stop.
sequenceDiagram
participant App as "Application"
participant AI as "AI cloud module"
participant HTTP as "Network request"
App->>AI : Call get_text(recording file)
AI->>HTTP : Obtain access token
HTTP-->>AI : Return token
AI->>HTTP : POST to speech recognition interface
HTTP-->>AI : Return recognition result
App->>AI : Call get_answer(text)
AI->>HTTP : POST to ERNIE conversation interface
HTTP-->>AI : Return answer text
AI-->>App : Return the final answer
Diagram Sources
ai_cloud.py:18-24ai_cloud.py:69-92ai_cloud.py:94-122
Section Sources
ai_cloud.py:18-24ai_cloud.py:69-92ai_cloud.py:94-122
Feature Module List
AI cloud module (ai_cloud.py)
Responsibilities: provides convenient interfaces for speech recognition and ERNIE Q&A, encapsulating authentication, HTTP requests, and JSON parsing.
User value: simplifies cloud AI integration and reduces network and protocol complexity.
Acceptance points: successful token retrieval, correct recognition results, and reasonable Q&A text; on exceptions, print the error and raise a prompt.
GNSS positioning module (ATGM336H_5N.py)
Responsibilities: reads NMEA data over UART, parses RMC and BDGSA, and outputs time, latitude/longitude, speed, and date.
User value: provides basic positioning capability for location services, trajectory logging, and navigation projects.
Acceptance points: stable reading of RMC/BDGSA, parsing of valid coordinates and UTC time, and logic for handling invalid data.
Heart-rate and blood-oxygen module (max30102.py + hrcalc.py)
Responsibilities: drives the MAX30102 over I2C, collects PPG signals, and computes heart rate and blood-oxygen concentration.
User value: collects basic physiological indicators for health projects.
Acceptance points: the device is discoverable, FIFO data is readable, HR/SpO2 are within a reasonable range with validity flags.
Robot-dog control module (xgo.py)
Responsibilities: controls the movement, posture, actions, servos, battery, and firmware information of the XGO robot dog over the serial protocol.
User value: the core control library for robotics teaching and competitions.
Acceptance points: command sending and response validation pass, parameter range limits take effect, and read-back data is accurate.
LVGL event-loop utility (lv_utils.py)
Responsibilities: provides LVGL periodic refresh and task scheduling, supporting synchronous/asynchronous modes and exception handling.
User value: unifies the GUI refresh mechanism and avoids blocking and memory-allocation issues.
Acceptance points: after lv.init, the tick advances normally, task_handler executes, and exceptions are captured and safely stopped.
iFLYTEK speech module (xunfei.py)
Responsibilities: implements speech recognition (IAT) and speech synthesis (TTS) over WebSocket, including authentication signatures.
User value: extends online speech capabilities, suitable for voice interaction projects.
Acceptance points: the WebSocket connection is established, the audio stream is sent in segments, recognition results are concatenated correctly, and TTS audio download completes.
ESP-NOW communication module (espnow.py)
Responsibilities: encapsulates the underlying ESPNowBase, providing connectionless short-message send/receive and iterative reception.
User value: a low-power, low-latency LAN communication solution.
Acceptance points: byte streams are sent and received correctly, timeout handling is reasonable, and the iterator mode is usable.
Section Sources
ai_cloud.py:18-24ai_cloud.py:69-92ai_cloud.py:94-122ATGM336H_5N.py:29-165ATGM336H_5N.py:198-388max30102.py:39-168hrcalc.py:13-105xgo.py:123-787lv_utils.py:43-142xunfei.py:65-229espnow.py:6-41
Data and State
AI cloud module
Input: recording file path, text content; configuration: API_KEY/SECRET_KEY.
Output: recognized text, Q&A answers; state: token cache, request header settings.
Boundaries: network reachability, cloud rate limiting, and authentication failures require retry or degradation.
GNSS positioning module
Input: raw UART NMEA frames; state: whether decoded, whether parsed, data validity flags.
Output: UTC time, latitude/longitude (degrees-minutes format and decimal), speed (knots and km/h), date, satellite PRN list.
Boundaries: fault tolerance for non-ASCII characters, and null-value handling when RMC/BDGSA is missing.
Heart-rate and blood-oxygen module
Input: I2C FIFO red/infrared value sequences; state: device initialization, measurement in progress.
Output: HR (bpm), SpO2 (%), and validity; internal state: ir_D/red_D mean values.
Boundaries: sampling duration about 30 seconds; returns None or an error code on exceptions.
Robot-dog control module
Input: movement direction/steps, posture angles, action IDs, servo angles, battery queries, etc.
Output: firmware version, battery level, IMU angles (roll/pitch/yaw), servo angles.
State: protocol frame parsing state machine, timeout and validation-failure handling.
LVGL event loop
Input: frequency, timer ID, refresh callback, exception handler.
Output: LVGL task handling, UI refresh; state: running/stopped, nesting count.
Boundaries: only synchronous mode is supported when uasyncio is missing; auto-deinit on exceptions.
iFLYTEK speech module
Input: audio file or text; state: WebSocket connection, message frame state (first frame/middle frame/last frame).
Output: recognition result string, TTS audio file.
Boundaries: authentication signature generation, RTC time retrieval, network stability.
ESP-NOW module
Input: target address, data length, format; state: receive buffer, iterator state.
Output: byte stream or floats (optional formatting); state: timeout and empty result.
Boundaries: MAX_DATA_LEN limit, byte-order conversion.
Section Sources
ai_cloud.py:18-24ai_cloud.py:69-92ai_cloud.py:94-122ATGM336H_5N.py:29-165ATGM336H_5N.py:198-388max30102.py:39-168hrcalc.py:13-105xgo.py:123-787lv_utils.py:43-142xunfei.py:65-229espnow.py:6-41
Key Constraints and Boundaries
Network and cloud dependencies
Both AI cloud and iFLYTEK speech depend on Wi-Fi and the internet; ensure network connectivity and correct authentication configuration; implement retry and degradation strategies on cloud rate limiting or failures.
Hardware resources and pins
GNSS uses UART (UART1 by default, TX/RX configurable); mind pin conflicts; MAX30102 uses I2C; confirm the device address and that the bus is idle.
XGO communicates over serial; mind the baud rate and protocol frame validation; the LVGL event loop occupies timer or coroutine resources.
Data validity and fault tolerance
GNSS data requires checking the RMC validity flag and returns a placeholder value when missing; heart-rate and blood-oxygen measurement requires stable contact and sufficient sampling duration.
The LVGL event loop stops automatically on exceptions to avoid deadlocks; ESP-NOW reception handles timeouts and empty results.
Performance and real-time behavior
Heart-rate and blood-oxygen measurement takes about 30 seconds; avoid blocking other tasks during this period; the LVGL refresh frequency should be adjusted according to the screen and task load.
The XGO command sending interval and minimum time parameters affect movement smoothness and should be tuned to the actual hardware.
Section Sources
ai_cloud.py:69-92xunfei.py:65-229ATGM336H_5N.py:29-165max30102.py:133-168xgo.py:610-688lv_utils.py:43-142espnow.py:14-41