What i have seen and currently implemented in my Lua scripts running on my ESP8266 WiFi module, are polling the server page every few seconds and than checking whether the value has changed or not. If value has changed than take some action.
Here's my code
tmr.alarm(1,10000, 1, function()
if (wifi.sta.getip() == nil) then
print("IP unavaiable, Waiting...")
else
tmr.stop(1)
tmr.alarm(1,10000, 1, function()
event_driven_func()
end)
end
end)
function event_driven_func()
print("Inside event_driven_func function"..node.heap());
conn = nil
conn=net.createConnection(net.TCP,0)
conn:on("receive", function(conn, payload)
-- local buf = "";
startRead = false
Data = ""
print("payload : "..payload)
for i = 1, #payload do
c = payload:sub(i,i)
if (c=="{") then
startRead=true
end
if (startRead) then
Data=Data..c
end
if (c=="}") then
startRead=false
print("json string detected Do something now ...");
end
end
conn:close()
collectgarbage("collect");
end)
conn:connect(80,"my_server.co.in")
conn:on("connection", function(conn, payload)
conn:send("GET /xyz.php HTTP/1.0\r\nHost: my_server.co.in\r\nConnection: keep-alive\r\nAccept: */*\r\n\r\n")
end)
end
But this is highly inefficient.
How to implement or better a pseduo-code, a event driven code where whenever the value changes there's an interrupt to the code and then event_driven_func executes ?
Or any better efficient way.
Application example :
Say i've number of youtube subscribers showing on a server page, whenever the subscribers change it should make a "POST" web request to one of my json page xyz.php (this will be done using IFTTT ) and then a event will execute the function in Lua code so that value will be reflected.