Post your best Lua script examples here

User avatar
By siklosi
#5830 Hi,
I had existing Arduino project that was using CmdMessenger library. I wanted to replace cable with esp8266 and after some playing I found easiest way to do it. I installed NodeMCU on esp8266 and installed on it two scripts

init.lua
Code: Select allwifi.setmode(wifi.STATION)
wifi.sta.config("ssid","password")
dofile("tel.lua")
tmr.alarm(0, 5000, 0, function()
uart.write(0,"\n\r5,")
uart.write(0,wifi.sta.getip())
uart.write(0,";")
 end )


and tel.lua
Code: Select alls=net.createServer(net.TCP,180)
s:listen(1001,function(c)
    function s_output(str)
      if(c~=nil)
        then c:send(str)
      end
    end
    node.output(s_output, 0)   
    c:on("receive",function(c,l)
      node.input(l)           
    end)
    c:on("disconnection",function(c)
      node.output(nil)       
    end)
    print("Connected\r\n")
end)


when esp8266 boots it connects to my router and sends it's IP to Arduino ("5,192.168.0.10;") IP is printed on LCD (although it's fixed on router by mac address) When connected from PC I send for example uart.write(0,"1,255;) and Arduino receives it. When sending data back to PC from arduino I send "print"123"" (with print and quotes) and python script receives 123. I was looking for some easy solution and could not find it (there is transparent serial mode but I don't think it works in STA mode and telnet server or I just could not make it work) Anyway it works for me and I hope someone will find it useful.
User avatar
By michar71
#5836 I think what you are looking for to pass your UART data through is this:

Code: Select all-- Create UART <-> TCP Server
s=net.createServer(net.TCP,180)
s:listen(telnetport,function(c)
function s_output(str)
  if(c~=nil) then c:send(str) end
end
uart.on("data",s_output,0)
c:on("receive",function(c,l)
  uart.write(0,l)
  end)
end)


This creates a transparent UART bridge. For some reason it spits out some garbage characters on the ESP8266 on startup so make sure the arduino can handle that...
I'm actually trying to get this working with UDP (So TCP is still free for some sort of config-page/commands) via broadcast but no luck so far.

Has anybody else gotten anything working with UDP?