Writing init.lua
Posted: Wed Jun 10, 2015 4:10 pm
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
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:
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:
So I can recreate this (cool "telnet into Lua") setup easily on different chips.
Now I have two questions:
Hermann.
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
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 all
file.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 all
file.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.