ESP8266 LUA editing tool

Need a serial terminal to help program the ESP8266 in LUA?

Well, I certainly did as even the rather nice Coolterm was driving me nuts. As of the latest version, we have the basics of an excellent programming tool in the Lua Interpreter for the ESP8266 boards. All you have to do is use a file like FLASH DOWNLOAD TOOL to blow the LUA interpreter onto the board then it’s a matter of sending information via serial to the board and watching the responses.

Except for some peculiarities – the board is chronically short of RAM and sending functions directly eats up more of it… one way to get around this and also to ensure that your functions are set in the board “permanently” – ie stored in FLASH, is to write them as files. But that can get a little messy. It is sometimes better to store functions in “files” – these are then activated by the “dofile” command. For more on that see the LUA documentation.

One particular file you will NEED is the “init.lua” file – because whatever is in there will be run when the board powers up! If you want a little remote light controller you are going to need the board to power up, connect to your router and start some code ALL ON IT’S OWN.

In my case the code is simple..

print “Pete’s LUA module 0.4”
tmr.alarm(4000,0,function() dofile(“thelot.lua”) dofile(“mylistener.lua”) end

WHAT?? What on EARTH was that all about???

Well, on powerup I want the interpreter to say “Pete’s LUA module 0.4” – I then want it to wait for 4 seconds… and call a stored function (one I stored) called “thelot.lua”. That function will check to see if the board connected is actually talking to my router – and if not – will run code to make that happen. It will then call another stored function I wrote called “myslistener.lua” which will go off and listen for commands coming in front the Internet.

AND THAT’S FINE, I’ll not detail those functions here, that’s for another place… BUT you have to get this information and the other functions (which are a lot bigger than this one)  INTO the Lua interpreter via the serial port.  Using Coolterm you could do this by pasting in the code from, say, NOTEPAD.. but there’s a problem – that stuff will run there and then – it won’t wait till powerup!!

SO to delay the inevitable, you make it a FILE by wrapping your code inside file commands.. You firstly ensure the file does not exist by erasing it.

file.remove(“init.lua”)

If it does not already exist, no problem. Then you create the file.

file.open(“init.lua”,”w”)

Easy enough, when you’re done you close it.

file.close()

Also easy.. but the bit in the middle – my code above needs wrapping in “file.writeline([[“ and “]])” and that gets messy. See the final code you need to enter into the LUA interpreter.

file.remove(“init.lua”)
file.open(“init.lua”,”w”)
file.writeline([[print(“Pete’s LUA module 0.3”)]])
file.writeline([[tmr.alarm(4000, 0, function() dofile(“thelot.lua”) dofile(“mylistener.lua”) end )]])  
file.close()

In this example it’s not too bad… but it makes the code harder to read. What would be nice would be if Coolterm could spot that you’re in a file and add that info for you. Also what about comments

–this is a comment

or blank lines.. here’s what you might have

— My startup file – this needs loading into LUA

    file.remove(“init.lua”)
    file.open(“init.lua”,”w”)
    file.writeline([[print(“Pete’s LUA module 0.3”)]])
    file.writeline([[tmr.alarm(4000, 0, function() dofile(“thelot.lua”) dofile(“mylistener.lua”) end )]])  
    file.close()

— All done.

So here we are, after struggling for weeks, we now have a nice interpreter for our little boards and we can start to get ambitious – but the tools for sending this to the board arent’ really up to the job and if you’re a Windows person you don’t want to go messing with command line stuff.

SO I wrote this little serial terminal specially for the job – to make life easier for myself. It started off simple enough but like all things I got ambitious. The terminal will now take your code, strip out leading and trailing spaces, strip out comments, it will add delays between each line to make sure the LUA interpreter doesn’t get overloaded, it will spot that you planned to send stuff as files and will add the relevant code.

So this is what you send.

— My startup file – this needs loading into LUA

    file.remove(“init.lua”)
    file.open(“init.lua”,”w”)
    print(“Pete’s LUA module 0.3”)
    tmr.alarm(4000, 0, function() dofile(“thelot.lua”) dofile(“mylistener.lua”) end
    file.close()

— All done.

That’s a bit neater.. and this is what will actually appear in the window showing feedback from the board.

file.remove(“init.lua”)
> file.open(“init.lua”,”w”)
> file.writeline([[print “Pete’s LUA module 0.4”]])
> file.writeline([[tmr.alarm(4000, 0, function() dofile(“thelot.lua”) dofile(“mylistener.lua”) end )]])
> file.close()
>

And that, is that. The installation files are below, no source code, no support and no I’m not spying on you… I wrote this for me and will develop it as the need to do so arises. Works on Windows 7 and 8 is all I can tell you.

Here’s a screenshot

tmpD4BC

And here’s the code link. Enjoy. Note this will NOT work on XP as apparently said operating system does not support .NET 4.5 – sorry.

Incidentally you RUN files – i.e. make the Interpreter load up the code with the “dofile” function. For example dofile(“init.lua”) – in the case of this particular file you don’t have to worry as it will automatically be loaded on power up). All of this is detailed with the LUA interpreter.

Of course, you don’t need to be using LUA to enjoy this – you could be using the AT demo… or something else – but I wrote it to help with LUA programming.  Did I miss some really neat, simple feature?

12:50AM – Update – I’ve added tooltips, turned the format on by default, replaced the original simple automcomplete with a new version with most of the AT commands and some Lua commands all in there and put a check in for opening a dead port. The project now has a sensible name and will create a desktop icon.

4 thoughts on “ESP8266 LUA editing tool

  1. Hi Peter

    Recently you have talked about the desire to see a ESP-01 type board with level shifters on serial in and reset so that the board is usable on 5v systems without bodging.

    You have also said that you would like to have more of the GPIO pins available.

    You also blogged “A little further down the line I can see a session with EAGLE PCB coming on”

    Have a look at this little beauty :- http://olimex.wordpress.com/2014/11/28/esp8266-update-the-ultimate-development-boards-for-esp8266-are-in-stock/

    OK it does not have the level shifters but it seems to have all the pins available and is price comparable with the ESP-03, but without the stupid 2mm SMD form-factor.

    BUT, best of all, this is Open Source Hardware! The Eagle CAD files will be available for you to modify to your hearts content.

    At present the resouce links on the site do not work, I would expect this to be corrected in the next 24 hours.

Leave a comment