外部 API 参考
本页索引 M5Cardputer 库常用到的外部 ESP32 平台 API。这些 不属于 M5Cardputer — 点击链接可查看各库的官方完整 API 文档。
WiFi
| 库 |
来源 |
关键类 / 函数 |
| arduino-esp32 WiFi |
文档 |
WiFi.begin(), WiFi.status(), WiFi.localIP(), WiFi.disconnect() |
#include <WiFi.h>
WiFi.begin("SSID", "password");
while (WiFi.status() != WL_CONNECTED) { delay(500); }
HTTP 客户端
| 库 |
来源 |
关键类 |
| HTTPClient |
arduino-esp32 内置 |
HTTPClient, begin(), GET(), POST(), getString() |
#include <HTTPClient.h>
HTTPClient http;
http.begin("http://example.com/api");
int code = http.GET();
String body = http.getString();
http.end();
Web 服务器
| 库 |
来源 |
关键类 |
| WebServer |
arduino-esp32 内置 |
WebServer, on(), send(), handleClient() |
MQTT
| 库 |
来源 |
关键类 |
| PubSubClient |
GitHub |
PubSubClient, setServer(), connect(), publish(), subscribe(), loop() |
| AsyncMQTTClient |
GitHub |
异步、非阻塞替代方案 |
#include <PubSubClient.h>
WiFiClient wifiClient;
PubSubClient mqtt(wifiClient);
mqtt.setServer("broker.emqx.io", 1883);
mqtt.connect("cardputer");
mqtt.publish("topic/hello", "data");
mqtt.loop();
蓝牙 (BLE)
| 库 |
来源 |
关键类 |
| ESP32 BLE Arduino |
GitHub |
BLEDevice, BLEServer, BLECharacteristic, BLEAdvertising |
| NimBLE |
GitHub |
更轻量,占用更少 RAM |
#include <BLEDevice.h>
#include <BLEUtils.h>
#include <BLEServer.h>
USB HID
| 库 |
来源 |
关键类 |
| ESP32TinyUSB |
GitHub |
USBHID, keyboardReport(), mouseReport() |
| Arduino ESP32 USB |
ESP32-S2/S3 内置 |
USB.begin(), Keyboard.begin(), Keyboard.print() |
#include <USB.h>
#include <USBHIDKeyboard.h>
USBHIDKeyboard Keyboard;
Keyboard.begin();
Keyboard.print("你好");
TinyUSB 是 usbKeyboard 示例使用的库。参见 examples/Basic/keyboard/usbKeyboard。
SSH 客户端
| 库 |
来源 |
关键类 |
| LibSSH-ESP32 |
GitHub |
SSHClient, connect(), authenticate(), executeCommand() |
用于 SSHClient 示例。参见 examples/Advanced/SSHClient。
FreeRTOS(多任务)
| 资源 |
链接 |
| ESP-IDF FreeRTOS |
文档 |
TaskHandle_t taskHandle;
xTaskCreatePinnedToCore(
myTask, "任务名", 4096, NULL, 1, &taskHandle, 0
);
void myTask(void* param) {
while (1) {
// 执行工作
vTaskDelay(10 / portTICK_PERIOD_MS);
}
}
Warning
M5Cardputer 键盘驱动内部使用 FreeRTOS 功能。使用 TCA8418KeyboardReader(ADV 版)时,避免在核心 0 上创建任务 — ISR 在那里运行。
OTA 升级
| 库 |
来源 |
| ArduinoOTA |
arduino-esp32 内置 |
| ElegantOTA |
GitHub(Web UI 方式) |
#include <ArduinoOTA.h>
ArduinoOTA.begin();
// 在 loop() 中:
ArduinoOTA.handle();
JSON 解析
| 库 |
来源 |
关键类 |
| ArduinoJson |
GitHub |
StaticJsonDocument, DynamicJsonDocument, deserializeJson(), serializeJson() |
性能与调试
| 工具 |
用途 |
ESP.getFreeHeap() |
检查剩余 DRAM |
ESP.getPsramSize() / ESP.getFreePsram() |
PSRAM 信息 |
ESP.getChipModel() / ESP.getChipCores() |
芯片信息 |
esp_task_wdt_init() |
看门狗定时器 |
Serial.printf() |
调试输出 |
另请参阅