Agent + consistency

So I have a “working” sketch that communicates packets between imps in my planner to turn leds a specific color (video here: http://youtu.be/31F0LaFvftw).

I am a bit confused though; I know the new IDE for imp is beta, but I doubt this is the reason for my issue. Though my sketch works initially, when I change a certain line of code in any of the sketches, then revert back to the original sketch, communication between imps seems to stop. their are no error messages. and the video illustrates working code.Sketches are too long to post here.

further detail, reach me at danbertner(@)gmail(dot)com

I’m afraid we need a lot more detail to debug that. What line are you changing? Is the agent restarting when you re-run the code? (add a log message so you can see), etc.

here is my agent code; its the same across imps minus the URL’s of each imp.

`/////AGENT/////////

server.log("Copy this URL to your other agent (otherAgentURL): " + http.agenturl());
server.log("Turn LED on by calling: " + http.agenturl() + “?led=1”);
server.log("Turn LED off by calling: " + http.agenturl() + “?led=0”);

// setup an HTTP handler to process incoming http requests
http.onrequest(function(request, response) {
try {

    // check an 'led' query parameter was passed into the request
    if (request.query != null && "led" in request.query) {
        // if it was, pass it on the device
        device.send("setLed", request.query.led.tointeger());
    }
    // send an http 200 to say everything was OK
    response.send(200, "OK");
} catch (ex) {
    // if there was an error, send back an http 500 and the error
    response.send(500, "Internal Server Error: " + ex);
}

});

//////////////////////////////////////////////////////////////////////////

const otherAgentUrl1 = “https://agent.electricimp.com/YADA1”; // concealed
const otherAgentUrl2 = “https://agent.electricimp.com/YADA2”; //concealed

device.on(“touchstone3”, function(stoneThree) {
local url1 = otherAgentUrl1 + “?led=” + stoneThree;
local url2 = otherAgentUrl2 + “?led=” + stoneThree;
server.log("sending request to " + url1);
server.log("sending request to " + url2);
// create the request
local request1 = http.get(url1);
local request2 = http.get(url2);
// send the request
request1.sendsync();
request2.sendsync();

});`

and the code:

`//////MAIN//////////

led9 <- hardware.pin9;
led8 <- hardware.pin8;
led7 <- hardware.pin7;
led9.configure(DIGITAL_OUT);
led8.configure(DIGITAL_OUT);
led7.configure(DIGITAL_OUT);

imp.configure(“touchstone1”, [], []);

// assign hardware.pin9 to a variable to make things more clear
button <- hardware.pin2;

led9.write(0);
led8.write(1);
// function to be called when button state changes
function buttonStateChanged() {
// read the current state
local state = button.read();
// send it to the agent
agent.send(“touchstone1”, state);

if(state==1){
    led9.write(1);
    led8.write(0);
}
if(state==0){
    led9.write(0);
    led8.write(1);
    
}

}

function setLed(value) {

if(value==2){
    led9.write(0);
    led8.write(0);
    led7.write(1);
}
if(value==0){
    led9.write(0);
    led8.write(1);
    led7.write(0);
    
}

if(value==3){
    
    led9.write(1);
    led8.write(0);
    led7.write(1);
}

}

// setup a handler for setLed message from the agent
agent.on(“setLed”, setLed);

// configure the button
button.configure(DIGITAL_IN_PULLDOWN, buttonStateChanged);`

What line of code are you changing that breaks everything?

As a side note… if you change
request1.sendsync(); request2.sendsync();
to:
request1.sendasync(function(resp) { }); request2.sendasync(function(resp) { });

the leds on the other imps should turn on closer to each other. Basically, instead of waiting for the first request to be sent before sending the second one, we send them asynchronously (at the same time).

Thanks for the suggestion. I am a bit unclear though on what function you are suggesting to go into the sendasync bracket?

and I guess the fact of me changing a line of code and reverting back is irrelevant because the same issue happens when I trip a switch on two imps simultaneously. So initially you see whats happening in the video, but when I delete any line of code and then revert back to previous code, trip two sensors, same result happens with the imps losing a connection between the two.

I would think that all of the imps would need this general structure of agent code to both send/receive to each other.

.sendasync() expects 1 parameter - a callback function. That function gets called when the HTTP request is completed (and the callback function requires a parameter, which is the response).

Since you aren’t doing anything with the result of your http requests, we pass an empty function into the sendasync() call…

You want to replace the two lines of sendsync() code with the two lines I gave (with no changes to them). Your finished agent code would look like this:

`/////AGENT/////////

server.log("Copy this URL to your other agent (otherAgentURL): " + http.agenturl());
server.log("Turn LED on by calling: " + http.agenturl() + “?led=1”);
server.log("Turn LED off by calling: " + http.agenturl() + “?led=0”);

// setup an HTTP handler to process incoming http requests
http.onrequest(function(request, response) {
try {

    // check an 'led' query parameter was passed into the request
    if (request.query != null && "led" in request.query) {
        // if it was, pass it on the device
        device.send("setLed", request.query.led.tointeger());
    }
    // send an http 200 to say everything was OK
    response.send(200, "OK");
} catch (ex) {
    // if there was an error, send back an http 500 and the error
    response.send(500, "Internal Server Error: " + ex);
}

});

//////////////////////////////////////////////////////////////////////////

const otherAgentUrl1 = “https://agent.electricimp.com/YADA1”; // concealed
const otherAgentUrl2 = “https://agent.electricimp.com/YADA2”; //concealed

device.on(“touchstone3”, function(stoneThree) {
local url1 = otherAgentUrl1 + “?led=” + stoneThree;
local url2 = otherAgentUrl2 + “?led=” + stoneThree;
server.log("sending request to " + url1);
server.log("sending request to " + url2);
// create the request
local request1 = http.get(url1);
local request2 = http.get(url2);
// send the request
request1.sendasync(function® { });
request2.sendasync(function® { });

});`

As for the other questions:

If it’s happening when you’re tripping two imps at the same time, that sounds like an error in your code somewhere…

In terms of communicating between imps, that is somewhere we’re working on and discussing internally - we’re trying to find really good ways of doing it before we push something out.

This had fixed my error! Thanks a bunch.

So are you saying communication between imps simultaneously is still beta? i.e. an example is I have 3 imps and if two imps are triggering messages to the 3rd, the 3rd cannot read them simultaneously?

No, we are working on ways to do inter-imp communication for a single user without hard-coding agent URLs all over the place.

Thanks for the clarification, Hugo.