Post your best Lua script examples here

User avatar
By fabix68
#33884 Hi, I thought of using a esp - 12 to transmit through the service Pushover states ( arm / disarm / fault / alarm ) of a central anti-theft Bentel .
Using open collector outputs present in alarm system, appropriately configured , connected to 2 pin of the ESP, everything work properly ( the pins are configured with internal pullup resistor and managed via an interrupt ) those directly connected to the central alarm without any further resistance.
Unfortunately after a few hours of regular operation of the system it seems to be immune to interrupts .
The chip regularly responds to the ping , I see it connected to the router but it ignores the change in the states .
If restart all back to work .

Below the code used , still very crude because in testing .
Thank you
NodeMCU version 0.9.5 build 20150318 powered by Lua 5.1.4

Code: Select allprint('Pushover.lua started')
-- Config Variables
po_token="xxxxxxxxxxxx"
po_user="xxxxxxxxxx"
po_message="Allarme !!!!!"

-- Setto i pin 5 e 6 come ingressi INTerrupt con PULLUP
gpio.mode(5,gpio.INT,gpio.PULLUP)
gpio.write(5,gpio.HIGH)

gpio.mode(6,gpio.INT,gpio.PULLUP)
gpio.write(6,gpio.HIGH)


function callpushover(po_string)
conn = nil
conn=net.createConnection(net.TCP, 0)
conn:on("receive", function(conn, payload)
   success = true
   print(payload)
   end)
-- when connected, send stuff
conn:on("connection", function(conn, payload)
      print('\nConnected')
      conn:send("POST /1/messages.json HTTP/1.1\r\nHost: api.pushover.net\r\n"
      .."Connection: keep-alive\r\nkeep-alive: 1\r\nPragma: no-cache\r\n"
      .."Cache-Control: no-cache\r\nContent-Type: application/x-www-form-urlencoded"
      .."\r\nContent-Length: "..post_length.."\r\nAccept: */*\r\n\r\n"
      ..po_string
      )
   end)
conn:close()
-- when disconnected, let it be know
conn:on("disconnection", function(conn, payload) print('\nDisconnected') end)
conn:connect(80,'api.pushover.net')
end

function debounce(func)
    local last = 0
    local delay = 2000000
    return function (...)
        local now = tmr.now()
        if now - last < delay then return end

        last = now
        return func(...)
    end
end

function pin_5()
   print('Rilevato valore a 0 sul pin 5')
   po_title="Attenzione impianto in allarme sul pin 5"
   po_sound="siren"
   po_priority="2"
   po_retry="30"
   po_expire="300"
   po_string="token="..po_token.."&user="..po_user.."&message="..po_message.."&title="..po_title.."&sound="..po_sound.."&priority="..po_priority.."&retry="..po_retry.."&expire="..po_expire
   post_length=string.len(po_string)
   callpushover(po_string)
end

function pin_6()
if gpio.read(6) == 0 then
   print('Rilevato valore a 0 sul pin 6')
   po_title="Attenzione impianto inserito"
   po_sound="Piano Bar"
   po_string="token="..po_token.."&user="..po_user.."&message="..po_message.."&title="..po_title.."&sound="..po_sound
   post_length=string.len(po_string)
   callpushover(po_string)
 end
 if gpio.read(6) == 1 then
   print('Rilevato valore a 1 sul pin 6')
   po_title="Attenzione impianto disinserito"
   po_sound="Piano Bar"
   po_string="token="..po_token.."&user="..po_user.."&message="..po_message.."&title="..po_title.."&sound="..po_sound
   post_length=string.len(po_string)
   callpushover(po_string)
 end
end

gpio.trig(5, 'down', debounce(pin_5))
gpio.trig(6, 'both', debounce(pin_6))