-->
Page 1 of 2

Writing init.lua

PostPosted: Wed Jun 10, 2015 4:10 pm
by HermannSW
Hi,

I am new to Lua, did flash latest NodeMCU today for the first time (until now I did use the Arduino ESP8266 IDE).

Before Lua interpreter I did use Arduino ESP8266 IDE "Examples -> ESP8266 WiFi -> WiFiTelnetToSerial" to connect to a Arduino Pro Mini with Bitlash command interpreter running.
Today I got rid of the Arduino by using NodeMCU:
http://www.esp8266.com/viewtopic.php?f=15&t=3525
Image

I made use of "pure lua telnet server" and was impressed how easy it is to remotely execute Lua commands:
http://www.nodemcu.com/index_en.html#fr_54900e2bf6fa847bcb00004c

I wanted my ESP8266-01 be the access point, and the telnet server be in place after chip reset.
I learned that I can do that by writing "init.lua" file.

My first attempt was to do it line by line, just omitting the comment lines:
Code: Select allfile.open("init.lua","w")
file.writeline([[ print("Hello from init.lua") ]])
file.writeline([[ s=net.createServer(net.TCP,180) ]])
file.writeline([[ s:listen(2323,function(c) ]])
file.writeline([[     function s_output(str) ]])
file.writeline([[       if(c~=nil) ]])
file.writeline([[         then c:send(str) ]])
file.writeline([[       end ]])
file.writeline([[     end ]])
file.writeline([[     node.output(s_output, 0)   ]])
file.writeline([[     c:on("receive",function(c,l) ]])
file.writeline([[       node.input(l)           ]])
file.writeline([[     end) ]])
file.writeline([[     c:on("disconnection",function(c) ]])
file.writeline([[       node.output(nil)        ]])
file.writeline([[     end) ]])
file.writeline([[     print("Welcome to NodeMCU world.")]])
file.writeline([[ end) ]])
file.close()
node.restart()


It worked, but having to copy each single line into Arduino IDE Serial Monitor was not nice.

So I decided to make a 1-liner out of it and failed (some error wrt "=" somewhere).
But I was successful with this equivalent 2-liner:
Code: Select allfile.open("init.lua","w") file.writeline([[ print("Hello from init.lua") s=net.createServer(net.TCP,180) s:listen(2323,function(c) function s_output(str) if(c~=nil) then c:send(str) end end node.output(s_output, 0) ]])
file.writeline([[ c:on("receive",function(c,l) node.input(l) end) c:on("disconnection",function(c) node.output(nil) end) print("Welcome to NodeMCU world.") end) file.close() ]]) file.close() node.restart()


So I can recreate this (cool "telnet into Lua") setup easily on different chips.

Now I have two questions:
    * Can this be done with a 1-liner?
    * Can "init.lua" be populated differently/more easily?

Hermann.

Re: Writing init.lua

PostPosted: Wed Jun 10, 2015 4:22 pm
by kriegste
Maybe you can upload the lua file to a webspace and then design an html downloader (which itself fits into a single line).

Re: Writing init.lua

PostPosted: Fri Jun 12, 2015 6:58 am
by HermannSW
Did not find an easier method to write "init.lua".

I did add a 3rd line to "init.lua" for setting the IP address of ESP8266 AP to value different to 192.168.4.1:
Code: Select allfile.open("init.lua","w") file.writeline([[ wifi.ap.setip( {ip="192.168.179.1", netmask="255.255.255.0", gateway="192.168.179.1"} ) ]])
file.writeline([[ s=net.createServer(net.TCP,180) s:listen(2323,function(c) function s_output(str) if(c~=nil) then c:send(str) end end node.output(s_output, 0) ]])
file.writeline([[ c:on("receive",function(c,l) node.input(l) end) c:on("disconnection",function(c) node.output(nil) end) print("Welcome to NodeMCU world.") end) ]]) file.close() node.restart()


But I found that "print(file.read())" allows to print out a file completely, like Linux "cat" command:
http://www.esp8266.com/viewtopic.php?f=22&t=3550#p20195

Hermann.

Re: Writing init.lua

PostPosted: Mon Jun 15, 2015 11:41 am
by dnc40085
I've found that ESPlorer makes life much easier when writing scripts for NodeMCU.