# Lua Example This example demonstrates how to embed Lua in an ESP-IDF application. ## Features - **Embedded Lua script execution**: Run Lua code directly from C strings - **File-based Lua script execution**: Load and execute Lua scripts from LittleFS filesystem - **Memory tracking**: Monitor memory usage throughout Lua operations - **Error handling**: Proper error handling and reporting ## Example Contents 1. **Simple Embedded Script**: A basic Lua script that calculates and prints a value (answer = 42) 2. **Fibonacci Script**: A Lua script loaded from filesystem that calculates Fibonacci numbers ## Hardware Requirements - Any ESP32 series board (ESP32, ESP32-S2, ESP32-S3, ESP32-C3, ESP32-C6, etc.) - Minimum 4MB flash (for filesystem partition) - On ESP32-C5 the example turns on PSRAM (`sdkconfig.defaults.esp32c5`). Use a C5 board that actually has PSRAM. ## Configuration - The example uses LittleFS to store Lua scripts - A custom partition table is included with an "assets" partition for Lua scripts - ESP32-C5 enables `CONFIG_LUA_USE_PSRAM` so large Lua allocations prefer PSRAM ## Building and Flashing ```bash idf.py build flash monitor ``` ## Expected Output ``` I (xxx) lua_example: Lua Example Starting I (xxx) lua_example: Initializing LittleFS filesystem I (xxx) lua_example: Filesystem mounted at /assets I (xxx) lua_example: Starting Lua test: Simple Embedded Script The answer is: 42 I (xxx) lua_example: End of Lua test: Simple Embedded Script I (xxx) lua_example: Starting Lua test from file: Fibonacci Script from File Fibonacci of 10 is: 55 I (xxx) lua_example: End of Lua test from file: Fibonacci Script from File I (xxx) lua_example: End of Lua example application. ``` ## Libraries The example opens only `base`, `math`, `string`, `table`, and `utf8`. It does not call `luaL_openlibs()`: that helper (and `luaL_openselectedlibs()`) keeps a table of every `luaopen_*` pointer, so the linker cannot drop unused libraries. ## Customization You can add your own Lua scripts to the `assets/` directory and execute them from your application code. ## See Also - [Lua Component Documentation](../../README.md) - [Lua Official Website](https://www.lua.org/) - [Lua Reference Manual](https://www.lua.org/manual/)
To create a project from this example, run:
idf.py create-project-from-example "espressif/lua=5.5.1:lua_example"