[SOLVED] Catch "hit enter within 15 seconds from app start"

filimonic
Posts: 6
Joined: Tue Jun 18, 2019 9:34 pm

[SOLVED] Catch "hit enter within 15 seconds from app start"

Postby filimonic » Mon May 18, 2026 12:16 am

Hi. I have esp32c6 board and it has app running without "external" hw (it uses bt and wifi).
CONFIG_ESP_CONSOLE_UART_DEFAULT=y and it just uses ESP_LOGx to output logs.

I want to implement esp console on this serial in this way:
On app startup, it gives user 15 seconds to hit ENTER through terminal console like PuTTY.
If user hits ENTER, then something like CLI started on CONSOLE serial port (like esp_console)
If user did not hit ENTER, then app runs normally

I expect that serial port is already initialized somehow for logging, and i exprected to use something like this with no luck, right inside app_main:

Code: Select all

while(...) {
// esp_rom_output_rx_one_char(&c);
// int ret = select(stdin_fd + 1, &rfds, NULL, NULL, &tv);
// 
}
But all my attemts just ignoring key press.
Could you help me?
Last edited by filimonic on Mon May 18, 2026 2:51 pm, edited 1 time in total.

username
Posts: 593
Joined: Thu May 03, 2018 1:18 pm

Re: Catch "hit enter within 15 seconds from app start"

Postby username » Mon May 18, 2026 2:57 am

while(...)
{
int ch = fgetc(stdin);

if (ch == '\n' || ch == '\r')
{
// Go do your thing
}

vTaskDelay(pdMS_TO_TICKS(100));
}

filimonic
Posts: 6
Joined: Tue Jun 18, 2019 9:34 pm

Re: Catch "hit enter within 15 seconds from app start"

Postby filimonic » Mon May 18, 2026 8:09 am

This way does not work.

Code: Select all

bool startup_wait_for_console_command(uint8_t seconds)
{
    static const char *tag = __func__;
    vTaskDelay(pdMS_TO_TICKS(3000));
    while (seconds > 0) {
        ESP_LOGI(tag, "=== Hit ENTER to configure. %d left ===", seconds);
        for(int i = 0; i < 10; i++)
        {
            int ch = fgetc(stdin);
            ESP_LOGI(tag, "Got char %d", ch);
            vTaskDelay(pdMS_TO_TICKS(100));
        }
        seconds--;
    }
    ESP_LOGI(tag, "Timeout - entering normal operation mode");
    return false;
}
When i connect (I use PuTTY), i see this. Any keypress, including enter, ignored.

Code: Select all

I (11524) startup_wait_for_console_command: === Hit ENTER to configure. 7 left ===
I (11524) startup_wait_for_console_command: Got char -1
I (11624) startup_wait_for_console_command: Got char -1
I (11724) startup_wait_for_console_command: Got char -1
I (11824) startup_wait_for_console_command: Got char -1
I (11924) startup_wait_for_console_command: Got char -1
I (12024) startup_wait_for_console_command: Got char -1
I (12124) startup_wait_for_console_command: Got char -1
I (12224) startup_wait_for_console_command: Got char -1
I (12324) startup_wait_for_console_command: Got char -1
I (12424) startup_wait_for_console_command: Got char -1
I (12524) startup_wait_for_console_command: === Hit ENTER to configure. 6 left ===
...

MicroController
Posts: 2661
Joined: Mon Oct 17, 2022 7:38 pm
Location: Europe, Germany

Re: Catch "hit enter within 15 seconds from app start"

Postby MicroController » Mon May 18, 2026 12:06 pm

https://docs.espressif.com/projects/esp ... figuration
if you also want to input or use REPL with the console, please select CONFIG_ESP_CONSOLE_USB_SERIAL_JTAG
And/or try the example:
https://github.com/espressif/esp-idf/tr ... _jtag_echo

filimonic
Posts: 6
Joined: Tue Jun 18, 2019 9:34 pm

Re: Catch "hit enter within 15 seconds from app start"

Postby filimonic » Mon May 18, 2026 2:50 pm

Thank you, missed this. This helped.
https://docs.espressif.com/projects/esp ... figuration
if you also want to input or use REPL with the console, please select CONFIG_ESP_CONSOLE_USB_SERIAL_JTAG
And/or try the example:
https://github.com/espressif/esp-idf/tr ... _jtag_echo

Who is online

Users browsing this forum: PetalBot and 11 guests