关于 esp32-serial-logging
esp32-serial-logging 是用于采集微控制器串口输出并保存到文件的调试技能,适合 ESP32 及其他设备的开发与故障排查。它说明如何实时监控日志、筛选错误并检查重启记录。
主要功能
- 01将串口输出追加到日志文件
- 02实时查看日志内容
- 03按关键词筛选日志
- 04搜索崩溃和错误记录
- 05检查设备重启信息
适用场景
- 01复现 ESP32 问题时采集日志
- 02实时观察设备运行状态
- 03筛查错误、崩溃或断言
- 04排查设备重启原因
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ h1d/ agent-skills-esp32 --skill esp32-serial-logging安装后在 Claude Code 中输入/esp32-serial-logging确认已加载。
详细介绍
ESP32 串口日志监控
概述
将 ESP32(或任何微控制器)的串口输出捕获到文件中,以便实时监控和分析。
ESP32 设置(设备端)
ESP-IDF 框架
#include "esp_log.h"
static const char* TAG = "MyComponent";
void my_function() {
ESP_LOGI(TAG, "Info message");
ESP_LOGW(TAG, "Warning: value=%d", some_value);
ESP_LOGE(TAG, "Error occurred");
ESP_LOGD(TAG, "Debug details");
}
Arduino 框架
void setup() {
Serial.begin(115200);
}
void loop() {
Serial.println("Status: running");
Serial.printf("Sensor: %d\n", analogRead(A0));
delay(1000);
}
主机端 - 捕获日志
# Find serial port
PORT=$(ls /dev/cu.usbmodem* /dev/ttyUSB* /dev/ttyACM* 2>/dev/null | head -1)
echo "Found port: $PORT"
# Configure and start capture
stty -f "$PORT" 115200 raw -echo 2>/dev/null || stty -F "$PORT" 115200 raw -echo
cat "$PORT" >> /tmp/device.log &
echo "Logging to /tmp/device.log (PID: $!)"
监控日志
# Real-time monitoring
tail -f /tmp/device.log
# Filter specific patterns
tail -f /tmp/device.log | grep -E "ERROR|WiFi|Button"
搜索错误
# Find crashes and errors
grep -E "ERROR|crash|overflow|panic|assert|Backtrace" /tmp/device.log
# Find reboots (look for boot messages or uptime resets)
grep -E "boot:|rst:|Uptime: [0-9] sec" /tmp/device.log
调试工作流
-
在重现问题前清除日志:
> /tmp/device.log -
重现问题
-
分析捕获的日志:
cat /tmp/device.log
常见波特率
| 设备 | 波特率 |
|---|---|
| ESP32(默认) | 115200 |
| ESP32(高速) | 460800 |
| Arduino | 9600 |
| STM32 | 115200 |
停止记录
pkill -f "cat /dev/cu.usbmodem"
pkill -f "cat /dev/ttyUSB"
故障排除
- 找不到端口:检查 USB 连接,尝试
ls /dev/cu.* /dev/tty.* - 权限被拒绝:将用户添加到
dialout组(Linux) - 输出乱码:波特率错误
评论
支持 Markdown