Hi, I am trying to do an http.get and halt code execution until I register a response from the call and I log it. Can you help me with the correct code?
local getrequest = http.get("https://maker.ifttt.com/trigger/eventgoeshere/with/key/keygoeshere");
getrequest.sendsync();
Sure. If a certain threshold is met on my temperature sensor. I need the ImpExplorer to (1) hit that URL (not sending data of any sort, just “visit” that link), (2) pause running code until a response is received from ifttt (3) log the response received via a server.log() line of code.
Looking closer at the sample code here (2 methods described) …
You can do the GET and expect back a “200” from ifttt.
I think ifttt sends back a status code of ‘200’ if received successfully.
(that’s pretty much standard for most API’s).
Now, there are two methods…
The imp will do the GET and go on with whatever it is doing.
It will process the status 200 when it arrives.
The imp will do the GET and simply stop until it gets the status 200 from ifttt.
I think you want to do the asynchronous (no. 1) method.
Do the GET and move on with your imp stuff.
When ifttt reports back a 200 (and only if it is a status 200), then log it, or process it as successful.
That “if” statement will execute when the status comes back, even if the imp has moved on to something else.
I believe that ifttt is sometimes lazy or delayed in sending back status. By that I mean 2-6 seconds?
I’ve only used ifttt in the other direction, where ifttt posts to my imp. Doing it your way, where the imp sends to ifttt … I send imp data to my website (PHP scripting to a database). So I cannot attest for how timely ifttt operates.
(and when I keep saying “imp”, I mean the ‘agent’ and the ‘device’ as a whole. The agent is actually what is talking to ifttt).
local request = http.get("https://maker.ifttt.com/trigger/flood/with/key/bfilekjoehiu7878L");
// Send the request synchronously. This will block the imp CPU
// until a response has been received from the remote service
local responseTable = request.sendsync();
if (responseTable.statuscode == 200) {
// Remote service has responded with 'OK' so decode
// the response's body 'responseTable.body' and headers 'responseTable.headers'
// Code omitted for clarity...
} else {
// Log an error
server.log("Error response: " + responseTable.statuscode);
}
@roquito,
Glad you showed Hugo the error message. We would not have figured-out that you were doing your HTTP commands within the device. As Hugo mentioned, the “agent” is who communicates with the outside world. Your device (the physical imp) only communicates with the agent.
You want the imp to access IFTTT, so your imp sends its data to the agent (who is always waiting for it), and the agent does the GET or POST to IFTTT.
Your agent is also given its own URL. If you wish to affect something on the imp hardware, you POST to the agent, and the agent communicates with the imp. I like to experiment with the IFTTT webhooks. For example, IFTTT has an app to trigger a remote URL when my cell phone gets within a certain distance of a location (my house in this case). That remote URL is my imp agent. The agent then sends data to the physical imp (device) that turns on an output pin. That turns-on outside lights on my house.