Demo 影片:
效果:
在 Arduino IDE 序列埠監控窗(Serial Monitor)輸入字串,ESP32 開發板上的程式讀取字串,當輸入字串為 RST,則執行 Reset,重啟開發板。
- 使用 Serial.readStringUntil() 讀取字串,並設定讀取到換行字元(\n)時停止,所以序列埠監控窗(Serial Monitor)須設定送出訊息時,加上 New Line。
等待 serial data,預設 Timeout 為 1000ms (1秒),所以若送出訊息時,選擇 No Line Ending,過了1秒 Timeout,仍會結束讀取,繼續執行後面程式。 - Serial Monitor 和程式的 baud rate 需相同,這邊設為 115200
- 使用 ESP.restart() 進行 Reset
- software reset of the chip (對晶片進行軟體重設)
- execution of the program stops (程式執行停止)
- both CPUs are reset (兩個 CPU 都將被重設)
- the application is loaded by the bootloader and starts execution again (應用程式由啟動引導程式載入並重新開始執行)
- 使用 esp_reset_reason() 取得最近一次 Reset 的原因
- 執行結果
程式碼:
void setup() {
Serial.begin(115200); //設定 baud rate
// 獲取最近一次重置的原因
esp_reset_reason_t reason = esp_reset_reason();
Serial.println("========================================");
Serial.print("系統啟動... 重置原因代碼: ");
Serial.println(reason);
switch (reason) {
case ESP_RST_UNKNOWN:
Serial.println("原因: Reset reason can not be determined. (無法確定重置原因)");
break;
case ESP_RST_POWERON:
Serial.println("原因: Reset due to power-on event. (因開機事件而重設)");
break;
case ESP_RST_EXT:
Serial.println("原因: Reset by external pin. (透過外部引腳,不適用於 ESP32)");
break;
case ESP_RST_SW:
Serial.println("原因: Software reset via esp_restart. (軟體重置:透過調用 esp_restart 觸發)");
break;
case ESP_RST_PANIC:
Serial.println("原因: Software reset due to exception/panic. (異常重置:系統崩潰或程式碼運行錯誤)");
break;
case ESP_RST_INT_WDT:
Serial.println("原因: Reset due to interrupt watchdog. (中斷看門狗重置:中斷服務例程 ISR 受到長時間阻斷,即 IWDT 逾時)。IWDT:Independent Watchdog Timer");
break;
case ESP_RST_TASK_WDT:
Serial.println("原因: Reset due to task watchdog. (任務看門狗重置:某個 FreeRTOS 任務未及時餵狗)");
break;
case ESP_RST_WDT:
Serial.println("原因: Reset due to other watchdogs. (其他看門狗重置:包括 RTC 看門狗等硬體計時器)");
break;
case ESP_RST_DEEPSLEEP:
Serial.println("原因: Reset after exiting deep sleep mode. (深睡喚醒重置:系統從深度睡眠模式中恢復)");
break;
case ESP_RST_BROWNOUT:
Serial.println("原因: Brownout reset (software or hardware). (欠壓重置:供電電壓不穩定導致)");
break;
case ESP_RST_SDIO:
Serial.println("原因: Reset over SDIO. (SDIO 重置:透過 SDIO 接口發送的復位指令)");
break;
case ESP_RST_USB:
Serial.println("原因: Reset by USB peripheral. (USB 重置:由 USB 設備接口觸發的重置)");
break;
case ESP_RST_JTAG:
Serial.println("原因: Reset by JTAG. (JTAG 重置:調試器通過 JTAG 觸發)");
break;
case ESP_RST_EFUSE:
Serial.println("原因: Reset due to efuse error. (eFuse 錯誤重置:檢測到硬件 eFuse 數據損壞)");
break;
case ESP_RST_PWR_GLITCH:
Serial.println("原因: Reset due to power glitch detected. (電源毛刺重置:檢測到極短時間的電壓異常)");
break;
case ESP_RST_CPU_LOCKUP:
Serial.println("原因: Reset due to CPU lock up (double exception). (CPU 死鎖重置:發生了嚴重的雙重異常)");
break;
default:
Serial.println("原因: Undefined reset reason. (未定義的重置原因)");
break;
}
Serial.println("========================================");
}
void loop() {
//讀取 Serial Monitor 輸入
if (Serial.available()) {
String readString = Serial.readStringUntil('\n'); // Read until newline
readString.toUpperCase();
Serial.println("[readString]:" + readString);
if (readString == "RST") {
Serial.println("[ESP.restart]");
ESP.restart(); //software reset
}
}
}- https://aiot.taiwansensor.com/courses/arduino-uno-r4-wifi-%E5%88%9D%E5%AD%B8%E8%80%85%E7%9A%84%E5%85%A8%E6%96%B0%E7%B7%9A%E4%B8%8A%E5%BD%B1%E9%9F%B3%E8%AA%B2%E7%A8%8B/lessons/%E7%AC%AC-14-%E8%AA%B2%EF%BC%9A%E5%BE%9E%E4%B8%B2%E5%88%97%E7%9B%A3%E6%8E%A7%E8%A6%96%E7%AA%97%E8%AE%80%E5%8F%96%E4%BD%BF%E7%94%A8%E8%80%85%E8%BC%B8%E5%85%A5/
第 14 課:從串列監控視窗讀取使用者輸入 – 智慧物聯網整合應用實驗室 - https://docs.arduino.cc/software/ide-v2/tutorials/ide-v2-serial-monitor/
Using the Serial Monitor tool | Arduino Documentation - https://docs.arduino.cc/language-reference/en/functions/communication/serial/ifSerial/
if(Serial) | Arduino Documentation - https://docs.arduino.cc/language-reference/en/functions/communication/serial/readStringUntil/
Serial.readStringUntil() | Arduino Documentation - https://docs.arduino.cc/language-reference/en/functions/communication/serial/setTimeout/
Serial.setTimeout() | Arduino Documentation - https://docs.arduino.cc/language-reference/en/variables/data-types/stringObject/Functions/toUpperCase/
toUpperCase() | Arduino Documentation - https://forum.arduino.cc/t/comparing-serial-readstring-to-text/575820/5
Comparing Serial.readString() to text - Projects / Programming - Arduino Forum - https://www.reddit.com/r/esp32/comments/vggc1t/how_to_reset_the_esp32_by_code/
How to reset the Esp32 by code. : r/esp32 - https://docs.espressif.com/projects/esp-idf/en/v5.5.2/esp32/api-reference/system/misc_system_api.html
Miscellaneous System APIs - ESP32 - — ESP-IDF Programming Guide v5.5.2 documentation - https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/system/misc_system_api.html#_CPPv418esp_reset_reason_t
Miscellaneous System APIs - ESP32 - — ESP-IDF Programming Guide latest documentation - https://docs.espressif.com/projects/esp-idf/en/v5.5.2/esp32/api-reference/system/wdts.html
Watchdogs - ESP32 - — ESP-IDF Programming Guide v5.5.2 documentation - https://docs.espressif.com/projects/esp-idf/zh_CN/latest/esp32/api-reference/system/misc_system_api.html#_CPPv418esp_reset_reason_t
杂项系统 API - ESP32 - — ESP-IDF 编程指南 latest 文档 - https://docs.espressif.com/projects/esp-idf/zh_CN/stable/esp32/api-reference/system/wdts.html
看门狗 - ESP32 - — ESP-IDF 编程指南 v5.5.2 文档
































