You’ve already learned in the previous tutorials that all the configuration of the esp-idf framework is done using a configuration menu:
You can use the same menu to set options/parameters for the program you’re developing: in today’s tutorial I’m going to explain how to do it!
Kconfig
The framework‘s config menu is configured via some text files. The syntax of those files is similar to the one used to configure the Linux kernel, known as the kconfig language.
To be able to add your own configuration items, first you have to create a file named Kconfig.projbuild in the same folder of your source code:
All the content of the file is enclosed by the following two lines:
menu "Nome menu" [...] endmenu
which define the name of the menu that will group all your custom parameters. For example is you type
menu "Custom configuration"
you’ll see:
The most common configuration parameters have the following structure:
config <parameter name> <parameter type> <prompt> range min max default <default value> help <help text>
Parameter type can have the following values:
- string, text parameters
- int, parameters with integer values
- hex, parameters displayed in hexadecimal format
- bool, parameters with boolean values
If for example you need to configure the TCP port a server is listening on, you can define the corresponding parameter as it follows:
config TcpPort int "Server TCP port" range 1 65535 default 80 help Enter the TCP port the server is listening on
You can also create menu items with multiple choices:
choice config <choice_1> bool <prompt> config <choice_2> bool <prompt> ... default <default choice>
Once configured, the parameter values are saved in the sdkconfig file within the project’s main folder:
The name of the constants that correspond to the different parameters is CONFIG_<parameter name>. For the example above, the constant that corresponds to the TCP port you have to use in your program is CONFIG_TcpPort.
In my Github repository you can find a complete example. The Kconfig.projbuild file defines some parameters to demonstrate the use of the different types:
Once configured, you can find the parameters’ values in the sdkconfig file:
and if you run the program, the values are also displayed on the serial port:
Conclusion
The ability to extend the use of the menuconfig of the framework to include also custom parameters of your program allows to configure all the aspects of your project in a handy, unified way. In the previous tutorial the configuration of the wifi network (SSID and password) was hardcoded in the source of the program: a better choice is now to move those parameters out of the code and insert them in the configuration menu… as exercise, try to do it!
Salve!
Innanzitutto complimenti per il tutorial!
Il menu di configurazione del mio esp32 non presenta la voce “custom configuration”.
Si presenta come la prima foto che compare in questa pagina.
Come mai?
Mi servirebbe proprio una funzionalità per assegnare dei parametri non hardcoded e non so come fare.
Ciao Susanna, hai creato il file Kconfig.projbuild con i parametri che vuoi configurare tramite menuconfig? Verifica che sia nel path corretto…
Grazie!
Ho notato che l’intero sistema di sviluppo Espressif è piuttosto “frammentato”, ed è difficile trovare le informazioni che servono velocemente.
Con questa tua pagina mi hai risolto un problema velocemente!
Il guaio è che nel progetto che sto sistemando ci sono due Kconfig.projbuild, e vengono entrambe aggiunti nel menuconfig! Sto cercando di rimendiare…