using scanf() over esp-ide serial monitor for user input

PollysCracker
Posts: 13
Joined: Fri Jul 19, 2024 11:08 pm

using scanf() over esp-ide serial monitor for user input

Postby PollysCracker » Fri Jan 24, 2025 7:17 pm

Hello,

tested with the esp32 c6 devkit, with watchdog disabled.
A simplistic routine where i read from the terminal for keyboard input using scanf in a while loop, sometimes when log messages are printed to the terminal while inside the loop waiting for user input, the esp simply stops responding without any error or warning.

I was wondering why this is, i suspect my approach is actually very bad or missing some key elements, either that or the serial terminal in the esp-ide is to blame?
I have not yet tested this with a program such as PuTTY.

The way i have implemented this is by blocking, this is soley for testing and simulation purposes, i had to disable watchdog timer as it was inflicting with this functionality.
I'm using while loop to check for input and "should" normally stay in that blocking loop untill the user has entered one of the recognized number (/letter/sentence).
But the system seems to freeze unable to check for input, i'm fairly sure its due to the program printing to the serial monitor while checking for user input, but can't figure out where the issue comes from or how to get around that.


Another thing to note is that there is another function in this program that is being called that is also using user input over serial terminal in the same manner as described in scan_usercmd().
Normally these should not be able to conflict with each other as they are both being processed independently in a while loop without using freertos.

I suspect that when the program prints to the monitor(either using esp_log or printf functions) while waiting for user input, the system used the printed char or string and there freezes somehow.

Code: Select all

#include <stdio.h>
#include <stdbool.h>
#include <time.h>
#include <unistd.h>
#include <inttypes.h>
#include "esp_err.h"
#include "wifi_app.h"
#include "sntptime.h"
#include "nvs_flash.h"
#include "esp_log.h"
#include "esp_timer.h"

bool started = false;
static const char tag[] = "-[MAIN.c]-";

void scan_usercmd(void)
{
	int cmd = 0;
	esp_err_t err;
	printf(" Options: \n1: start wifi connection\n2: manually start sntp sync \n3: Stop sntp service\n4: Get system time\n5: Scan en list all available AP\nEnter command: ");
	
	while( cmd == 0 || cmd > 5)	scanf("%i", &cmd); // Wait for valid input
		
	printf("%i\n",cmd);
	switch(cmd)
	{
		case 1: {
			err = InitWifi();
			if( err == ESP_OK)	{
				ESP_LOGI(tag, "Succesfully started the wifi connection.");
			} else ESP_LOGE(tag, "Failed to start the wifi connection (%i)", err);
				break;
		}
		/*case 10: {
			DeinitWifi();
			break;
		}*/
		case 2: {
			err = timeSyncStart();
			if( err == ESP_OK) {
				ESP_LOGI(tag, "Succesfully started sntp.");
			} else ESP_LOGE(tag, "Failed to start sntp (%i)", err);
				break;
		}
		case 3: {
			timeSyncStop();
			break;
		}
		case 4: {
			struct tm timeinfo;
    		        time_t now;
    		        char Strtime[100];
			printf("\nGetting system time..");
			time(&now);
			localtime_r(&now, &timeinfo);
			strftime(Strtime, sizeof(Strtime), "%c", &timeinfo);
			printf("System time: '%s'\n", Strtime);
			break;
		}
		case 5: {
			ScanPrintWifiStations();
			break;
		}
	}
	
}

void app_main(void)
{
	esp_err_t err = nvs_flash_init();
	if(err == ESP_ERR_NVS_NO_FREE_PAGES || err == ESP_ERR_NVS_NEW_VERSION_FOUND)
	{
		ESP_ERROR_CHECK(nvs_flash_erase());
		err = nvs_flash_init();
	}
	if(err != ESP_OK)
	{
		ESP_LOGD(tag, "Error (%s) while initializing memory, can not proceed", esp_err_to_name(err));
	}
	err = esp_timer_early_init();
	if(err != ESP_OK) ESP_LOGE(tag, "Could not early initiate timer.h library (%i).", err);
	
	while(1) {
		scan_usercmd();
		/*if(!started) {
			if( InitWifiConnection() == ESP_OK)	started = true;
		}*/
		
	}
	ESP_LOGE(tag,"app_main escaped infinite loop..");
}

Who is online

Users browsing this forum: No registered users and 54 guests