1

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.

aditgupta100
  • 21
  • 2
  • 7
  • Sorry, don't understand the problem. However, anything inside `event_driven_func()` except `conn:connect(80,"my_server.co.in")` (and the print out) must only be executed once, no need to do that every time. – Marcel Stör Jun 01 '17 at 10:00
  • If there's a change in the server how'll i get to know without making the connection again and doing conn:on("send") , conn:on("receive") again & again. So just to avoid this thing i want it event driven. – aditgupta100 Jun 06 '17 at 06:33
  • You create the connection "object" once, you register the on-receive handler once, you register the on-connection handler once then you call `connect` as often as you like. – Marcel Stör Jun 06 '17 at 06:54
  • It gives me an error calling connect again "already connected". How can i disconnect and re-connect. Apart from that, the heap usage is same in both the case.I thought that heap usage will be less, as stack will be freed. – aditgupta100 Aug 06 '17 at 18:22
  • So if you could have your PHP file emit an MQTT message. I have a home-automation system where a button press emits an MQTT message and the target switch parses it and turns the light on/off. My answer is here: https://stackoverflow.com/questions/41776063/making-a-ifttt-enabled-home-automation-system-with-raspberry-pi?rq=1 – alok Nov 01 '17 at 10:47

0 Answers0