Current Lua downloadable firmware will be posted here

User avatar
By hbouzas
#20949 I am trying to call a lua function such as wifi.sta.getip from c. So, how do you load the module wifi and call wifi.sta.getip?
The following code is not working, lua_pcall returns 0

lua_getglobal(L, "wifi.sta.getip"); /* function to be called */
lua_pcall(L, 0,3, 0); /* should return 3 results */

Can anybody help? Thanks
User avatar
By cal
#20970 Moin,

You are writing so little that its difficult to understand what you know and what you don't know.
If you don't know that lua does use the stack for value exchange I suggest reading

viewtopic.php?f=24&t=2526

and some of it's links.

Cal
User avatar
By TerryE
#20979 nodeMCU is essentially a port of eLua. See my links below for more references and read the PiL chapters on this. Also look at how the callback code in the various library modules such as tmr.c do this. But remember that nodeMCU assumes that it is executed through init_user so you will need to extend Lua to add your own hooks or module and then include this type of callback mechanism. Not for a a beginning Lua developer, I think.
User avatar
By hbouzas
#21021 Sorry for being brief, I though less is more, and the question was clear how do you call a lua module and function from c. Got it, and here it is

This will call wifi.sta.getip() and return the 3 values (no error checking)

static int snaptekk_getip_test (lua_State* L )
{
lua_getglobal(L, "require");
lua_pushstring(L, "wifi");
lua_getglobal(L, "wifi");
lua_getfield(L, -1, "sta");
lua_getfield(L, -1, "getip");
lua_pcall(L, 0, 3, 0);

return 3;
}