grzegorzch

licznik

Apr 17th, 2015
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.17 KB | None | 0 0
  1. counterPIN = 4 --GPIO2
  2.  
  3. counter = 0
  4. delay = 0
  5. couterDATfile = "counter.dat"
  6. header = "HTTP/1.0 200 OK\r\nContent-Type: text/plain\r\nPragma: no-cache\r\n\r\n"
  7.  
  8. wifi.setmode(wifi.STATION)
  9. wifi.sta.config("MeLoNWiFi2", "haslo")
  10. wifi.sta.autoconnect(1)
  11.  
  12. function f_saveCounter()
  13.     file.open(couterDATfile, "w+")
  14.     file.write(counter)
  15.     file.close()
  16. end
  17.  
  18. function f_loadCounter()
  19.     if file.open(couterDATfile, "r") ~= nil then
  20.         counter = file.read()
  21.         file.close()
  22.     end
  23. end
  24.  
  25. function f_counter(level)
  26.     actTime = tmr.now()
  27.     if actTime > delay then
  28.         delay = actTime + 95000
  29.         counter = counter + 1
  30.             f_saveCounter()
  31.     end
  32. end
  33.  
  34. f_loadCounter()
  35.  
  36. gpio.mode(counterPIN, gpio.INT)
  37. gpio.trig(counterPIN, "down", f_counter)
  38.  
  39. srv=net.createServer(net.TCP)
  40. srv:listen(80,
  41.     function(conn)
  42.         conn:on("receive",
  43.             function(conn, payload)
  44.                
  45.                 if string.find(payload,"/getcounter") then
  46.                     conn:send(header..counter)
  47.                 else
  48.                     retval = "Energia elekryczna: " ..(counter/1000).."kWh\n"
  49.                     conn:send(header..retval)
  50.                 end
  51.             end
  52.         )
  53.         conn:on("sent",
  54.             function(conn)
  55.                 conn:close()
  56.             end
  57.         )
  58.     end
  59. )
Advertisement
Add Comment
Please, Sign In to add comment