Post your best Lua script examples here

User avatar
By mariusb57
#33238 I am beginner in Lua programming and I wrote a simple program for an ESP with NodeMCU, that counts pulses for a household application. Each time the monitored device generates pulses, they were taken with an optocoupler connected to ESP 01 and the ESP transmit information to a home automation server that processes them. Everything seems to be OK , but after two or three sessions of pulse reading, the circuit is blocking. I suspected a hardware problem and I tried on all of ESP 01, ESP 05 and ESP 07, same result . The circuit don't loses connectivity with the network ... just stop counting, sometimes loses network connectivity . Maybe it's a programming mistake ... but then it would not work from the beginning. I use NodeMCU custom builds and the heap is aprox 30000 after charge de code .Could anyone help me with a suggestion. Thanks .
Code: Select allwifi.setmode(wifi.STATION)
 cfg =
  {
    ip="192.168.1.67",
    netmask="255.255.255.224",
    gateway="192.168.1.65"
  }
wifi.sta.setip(cfg)
wifi.sta.config("SSID","password")
wifi.sta.connect()   

count = 0
state = 0
gpio.mode(5,gpio.INT,gpio.PULLUP)

function last ()
        cant = count*0.0114
        conn:send("last run="..cant.."")
        conn:send("state= off")
      conn:close()
        state = 0
       count = 0
        end
function counter()
      if state == 0 then
     conn=net.createConnection(net.TCP, 0)
      conn:on("receive", function(conn, payload) print(payload) end)
      conn:connect(80,'192.168.1.70') 
      conn:send("state= on ")
     delay = 0
     state = 1
     count = 1
      else
      count = count + 1
     tmr.delay(3000000)
     end
      tmr.alarm( 1,25000,0,function() last() end )
   end
gpio.trig(5, "down",counter)
Last edited by mariusb57 on Tue Nov 10, 2015 2:52 am, edited 1 time in total.
User avatar
By TerryE
#33458 Read link below carefully, and read it again.

Don't use tmr.delay as this will kill your TCP stack
Beware of relay bounce -- just posted another post on this.
TCP events are async and in general if you do 2 in one callback that your code will barf. Use the socket:on() events to sequence your TCP I/O. I need to put a better worked example in my FAQ because I keep having to explain this.
User avatar
By mariusb57
#33599 Thank you for your response TerryE . As I have said ...I'm a newbie but try to learn ...I rewrote the code considering your observations...maybe I can not fully succeeded in this . But still it shows the same problems: it count a session or two right (about 300 pulses per session) then freezes or count incorrect . I insist on the idea of correct code, because I suspect hardware problems. I wrote the same code in C, for ESP8266 Arduino ... it works ... but face similar problems, periodically restarts. I tried many board ESP01, ESP 07 ...same problems . Any suggestion is welcome . Thanks .
This is the new code:
Code: Select alldelay = 0
state = 0
gpio.mode(5,gpio.INT,gpio.PULLUP)

function client()
      conn=net.createConnection(net.TCP, 0) 
      conn:connect(80,'192.168.1.70') 
      conn:send("state= "..state..", last run= "..cant.."")
      end
function today ()
        cant = count*0.0114
       state = 0
      client()
       count = 0
        end
function counter()
      if state == 0 then
     cant = 0
     state = 1
     count = 1
     client()
      else
      x = tmr.now()
      if x > delay then
      delay = tmr.now()+3000000
      count = count + 1
     print(count)
     end
      end
      tmr.alarm( 1,25000,0,function() today() end )
   end
gpio.trig(5, "down",counter)