1
votes

I'm working with ESP8266 and I don't want to use Lua for the whole project, I just want to run a few snippets of Lua code, received from wifi/sd card. I'd need to start a Lua environment and run the scripts, which would then eventually call some native functions for low level tasks. In other words, I just want to use Lua as simple scripting language (as it's intended to be) to implement some dynamic behavior. Is it possible? Is there any build of lualib for arduino?

Thanks in advance!

3

3 Answers

1
votes

You can simply embed Lua in a extlibs/ folder for example and link to it when compiling your program. There is existing Lua binaries but building it yourself is easy and better (as it's multiplatform).

1
votes

OK, I know both answers told me I can just embed the code into my project, however, I found out I need to make some small changes. I made an example working project available here and the following list of changes had to be made:

  • The flags LUA_32BITS and LUA_USE_LONGJMP (C exception handling) were enabled
  • The following libraries were excluded: io, os, package, coroutine
  • The following functions were removed from C API: luaL_fileresult, luaL_execresult, luaL_loadfile, luaL_loadfilex, luaL_dofile, luaB_loadfile, luaB_dofile
  • Lua output messages are redirected to the Serial interface, check tinylua.h, tinylua.cpp and lauxlib.h to change this behavior

Hope this helps!

0
votes

The ESP8266 has up to 4MB of program storage. Theoretically you can get up to 16MB as the datasheet specifies.

As I remember, compiling an amalgamated version of Lua (all sources in one file), occupies less than 100kb.

So, you can compile the Lua library and use it as needed on esp8266, even using Arduino IDE.

But you will get NAKED Lua if you do so... No nifty libraries to control Wifi, serial, SD, ports... You would have to provide that in C, or use NodeMCU code as you need.

You can try LuaJIT and access C code directly from Lua, cutting out the need for writing libraries. I have no idea of how you would compile it to Esp8266, or if anyone have tried this before, but you can do it "for science" and tell us how it turned out.