If you're an embedded C/C++ developer, you might be asking why a high-level language like Lua has any place in an embedded environment. On the other hand, if you're coming from Python, you might wonder what Lua offers that Python doesn't.
Lua brings something unique to the table: unlike most high-level languages, Lua is specifically designed to be embedded as a C library, meaning it integrates seamlessly into C/C++ applications. Running Lua standalone has limited use, but when embedded in a program or framework, like the Barracuda App Server, it offers distinct advantages that make development faster, more adaptable, and far more manageable and secure.
Lua’s lightweight design is ideal for embedded systems with limited memory and processing power. With a minimal footprint, Lua requires only a few hundred kilobytes, allowing it to run efficiently alongside C/C++ code without taxing system resources.
Note: Lua is the only module in the Barracuda Application Server not developed by Real Time Logic. Lua is licensed under the terms of the MIT license and can, therefore, be used in any commercial product without any restrictions. See the Lua license for more information.
Lua's compact size, impressive speed, and engineering excellence make it unique. It is also the fastest non-JIT high-level language. Lua was not designed to be a standalone language; it was crafted to be an embeddable extension language from the ground up.
Lua: As Readable as Pseudocode, but Actually Runs.
In C, you declare variables with a specific data type, like int
, float
, char
, etc.:
int num = 10; char ch = 'A';
In Lua, you don’t need to specify a type; you just declare a variable:
num = 10 ch = 'A'
A string in C:
char* str = "Hello, World!";
A string in Lua:
str = "Hello, World!"
The syntax for an if
statement in C:
if (num > 10) { printf("num is greater than 10\n"); } else { printf("num is not greater than 10\n"); }
The equivalent syntax in Lua:
if num > 10 then print("num is greater than 10") else print("num is not greater than 10") end
A simple for loop in C:
for (int i = 0; i < 10; i++) { printf("%d\n", i); }
The equivalent loop in Lua:
for i = 0, 9 do print(i) end
Defining a function in C:
int add(int a, int b) { return a + b; }
Defining the same function in Lua:
function add(a, b) return a + b end
In C, an array must be declared with a size and type:
int arr[5] = {1, 2, 3, 4, 5};
Accessing an array in C (0-based index):
int first = arr[0];
In Lua, you use tables, which are a combination of arrays and hash maps:
arr = {1, 2, 3, 4, 5}
Accessing a table in Lua (1-based index):
first = arr[1]
Check out our Online Interactive Lua Tutorials for a gentle introduction to Lua.
Lua is a beginner-friendly coding language that is easy for C or C++ developers to learn. It is widely used in frameworks due to its user-friendly nature and is even taught as a first language to children in programs like Roblox. This makes it an excellent choice for teams where not all members may be experienced with hard-core C/C++ programming. Using a high-level language like Lua also allows non-expert developers to work safely with embedded system programming. Learning Lua can be a great way for developers to expand their skills and capabilities.
Do you think Lua is too sluggish for microcontrollers with tight resource limits? Think again! Lua is surprisingly agile and efficient, making it a perfect fit for embedded systems. Check out this video, where we tested Lua on an ESP32 microcontroller.
The Barracuda App Server C code and our version of the Lua C library are designed for easy integration into embedded systems, providing an optional amalgamated version (one C file) that can be easily added to any IDE or Makefile.
When comparing Lua and Python, shifting the focus from the language to the APIs and security features the framework provides is important. Learning a new language is relatively straightforward, but mastering the APIs, where the true functionality lies, is the real challenge. Many developers focus primarily on the language, but it’s the APIs that determine how effectively a platform meets the needs of its intended application. The Barracuda App Server with Lua is explicitly designed for IoT and embedded web solutions tailored to the constraints and requirements of these environments. While we don’t provide a direct API comparison with Python, we do offer one with Node.js, showcasing Lua’s advantages in embedded applications.
Python is a popular data science language and may be the king of the desktop, but Lua is a better choice for embedded systems, as Lua is designed specifically for easy integration into C/C++ programs, including firmware. It has a strong track record in the gaming industry and is known for its speed and efficiency, making it a top choice when performance is a priority. Additionally, Lua is easy for Python developers to learn, as the two languages have many similarities.
Most programming languages have similar basic concepts such as variables, data types, loops, functions, etc. Once you understand these concepts, you can easily apply them to any language. However, learning the APIs (Application Programming Interfaces) specific to the language can be more challenging and time-consuming. To simplify the process of learning our APIs, we have dedicated significant resources to creating user-friendly IoT and web APIs with comprehensive documentation and a wealth of ready-to-use examples. This makes it easier for developers to quickly and effectively integrate these APIs into their projects, saving time and increasing efficiency.
Lua emerges as a compelling choice for embedded systems for the following reasons:
Unlike Python, Lua was purposefully designed with minimal APIs to remain lightweight and highly adaptable for integration into various environments. The Barracuda App Server builds on this foundation by providing a comprehensive suite of IoT and web APIs, often referred to as "batteries", that significantly extend Lua's capabilities. These added APIs empower developers to fully utilize Lua in embedded devices, simplifying the development process and unlocking the many advantages of using Lua for their projects, including flexibility, efficiency, and ease of integration.
When designing embedded systems, Lua shines when used for business logic, taking advantage of its flexibility and simplicity, which are key reasons for its success in industries like gaming. By focusing on Lua's strengths, developers can maximize productivity and ease of development. The Barracuda App Server seamlessly combines Lua with C, enabling developers to handle time-critical computing tasks with C and manage them using Lua. This design pattern leverages the strengths of both languages, simplifying the development of sophisticated embedded systems with IoT and cloud connectivity.
Lua Server Pages (LSP) is a powerful and flexible tool that was initially designed for server-side web development but now provides much broader support for embedded, IoT, and network-related programming. With our innovative LSP engine, it is easy to create your own custom programmable Lua powered edge controller using the Barracuda App Server.
At Real Time Logic, we have found that using Lua has greatly simplified our own development process and has allowed us to build complex protocol stacks more quickly. We have used Lua to expedite the development of a range of protocols, including:
Lua has allowed us to focus on delivering high-quality products and solutions to our customers rather than spending unnecessary time and resources on low-level coding tasks. Whether you're working on a small-scale project or a large enterprise deployment, using Lua can help you streamline your development process and bring your products and solutions to market faster.
If you're interested in learning more about how Lua Server Pages (LSP) can be used to fast-track embedded web application development, we encourage you to read the following articles:
The Barracuda App Server is a 100% pure C library that can be integrated into any C application. However, most developers prefer our ready-to-build reference platforms for faster results: use Xedge for RTOS-based systems, and Mako Server for high-level OSes like embedded Linux and QNX.
If you are using an RTOS and are looking for a cost-effective solution for building professional gateway products or developing a powerful edge and IoT foundation, consider using our ready-to-run Barracuda App Server build files for NXP's crossover MCUs. This ready-to-use project is easy to use and can help you quickly and easily build high-quality, feature-rich applications and products quickly and easily. Alternatively, if you are exploring and/or learning, check out our ready-to-use Barracuda App Server project for the ESP32 MCU, which includes ready-to-use Lua code for controlling servos and GPIOs. Regardless of which RTOS platform you choose, the Barracuda App Server will provide a strong foundation for your development efforts, helping you build reliable and scalable RTOS-powered solutions that meet the needs of your organization.
Embedded Linux users can download our Mako Server. We provide ready-to-run binaries for many embedded platforms. You can also cross-compile the code for your own device. See the tutorial Embedded Linux Web Interface Design for details.
The Lua debugger makes it easy to develop and test Lua in embedded systems