I need to read input from the user console

User avatar
mzimmers
Posts: 643
Joined: Wed Mar 07, 2018 11:54 pm
Location: USA

I need to read input from the user console

Postby mzimmers » Mon Mar 26, 2018 1:26 am

Hi, all -

Thanks to the help of people here, I've gotten the WiFi software working on my esp32. I'm at the point where my next task is to remove my hard-coded password to my access point from my program.

So...I think it's time to learn how to read input from the console. I did a little online searching, and didn't find anything that was very recent, so I figured I better ask here.
  • What is the state of scanf(), or a reasonable equivalent?
  • If it still doesn't exist, then what if I wrote a routine that did something like this?

Code: Select all

char c = 0;
while (c != 0x0d)
{
	// run some 100ms delay (not sure how to do this)
	c = fscanf(stdin);
	if (c != 0xff)
		strcat (str, c);
}

(I realize the code fragment isn't all there, but hopefully you get the idea.)

Would this be an advisable course of action, or is there a better way?

Thanks...

User avatar
kolban
Posts: 1683
Joined: Mon Nov 16, 2015 4:43 pm
Location: Texas, USA

Re: I need to read input from the user console

Postby kolban » Mon Mar 26, 2018 2:20 am

That would likely work. The ESP32 uses its serial port for logging messages. Data sent into the serial port is available to be read by an application. You have UART APIs where you can read directly but, along the lines that you were previously writing about, it is my understanding that access to the UART can also be achieved through the file descriptors associated with 0 and 1 (stdin and stdout).
Free book on ESP32 available here: https://leanpub.com/kolban-ESP32

User avatar
mzimmers
Posts: 643
Joined: Wed Mar 07, 2018 11:54 pm
Location: USA

Re: I need to read input from the user console

Postby mzimmers » Mon Mar 26, 2018 2:28 am

Thanks, Mr. Kolban. So, am I correct in inferring from another code snippet I saw that an fgetc() will return a 0xff if no data was read?

User avatar
kolban
Posts: 1683
Joined: Mon Nov 16, 2015 4:43 pm
Location: Texas, USA

Re: I need to read input from the user console

Postby kolban » Mon Mar 26, 2018 1:24 pm

Thats a good question ... but its one we are going to have to test and see. If we think of a "real file" ... a real file has a fixed size ... if I have a file of 100 bytes ... then its size is 100. If I read each byte of data then after 100 reads there is no more and an attempt to read the next byte will return the EOF (End of File) marker.

However, a file that is actually connected to an input stream doesn't have the same notion. We can keep reading from the serial port "for ever". That then begs the question of what should happen when there is no next character "immediately" available. My gut is saying that the semantics of attempting to read that next character would be to "block" until there is a next character. You may be better off using the ESP-IDF UART APIs rather than their higher level encapsulation/modeling as files.
Free book on ESP32 available here: https://leanpub.com/kolban-ESP32

User avatar
fly135
Posts: 606
Joined: Wed Jan 03, 2018 8:33 pm
Location: Orlando, FL

Re: I need to read input from the user console

Postby fly135 » Mon Mar 26, 2018 2:24 pm


User avatar
mzimmers
Posts: 643
Joined: Wed Mar 07, 2018 11:54 pm
Location: USA

Re: I need to read input from the user console

Postby mzimmers » Mon Mar 26, 2018 3:43 pm

Thanks for that link, fly...that's a good-looking library. Any idea if I can use my msys2 window for terminal input. As it is, printf() output is directed to it.

If necessary, I could look into attaching an FTDI cable to my esp32 to give me a second UART connection (I assume the chip supports at least 2 UARTs).

User avatar
fly135
Posts: 606
Joined: Wed Jan 03, 2018 8:33 pm
Location: Orlando, FL

Re: I need to read input from the user console

Postby fly135 » Mon Mar 26, 2018 4:25 pm

I actually use tera term plus for my terminal. I imagine that msys32 would work fine too. You basically have a CLI that works interleaved with all the debug logging and prints that are in your app.

Look in .../examples/system/console for help figuring it out.

John A

User avatar
mzimmers
Posts: 643
Joined: Wed Mar 07, 2018 11:54 pm
Location: USA

Re: I need to read input from the user console

Postby mzimmers » Mon Mar 26, 2018 4:29 pm

Yeah, using the console for application I/O can clutter the screen and cause confusion, I agree.

I'd be happy to hook up teraterm or PuTTY or something else. Are there instructions somewhere for redirecting the app_main() I/O to such a device?

User avatar
mzimmers
Posts: 643
Joined: Wed Mar 07, 2018 11:54 pm
Location: USA

Re: I need to read input from the user console

Postby mzimmers » Mon Mar 26, 2018 6:19 pm

I incorporated the example C code into my own project. I'm missing a few functions like "esp_console_get_completion" at link time.

I looked at the map file for the example project and this function is found in build/console\libconsole.a. My libconsole.a doesn't have that entry.

I'm fairly new to the modern make environment, so forgive what must be a simplistic question: how do I tell make that I want this function?

Thanks...

User avatar
fly135
Posts: 606
Joined: Wed Jan 03, 2018 8:33 pm
Location: Orlando, FL

Re: I need to read input from the user console

Postby fly135 » Mon Mar 26, 2018 6:56 pm

That function is in .../components/console/command.c

I use Notepad++ in windows for all my editing. It has a great "find in files" search feature that allows me to locate things in files very easily. It located that function in the SDK in a snap.

When using a terminal program you just open the com port right after the download says it's complete and prints "Hard reseting..."

Should also mention that if you are mixing C and C++ you can run into an issue like this. C++ changes the names of functions and will not see that function in a lib originating from a C file unless the extern declarations are surrounded with extern "C" { <declares or includes> }

John A

Who is online

Users browsing this forum: Google [Bot] and 210 guests