Web-->Agent-->Device-->Agent-->web communication?

Wondering if there is a way to do this?

I need to get the current value of a device connected to my imp via a web request and send the value back to the web in response from original request.

I can’t seem to figure it out, so for now, I am mimicking some device states on the agent, so it can respond back
to the web in the response to the http.onrequest();

Web sends JSON to agent
agent needs to get values from device and report them back to web in response from original http request.

agent and device seem to communicate asynchronously via device.send() and agent.send() with handlers to get responses, so it’s not like I can call directly to the device and get a value response inline in agent code.

Only thing I can think of is like the following, or am I missing something?



commStat <-0;
curTemp <- 0;
timeOut >- 0;

device.on(“TempSent”, function(v) {
comStat = 1;
curTemp = v;


comStat = 0;
timeOut = 0;
wait for (comStat ==1 || timeOut)
if (!timeOut) {
local data = {“curTemp”:curTemp}
local message = http.jsonencode(data);
res.send(200, message);
else {
res.send(500, “Device timeout”);


agent.on(“getTemp”, function(value)){
agent.send(“TempSent”, getTemp(););

Here is a chunk of code from my garage door project:

else if (data.action == "toggle") { device.send("toggleDoor", data.action); device.on("doorToggled", function(data) { doorState = data; local json = "{ \"status\" : { \"doorState\" : \"" + doorState + "\" }}"; server.log("Response: " + json); response.send(200, json); });

I do device.send and then wait for device.on inline, and wait for the device to reply. In my security system I actually keep copies of the same variables on both the device and the agent and update them, So I don’t need to go to the device for for a simple state request.

Both project’s code are here. https://github.com/joel-wehr?tab=repositories

And I just noticed some bad code… :slight_smile:


I had no idea that device.on() could be used inline.

That’s all I needed to know. I do keep some simple states up to date in the agent, but some exact temperature calculations I want to get “on demand”


@dreslism - here is the code I use when I want to do web -> agent -> device -> agent -> web:

agent code:
`// max round-trip time: agent -> device -> agent
const TIMEOUT = 10.0;

// response queue
HttpResponses <- {};

// Send timeout responses when required
function CleanResponses() {
// get current time
local now = time();

// loop through response queue
foreach(t, resp in HttpResponses) {
    // if request has timed-out
    if (now - t > TIMEOUT) {
        // log it, send the response, then delete it
        server.log("Request " + t + " timed-out");
        resp.send(408, "Request Timed-out");
        delete HttpResponses[t];
// check for timeouts every seconds
imp.wakeup(1.0, CleanResponses);

} CleanResponses();

// sends a response based on a timestamp
function SendResponse(t, code, body) {
// if the response is in our queue (it hasn’t timed out)
if (t in HttpResponses) {
// send it
HttpResponses[t].send(code, body);
} else {
// if it wasn’t in the queue, log a message
server.log(“Response " + t + " not found in response queue”);

// when we get a request
http.onrequest(function(request, response) {
// get current timestamp
local t = time();
server.log("Got a request: " + t);

// add request to request queue
HttpResponses[t] <- response;

// pass on to device
device.send("GetValue", t);


device.on(“GetValueResponse”, function§ {
local t = p.t;
local data = http.jsonencode({ data = p.d });

// send the response
SendResponse(t, 200, data);


device code:
`function GetData(){
return “SomeData”;

agent.on(“GetValue”, function(timestamp) {
local data = GetData();
agent.send(“GetValueResponse”, { t = timestamp, d = data});

Thanks @beardedinventor, I like this as it’s non blocking on the agent request input side.

found a bug. fix:

// sends a response based on a timestamp function SendResponse(t, code, body) { // if the response is in our queue (it hasn't timed out) if (t in HttpResponses) { // send it HttpResponses[t].send(code, body); <b>delete HttpResponses[t];</b> } else { // if it wasn't in the queue, log a message server.log("Response " + t + " not found in response queue"); } }