2

I am trying to understand the use of dofile() in lua. Is is good practice to put into the init.lua file a row of other files with function declarations, wifi initializations and so forth? init.lua:

dofile("InitWifi.lua") 
dofile(helperfunctions.lua") 
dofile(...) dofile(..)

tmr.alarm(0,3000, function()

runprogram()

end)

This would considerably cut down on download time of the program to be tested.

Is the effect of dofile(xyx.lua ) the same as if the xyz.lua code was contained in the "myprogram.lua" file?

I0sens
  • 61
  • 4
  • (Short answer: Yes) You can write different parts of your program in different files. All these parts share the same set of global variables. But each part has its own isolated locals and upvalues. – Egor Skriptunoff Dec 21 '18 at 16:13
  • There is a wealth of information in our FAQ, specifically at https://nodemcu.readthedocs.io/en/master/en/lua-developer-faq/#how-do-i-minimise-the-footprint-of-running-application. We also published a somewhat evolved `init.lua` template example at https://nodemcu.readthedocs.io/en/master/en/upload/#initlua. Nifim's answer is a good one. – Marcel Stör Dec 22 '18 at 21:35
  • For examples of NodeMCU Lua modules take a look at the contributions in https://github.com/nodemcu/nodemcu-firmware/tree/master/lua_modules – Marcel Stör Dec 22 '18 at 21:38

2 Answers2

2

Another way to handle this topic is to use the require function.

require operates similarly to dofile in that it executes a Lua file, but it also has more intelligence built in.

Lua require

Loads the given module. The function starts by looking into the package.loaded table to determine whether modname is already loaded. If it is, then require returns the value stored at package.loaded[modname]. Otherwise, it tries to find a loader for the module.

require is common practice for loading modules, a module is like a library. A benefit to setting up your files as modules is you can avoid overwriting a function from another file, by defining a scope within the module.

Example code of a module foo.lua:

local foo {}

function foo.bar(a)
    local a = x + 7
    print(a)
end
-- More functions defined inside the foo table
-- ...

return foo

Example of using a require with a module:

local f = require("foo")

f.bar(7)
Marcel Stör
  • 22,695
  • 19
  • 92
  • 198
Nifim
  • 4,758
  • 2
  • 12
  • 31
  • Thanks very much to everybody! I will have to research the "require" statement. I don't really understand the difference of dofile vs require just yet – I0sens Dec 24 '18 at 03:03
0

general practice of init.lua is to execute the main Lua file after a small test time.

countdown = 5
tmr.alarm(0,2000,1,function()
if APPLICATION_REPROGRAM == nil then
    print("coutdown : "..countdown)
    countdown = countdown-1
    if countdown<1 then
        tmr.stop(0)
        countdown = nil
        local s,err
        if file.open("main.lua") then
            file.close()
            s,err = pcall(function() dofile("main.lua") end)                   
        end
        if not s then print(err) end
    end
end
end)

If you need to have more variables and functions into the global environment before the main.lua executed, you can simply add them using require inside the main.lua

local initWifi = require 'InitWifi.lua'

it will do the same as dofile() but with more control and err handling.

you also can use the functions in the initWifi.lua inside main.lua:

initWifi.helperFunct(param1)
Thevananthan
  • 15
  • 1
  • 5