So while it's quite simple to set up an IFTTT trigger as a web request to a publicly accessible URL, in order to target the ESP8266 sitting on my local network I'd have to "expose" it using port forwarding and a Dynamic DNS service in order to translate the dynamic IP assigned by my ISP into a known domain name.
...Or alternatively, to avoid drilling holes in my firewall, I could use an agent that the ESP8266 can communicate with over a TCP connection. Such an agent would offer a public URL for posting triggers from IFTTT and another URL that could be interrogated by the ESP8266 to listen for the triggers.The best candidate I can find for such an agent is dweet.io
The only issue (and I'm not sure if it's a show-stopper) is that I don't want to poll the agent, but make use of the real-time subscriptions available to dweets. But this uses "chunked" HTTP responses and I'm not sure how to handle these.
Dweeting is really free and easy to use (no sign-up, but security has to be paid for). An example of creating a dweet might be:
https://dweet.io/dweet/for/my-thing-name?light=on
my-thing-name being a unique string. Then to "listen" for such dweets a GET web request is issued to the URL:
https://dweet.io/listen/for/dweets/from/my-thing-name
Simples!
--dweet.lua
wifi.setmode(wifi.STATION)
wifi.sta.config("ssid","password")
wifi.sta.connect()
tries=0
tmr.alarm(0, 1000, 1, function()
if wifi.sta.getip() == nil then
tries=tries+1
if tries>10 then
tmr.stop(0)
print("unable to get onto LAN after 10 seconds")
end
else
tmr.stop(0)
--hook up with dweet.io
if cn then cn:close() cn=nil end
cn = net.createConnection(net.TCP, 0)
cn:connect(80,"dweet.io")
cn:on("receive", function(cn, pl) print("RX:",pl) end)
cn:on("connection", function(cn,pl)
-- Wait for connection before sending.
cn:send("GET /listen/for/dweets/from/am3naY3ui5ap044siUo3 HTTP/1.1\r\nHost: dweet.io\r\ncontent-type: application/json\r\nConnection: keep-alive\r\n\r\n")
end)
cn:on("sent",function(cn) print("SENT") end)
cn:on("disconnection", function(cn) print("DISCONNECT") end)
end
end)
Which goes like this:
> dofile('dweet.lua')
> SENT
RX: HTTP/1.1 200 OK
Access-Control-Allow-Origin: *
Content-Type: application/json
Transfer-Encoding: chunked
Date: Sat, 13 Feb 2016 14:06:30GMT
Connection: keep-alive
78
72
"{\"thing\":\"am3naY3ui5ap044siUo3\",\"created\":\"2016-02-13T14:06:52.577Z\",\"content\":{\"Light\":\"on\"}}"
All the while the connection is open, additional "chunks" come in asynchronously as they are dweeted:
RX:78
72
"{\"thing\":\"am3naY3ui5ap044siUo3\",\"created\":\"2016-02-13T14:07:18.136Z\",\"content\":{\"Light\":\"off\"}}"
Great! but then after what appears to be a minute elapsing since the last response (or the GET request if no response arrives within a minute) the disconnection callback fires and the fun stops unless I make the GET request once more.
I would like to know "who" is instigating this one-minute time-out and how to leave the connection open