Post your best Lua script examples here

User avatar
By rajbadri
#21267 i am using the following lua scripts and a pushover.php script on my server to get pushover notifications for button push. Every thing works fine but i get a lot of false notifications (specially when i open-close the tube light and fans. please guide
on the esp8266

init.lua
Code: Select allprint('init.lua')


print("Setting up WIFI...")
wifi.setmode(wifi.STATION)
--modify according your wireless router settings
wifi.sta.config("router-ssid","password")
wifi.sta.connect()
tmr.alarm(1, 1000, 1, function()
if wifi.sta.getip()== nil then
print("IP unavaiable, Waiting...")
else
tmr.stop(1)
print("Config done, IP is "..wifi.sta.getip())


tmr.alarm(0,1000,0,function() dofile('main.lua') end)
end
end)


main.lua
Code: Select allpin = 4 -- 3 = GPIO0, 4 = GPIO2
firstrun = true
gpio.mode(pin,gpio.INPUT)

function pinsendrequest()
   print("main.lua")
   dofile('pushover_notifier.lua')
end     
     
gpio.trig(pin, "up", pinsendrequest)
 


pushover_notifier.lua
Code: Select allprint('pushover_notifier.lua started')

conn = nil
signal = 'Good'
ssid='smartland'
Tstart  = tmr.now()
conn=net.createConnection(net.TCP, 0)

-- show the retrieved web page

conn:on("receive", function(conn, payload)
     print(payload)
     if string.match(payload, 'Button Received', 0) then   -- change this to the expected reply
       
     end
     end)

-- when connected, request page (send parameters to a script)
conn:on("connection", function(conn, payload)
     print('\nConnected')
     conn:send("GET /pushover.php?"          -- change this to your script
      .."TIME="..(tmr.now()-Tstart)
      .."&HEAP="..node.heap()
      .."&SSID="..ssid
      .."&SIG="..signal
      .."&ID="..node.chipid()..'.'..node.flashid()
      .." HTTP/1.1\r\n"
      .."Host: host-ip.com\r\n"                     -- change this to your host
      .."Connection: close\r\n"
      .."Accept: */*\r\n"
      .."User-Agent: Mozilla/4.0 (compatible; esp8266 Lua; Windows NT 5.1)\r\n"
      .."\r\n")
     end)
-- when disconnected, let it be known
conn:on("disconnection", function(conn, payload)
      print('\nDisconnected')
         end)
     
                                             
conn:connect(80,'host-ip.com')                       -- change this to your host


on the server i am using this php script
pushover.php
Code: Select all<?php
curl_setopt_array($ch = curl_init(), array(
  CURLOPT_URL => "https://api.pushover.net/1/messages.json",
  CURLOPT_POSTFIELDS => array(
  "token" => "apptoken",
  "user" => "usertoken",
  "message" => "hello world",
)));
curl_exec($ch);
curl_close($ch);
?>


esp8266-doorbell.jpg