How to query the status of a pin?

I’m new to the imp and I’m trying to do something that I thought you be fairly simple.

I’m trying to have an agent that retrieves the value of the state of a pin and respond with the value in the body of the response. But I cannot accomplish this, it seems the only way to do this is to send a message to the device and have a listener in the agent but that basically prevents me from replying with the value in the same transaction.

I’m hoping this is an easy one.

Rgds

This example is will show you how to write to a pin using the agent.
https://electricimp.com/docs/gettingstarted/3-agents/

In your case, you want to read a pin, so you could do something like this at the device:
local pin9State = hardware.pin9.read() (Assuming you were using Pin 9), and then send it to the agent, where you would respond back to your request.

There’s two common ways of doing this.

The first is the have the device update the agent whenever the state of the pin changes:

Device Code
function pinChanged() { agent.send("pin1", hardware.pin1.read()); } hardware.pin1.configure(DIGITAL_IN, pinChanged);

Agent Code
`_pin1 <- null;
device.on(“pin1”, function(pin1State) {
_pin1 = pin1State;
}

http.onrequest(function(req, resp) {
resp.send(200, http.jsonencode({ pin1 = _pin1 }));
});`

The other is using the method described in this post