Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- counterPIN = 4 --GPIO2
- counter = 0
- delay = 0
- couterDATfile = "counter.dat"
- header = "HTTP/1.0 200 OK\r\nContent-Type: text/plain\r\nPragma: no-cache\r\n\r\n"
- wifi.setmode(wifi.STATION)
- wifi.sta.config("MeLoNWiFi2", "haslo")
- wifi.sta.autoconnect(1)
- function f_saveCounter()
- file.open(couterDATfile, "w+")
- file.write(counter)
- file.close()
- end
- function f_loadCounter()
- if file.open(couterDATfile, "r") ~= nil then
- counter = file.read()
- file.close()
- end
- end
- function f_counter(level)
- actTime = tmr.now()
- if actTime > delay then
- delay = actTime + 95000
- counter = counter + 1
- f_saveCounter()
- end
- end
- f_loadCounter()
- gpio.mode(counterPIN, gpio.INT)
- gpio.trig(counterPIN, "down", f_counter)
- srv=net.createServer(net.TCP)
- srv:listen(80,
- function(conn)
- conn:on("receive",
- function(conn, payload)
- if string.find(payload,"/getcounter") then
- conn:send(header..counter)
- else
- retval = "Energia elekryczna: " ..(counter/1000).."kWh\n"
- conn:send(header..retval)
- end
- end
- )
- conn:on("sent",
- function(conn)
- conn:close()
- end
- )
- end
- )
Advertisement
Add Comment
Please, Sign In to add comment