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) end
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.