Newb Agent question

I have a question that may have a simple solution but it’s got me stumped so please let me know if you have a suggestion…

Code flow is;

  1. http.onrequest //incomming http request
  2. device.send // ask the device for a pin status
  3. device.on //receive the pin status back

At this point I am stuck. I want to still be able to see the http.onrequest(request.body) so I can respond to the http based on the pin value, but I exited that function at step 2.

Does anyone have some example code showing dynamic agent html response based on device pin status ?


I’m not sure that you have to do this, but you can wrap device.send and device.on in your http.onrequest function. Then in your device you receive with agent.on, check your pin state, and reply with agent.send. That will be picked up by device.on, and then you can finish the http.onrequest with a response. If you want to respond with HTML, you’ll need to have that as a properly formatted string. The Electric Imp GitHub page has lots of reference code… the Snackbot example has HTML reference in it.

one example is here - the post by beardedinventor


The response is saved into a variable so that it can be used to make the reply when the device.on runs

interesting news from jwehr that the device.on can be put inline with the http.onrequest function. I wonder how that works? guess I’ll play with that idea later.

I assume @beardedinventors code is non-blocking, though I’ve never had a problem with my garage door code, and I don’t queue requests. I can send a request for the door status and immediately send a request to toggle the door. I guess I could throw some log statements in there and see how it plays out.

The example linked to above is non-blocking!

I try be best to write all of my code such that it’s non-blocking (unless I specifically want it to block - which is rare).

Thanks for the speedy response ! Worked perfectly. Cheers.