ESP8266 NodeMCU/LUA: Saving, Executing, and Compiling Script Files

(click here to see index of all ESP8266 posts)

In all of my prior experiments, I have simply executed code as I load it to the ESP8266. I have not saved and executed scripts from the ESP8266 Flash memory yet. Today I will experiment with that ability.

I have been using ESPlorer for manipulating the ESP8266. It works pretty well, but there are some slightly clunkiness when accessing the flash file system.

I would expect to modify the source of the script in ESPlorer, periodically save to disk (on my PC), periodically upload the source to a file on the ESP8266, and then periodically execute the file from flash on the ESP8266.

It isn’t totally obvious how to do these operations by themselves. I keep having source uploaded to the ESP8266 and executed when that’s not what I mean to happen.

Only Executing a Script

First thing I want to be able to do is simply execute a script in ESPlorer’s editor. This is done by clicking on the Send to ESP button or pressing ALT-E.


Saving Script to Local Disk

To simply save the script to your PC’s hard disk, do File | Save to Disk. The shortcut is control-S, but I swear I’ve had this shortcut save the file to the ESP as well. It’s working fine now that I’m paying close attension, so, perhaps,  I’m unknowingly typing alt-S which saves the file to the ESP and executes it.




Save Script to ESP8266 Flash Memory

You would think ‘Save to ESP’ would just send the file to the ESP8266 and ‘Save&Run’ would save and run. No. ‘Save to ESP’ saves and runs. ‘Save&Run’ does nothing that I can see.

Instead, make sure the file has been saved to local disk, and click on ‘Upload’. That will upload the disk file to the ESP8266 (it will not upload the editor contents so make sure you save to disk first).


Listing Files in Flash

Use this snippet of code (from the API docs):

    l = file.list();
    for k,v in pairs(l) do
      print("name:"..k..", size:"..v)

I put this in code snippet 0 so I just have to click on a button to get a listing of all files in flash:


Removing Files From Flash

Use file.remove(). For example, to get rid of ‘xyz.lua’, execute the command


To erase everything in flash, click on the ‘Format’ button in the upper right hand corner.

Execute a File in Flash

If the file ‘dofiletest.lua’ is in flash AND that is the same program open in the editor, you can just press the run button:


If you don’t already have the file you wish to execute open, then just type the dofile command in and click on send:


Compile The Script

Compiling a script will normally save space (not in this particular example though, I guess because the script is so short). The sequence of events I found necessary to get it to compile are:

  • Save the script to local disk. Make sure to include the .lua extension.
  • Use the Upload button to upload the .lua file to the ESP8266 flash.
  • Now click on the Save&Compile button

 init.lua – Executing Scripts at Startup

If you want your script to execute automatically when the ESP8266 starts, name it ‘init.lua’.

The danger to doing this is if there is a bug in the script that locks the ESP8266 up then you cannot regain control to remove the script. If this happens, the only way to regain control is to reflash the ESP8266. That would be a headache.

The main recommendation I’ve been seeing is to name your script something like ‘myscript.lua’. Test it fully. Once you are happy with it, call it from init.lua by putting dofile(‘myscript.lua’) in init.lua.

In my opinion, that doesn’t do much to allow you to regain control. I need to research that more and I expect it will be my next topic.


This entry was posted in c-esp8266 and tagged . Bookmark the permalink.

15 Responses to ESP8266 NodeMCU/LUA: Saving, Executing, and Compiling Script Files

  1. Curt Wells says:

    Can you load multiple files into flash by just issuing consecutive “save to esp” ? Also, I have been trying to use a program that sends an email. It has a require(base64) statement, which I understand to be a library module with encoder/decoder functions…but would you know how to arrange for the esp8266 to access the module at run time ? e.g., load the module into flash…I tried that but the interpreter complained it could not find the module.

  2. Dan TheMan says:

    I’m already getting foggy on using the ESP, but if I recall correctly if you save each file to your PC disk with a different name, then save to the ESP it will work. I believe there is a way to manually do it using open/write/close, but I can’t tell you the specifics off the top of my head – but look around on the web, and the help files – it can be done.

    I haven’t played with Lua beyond what I did here for the ESP, so I don’t know how the require statement works. Take a look at the lua section in ( and maybe you can find the same basic question there. If not, there are lots of knowledgeable people there.

  3. Ravi Dhoble says:

    Hello, I have created init.lua file and it gives me the list of all avalaible access points. I have connected my ESP8266-01 to my home router using function wifi library. my simple program is as follows—- function listap(t)
    for k,v in pairs(t) do
    print(k..” : “..v)
    when i save this to ESP8266… it shows me available access points in my area after “wifi.sta.getap(listap)” command but all the commands below this line are not working . i mean it is connected to digisol but the command “print(wifi.sta.getip())” wont show me ip address immediately after the list of access point then to see it i have to manually send the command “wifi.sta.getip” in the Send window below ESplorer. so how to solve this problem? also i tried to make seperate file to connect and to get ip addresse of connected access points and then included in init.lua as dofile(“myfilename”) but this also not working. the program stops after giving list of all access points.. so how to execute different files using dofile in init.lua? please help me with this… thank you.

  4. xuchen says:

    Thanks for introducing the tool. I have 2 questions: I tried “Save and Compile”, then I can see any compiled file generated. can I download the lua files from ESP board?

  5. James Kyaw says:

    How can I schedule to run dofile(“file.lua”) automatically like a cron job?

    • Dan TheMan says:

      I’m pretty sure you can’t. You would need to just run ‘file.lua’ continuously and have it check the time. Or make your own version of cron as a lua script.

  6. Hi there

    the button Send to ESP seems to be grayed out for me…
    any idea why this may be the case?

    I am using a Cactus Rev02 ESP8266-03

    Thank you

    • Dan TheMan says:

      No idea, but i looks like I was using upload rather than send to ESP.

      • Thanks for your reply! It seems I needed to enable the DTR and RTS button.
        The ESP is finally running scripts from Esplorer now.
        But it seems there is another problem, the print(wifi.sta.getip()) keeps on returning nil even after I enter the SSID and password…
        Have encountered this issue before?

      • Dan TheMan says:

        Not that I recall, but I haven’t messed with the ESP8266 in almost 2 years now. Try googling “wifi.sta.getip() nil”. I see others having the issue.

      • The device seems to be rather temperamental. Looks like it’s working again.
        Did you ever try checking the status of the GPIO pins using lua?
        I can’t seem to get accurate readings on the GPIO pins through lua.

        I have set a press button such that when it is pressed, state 0 should be detected on the GPIO pin and 1 when it is not pressed. The correct data is seen on the serial monitor using the Arduino GPIO pins but with checking GPIO pins via lua, it doesn’t seem to detect the change in state.

      • Dan TheMan says:

        I don’t recall having any such issue. It may be the esp8266 doesn’t have built-in pull-down resistors. You may have to add them yourself.

  7. German says:

    Great guide! Do you know what’s the difference between “send to ESP” and “save to ESP”?
    Send to ESP upload something but somehow I can find the file then!

  8. Mario Laureti says:

    Many thanks, this article saved me so much time!

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s