Need an example

I need an example of an agent that will trigger (via http) an action on an imp (say read hardward.voltage) and then return that value to the http requester. Sort of like this:

agent - accept http request from web client
agent - send command to imp
device - accept request
device - perform action
device - send result of action
agent - accept result from imp
agent - send http response to web client

I understand how to get a value from an imp or how to send a command to an imp but I don’t see how to combine the two into one http request/response.

@beardedinventor’s non-blocking HTTP request example code:

Hey there, this link is not working. Any chance to find a similar example on this?

Thanks for your time :slight_smile:


So you’re inquiring about an old post on this forum?
I assume you are looking into the same topic.

You have a website on the internet somewhere that you will use PHP or some server-side scripting to request information (or hardware status) from an imp?

Just making sure you are asking for the correct thing.

1 Like

@mlseim yes, sort of (judging from what the OP is attempting to do).

A round trip starting from HTTP, going for the agent, then device returning a value to the agent, then agent sending back results to the HTTP. I already took care of the part of HTTP to Agent to Device, then device to agent. But I’m still missing the part in which I print the results I need in the HTTP side.

I’ve been trying to send them using “Response”, but that can only send one string. I need to try to send several (if requested).

You can cache the HTTPresponse object and make use if it when the agent has all of the data it needs from the agent. If you need to send multiple strings, probably the best way is to send them in a JSON string: { "strings" : [ stringOne, stringTwo... ] }:

// 'strings' is an array that's appended each time the device sends 
//           on to the agent
// 'savedResponse' is the cached HTTPresponse from the original 
//                 incoming request
local jsonTable = {"strings":strings};
local jsonString = http.jsonencode(jsonTable, {"compact":true});
savedResponse.send(200, jsonString);
1 Like

The imp and PHP (if you’re using that) play really well with JSON. Both have good functions to encode and decode JSON. Much better than XML.

1 Like

@smittytone @mlseim hey guys,

yes, that was the approach I used at the end, I reported it in my post here: Sending several string arrays through http.onrequest()

Thanks for the feedback! Always useful :slight_smile: