How to get 2 imps to talk... ASAP!:)

Hi, I am completely new here and would like to ask a question regarding a project.
I have 2 imps plus shields and on one I have an IR sensor, and the other will (hopefully) trigger a motor.
Got a friend to help me code, but still cannot get the right message to post on the receiving imp.
So far it just posts httppost, where I would ideally like it to post sensor values.

Many thanks in advance!

Here are the agent codes:

Sending imp(sensor):

`_last_sensor_value <- “”;
// Log the URLs we need
server.log("To poll sensor value: " + http.agenturl() + “?sensor”);

function sendSensorValue() {
local url = “https://agent.electricimp.com/xxxxxxxxxxxx”;
local header = {“Content-Type”: “application/x-www-form-urlencoded”};
local string = http.urlencode( {value = _last_sensor_value});
local request = http.post(url, header, string);
local response = request.sendsync();
server.log(response.statuscode);
return response;
}

function requestHandler(request, response) {
try {
local value = “ping”
// check if the user sent sensor as a query parameter
if (“sensor” in request.query) {
value = _last_sensor_value;
sendSensorValue();
}

// send a response back saying everything was OK.
response.send(200, value);

} catch (ex) {
response.send(500, "Internal Server Error: " + ex);
}
}

// register the HTTP handler
http.onrequest(requestHandler);

device.on(“sensorvalue”, function(value) {
server.log("received sensor value: " + value);
_last_sensor_value = value;
});`

receiving imp:

`// Log the URLs we need
server.log("Turn LED On: " + http.agenturl() + “?value=3000”);
server.log("Turn LED Off: " + http.agenturl() + “?value=0”);

function requestHandler(request, response) {
try {
server.log(“httppost”);

local sensorvalue = -1;
// check if the user sent led as a query parameter
if ("value" in request.query) {
    local ledState = 0;
  sensorvalue = request.query.value.tointeger();
  server.log("received sensor value: " + sensorvalue);
  if(sensorvalue >= 20000) {
      ledState = 1;
  } else {
      ledState = 0;
  }
  // send "led" message to device, and send ledState as the data
  device.send("led", ledState); 
}
// send a response back saying everything was OK.
response.send(200, sensorvalue);

} catch (ex) {
response.send(500, "Internal Server Error: " + ex);
}
}

// register the HTTP handler
http.onrequest(requestHandler);`

Two quick things… first, click the “C” above to wrap your code in tags, and remove your agent URL… add X’s or something. No need to tempt anyone to mess with your imps…

Your code looks good - I assume the first imp has some device code that periodically sends up the sensor values using:

agent.send("sensorvalue", someSensorValue);

I’m not sure I understand what you mean by this:

So far it just posts httppost, where I would ideally like it to post sensor values.

Could you clarify what your problem is / what the request the second imp is receiving?

ah thanks for quick reply!
and for the advice jwher, will wrap and secure in future.

beardedinventor: that’s right, i have a device code that entails agent.send, and this part works fine, and it logs the sensor values correctly.
the part i am confused about is, i want the sensor values from the first imp to trigger a motor (or for this example i have just used an led), so in the agent code of the first imp i have:

function sendSensorValue() { local url = "https://agent.electricimp.com/xxxxxxxxxxxx"; local header = {"Content-Type": "application/x-www-form-urlencoded"}; local string = http.urlencode( {value = _last_sensor_value}); local request = http.post(url, header, string); local response = request.sendsync(); server.log(response.statuscode); return response;

but in the log window of the second imp where i expected to find the sensor values logged, i just get:

2014-01-09 15:54:22 UTC+1: [Agent] httppost

any recommendations? maybe i am approaching this the wrong way…

oh ps, if i type a value like ?value=3000 or something after the url of the second imp agent, it will turn on/off the led fine.

Oh, I see - the “httppost” logging that’s happening is in the second imp.

Here’s what’s going on:

Your first imp is url-encoding your data (good), and then passing it as the body of the request.

Your second imp is expecting that data to be in the query parameters (which it is not, since it’s in the body). You have two options here:

Option 1: Change the imp that’s sending the data
`function sendSensorValue() {

/***** Add ?value={_last_sensor_value} to end of url *****/
local url = "https://agent.electricimp.com/xxxxxxxxxxxx?value=" + _last_sensor_value;

local request = http.post(url, header, "");
local response = request.sendsync();
server.log(response.statuscode);
return response;

}`

Option 2: Change the imp that receives the data:
`function requestHandler(request, response) {
try {
server.log(“httppost”);

local sensorvalue = -1;

/***** Decode the body, and use the decoded data instead of request.query *****/
local data = http.urldecode(request.body)

// check if the user sent led as a query parameter
if ("value" in data) {
    local ledState = 0;
  sensorvalue = data.value.tointeger();
  server.log("received sensor value: " + sensorvalue);
  if(sensorvalue >= 20000) {
      ledState = 1;
  } else {
      ledState = 0;
  }
  // send "led" message to device, and send ledState as the data
  device.send("led", ledState); 
}
// send a response back saying everything was OK.
response.send(200, sensorvalue);

} catch (ex) {
response.send(500, "Internal Server Error: " + ex);
}
}`

I haven’t tested either of those bits of code, but they both should work (or point you in the right direction at the very least).

Thanks! Will give those a go later!

Hi there, thanks for your help before, the above worked out fine.

I am now trying to poll the values of the first imp on my second imp so i don’t need to enter the url each time to get a value.

I am having problems identifying the value from the table.
Could you help?

At the moment agent polls: 200 and device polls: poll

thanks in advance!

`// Log the URLs we need
server.log("Turn LED On: " + http.agenturl() + “?value=3000”);
server.log("Turn LED Off: " + http.agenturl() + “?value=0”);

_last_sensor_value <- “”;

// when we get a “poll” message from the device
device.on(“poll”, function(state) {
local data = http.urldecode(requestSensorValue().body);
//server.log(data);
//here we have read the data form the page
//how do we get the sensor value out of the table?
if (“sensor” in data) {
server.log("!!!");
//color.red = req.query[“red”].tointeger();
}

});

// request a value from the sensor imp
function requestSensorValue() {
//server.log(“now requesting”);
local url = “https://agent.electricimp.com/xxxxxxxxxxx”;
local header = {“Content-Type”: “application/x-www-form-urlencoded”};
local string = http.urlencode( {value = _last_sensor_value});
local request = http.post(url, header, “”);
local response = request.sendsync();
server.log(response);
return response;
}

function requestHandler(request, response) {
try {
server.log(“httppost”);

local sensorvalue = -1;

/***** Decode the body, and use the decoded data instead of request.query *****/
local data = http.urldecode(request.body)

// check if the user sent led as a query parameter
if ("value" in data) {
    local ledState = 0;
  sensorvalue = data.value.tointeger();
  server.log("received sensor value: " + sensorvalue);
  if(sensorvalue >= 20000) {
      ledState = 1;
  } else {
      ledState = 0;
  }
  // send "led" message to device, and send ledState as the data
  device.send("led", ledState); 
}
// send a response back saying everything was OK.
response.send(200, sensorvalue);

} catch (ex) {
response.send(500, "Internal Server Error: " + ex);
}
}

// register the HTTP handler
http.onrequest(requestHandler); `