Close

Uploading a .lua file to the esp (nodemcu)

A project log for Fiddling with the esp8266

A place to scribble down my notes when playing with the esp8266 wifi chip. Might be helpfull to others as well...

androidersAndroiders 02/25/2015 at 12:100 Comments

nodemcu is a great firmware, but i need to add programs to it. And I want to make these programs locally and upload them. Lets do that.

First i wrote a simple program (or actually, someone else wrote the program. I stole it and put it in a file on my computer).

This is the program

mycounter=0
srv=net.createServer(net.TCP) 
srv:listen(80,function(conn) 
conn:on("receive",function(conn,payload) 
   if string.find(payload,"?myarg=") then
   mycounter=mycounter+1
    m="<br/>Value= " .. string.sub(payload,string.find(payload,"?myarg=")+7,string.find(payload,"HTTP")-2)
  else 
    m=""
 end
 conn:send("<h1> Hello, this is Androiders's web page.</h1>How are you today.<br/> Count=" .. mycounter .. m .. "Heap="
.. node.heap()) 
 end) 
conn:on("sent",function(conn) conn:close() end) 
end)
I saved it to a file called main.lua

Then i downloaded luatool from here https://github.com/4refr0nt/luatool

Luatool is a tool to put files on the esp where nodemcu can find them and execute them.

The script takes a file (defaults to main.lua) and uploads it and stores it with the same name. So simply executeing ./luatool puts a file named main.lua (in the same dir) on the esp.

After it was done i connected to the serial port again and exectuted node.restart().

Then, the magic comman, dofile("main.lua") executes the file.

Since the program was a small webserver it was easy to verify. First i got the ip adress from the esp using print(wifi.sta.getip()), then i entered that ip in the browser. Voila!

The restart and dofile commands can be set as arguments to the luatool. I will check it out later.


Discussions