Help with "agents"

I am a novice with squirrel and IMP. I have learnt so much since playing around with IMP (thks, Hugo), however, I cant seems to get my head around the concept of “agents”. Can someone plse give a simple explanation (preferably with examples) of what the the basic concept is. Are agents some sort of of fancy functions or modules? how does it work?

I second this. I gather from examples that I want to be using agents, but I still don’t know exactly what they are or how to use them. (do you add them to your existing code, are they separate objects in the planner, etc.)


You can see the agents as user defined “nodes” If you spend some time in observing the electric imp API Reference in the documentation, you will find a lot of examples

Agents are not separate objects in the planner, and agents are actually meant to be a step away from the planner in a lot of ways.

In the new (beta) IDE, your code is separated into two pieces:

  1. Device code (which runs on the imp itself)
  2. Agent code (which runs on the Electric Imo Cloud / Servers)

The device code and agent code are different files that run in different environments, but can communicate with each other very easily. Here is a piece of example code that shows how agents and devices talk to each other. In this example, the agent and the device send the messages “ping” and “pong” back and forth forever:

Agent Code
`// when we get a “ping” message from the device
device.on(“ping”, function(data) {

device.send("pong", null);

}); `

Device Code
`imp.configure(“Ping Pong”, [], []);

// when we get a “pong” message from the agent
agent.on(“pong”, function(data) {

// wakeup in 5 seconds and ping again
imp.wakeup(5.0, ping);


function ping() {
agent.send(“ping”, null);

// start the ping-pong

Each imp has one (and only one) agent associated with it. Along with being able to receive messages from it’s imp, each agent also has a unique URL you can use to communicate with it from external web services. In the next example, we create an HTTP handler that processes incoming HTTP requests and turns an LED on or off depending on the information that was sent in the request:

Agent Code
`// agent code
// Log the URLs to turn LED on/off when agent starts
server.log("Turn the LED on by browsing to " + http.agenturl() + “?led=1”);
server.log("Turn the LED off by browsing to " + http.agenturl() + “?led=0”);

// create an http handler for incoming requests
function requestHandler(request, response) {
// Check if the variable led was passed into the query
if (“led” in request.query) {
// if it was, send the value of it to the device
device.send(“led”, request.query[“led”]);
// send a response back to whoever made the request
response.send(200, “OK”);

// your agent code should only ever have ONE http.onrequest call.

device Code
`led <- hardware.pin1;

agent.on(“led”, function (value) {
if (value == “0”) led.write(0); // if 0 was passed in, turn led off
else led.write(1); // otherwise, turn it on

imp.configure(“Browser Controlled LED”, [], []);`

Agents also make it way easier to send HTTP requests. The URL doesn’t need to be hard coded in an HTTP Request node, you can add headers, use different verbs, etc. Here is a simple example where we send a fairly basic http PUT request to a service called
Agent Code
`// agent code
// Register a handler for messages send from device
device.on(“sendData”, function(data) {
local binId = “123abcd1”; // your request bin

local url = "" + binId;
local headers = {};

// create the request object
local request = http.put(url, headers, http.jsonencode(data));

// send the request
local response = request.sendsync();

// log the response, we expect '200: OK'
server.log(response.statuscode + ": " + response.body);


Device Code
`// device code
imp.configure(“HTTP PUT”, [], []);

function poll() {
local a1 =;
local v = hardware.voltage();

local data = {
	pin1 = a1,
	voltage = v,
	normalizedPin1 = a1 / v

// send message to agent
agent.send("sendData", data);
// wakeup in 15 seconds, and do it again
imp.wakeup(15, poll);



If you’ve read this far, and would like to start experimenting with agents, send me a message, and I’ll get you added to the beta.

please add me to the beta to use agents. I really want it…

Please add me as well. I messaged you too. Thanks

@ramstein74, @enque: You both should have received emails (at the email account your forum user is registered to) with more information :slight_smile:

got it .Thank you.
I have to say that electric imp is one of the most interesting cloud projects i know.

Bye Bye arduino…

Up and running on the beta now, THANKS. Is there a “+” button in the IDE interface to create a new code module, or agent? I’m sure I am overlooking it somewhere, but as is I can only edit my existing modules (which still works)

If you click the ‘Device Settings’ button, you should see a drop down to select what model (which is ‘code modules’ became in the new IDE) you want to edit.

There should be an option to “Create a new Model” in the drop down :slight_smile:

Thks, understand bit more now, Plse add me to beta so i can start playing with it. Thks, Ken

I have to say, there were at least two things that were a bit confusing about the IDE, the first was how to create a new model, but I eventually figured that out, and the second was that you apparently have to Check or Run a build to save it. This resulted in me losing code a few times before I got used to it. Also, is there a way to develop code that isn’t tied to an impee?

Thanks for the feedback :slight_smile:

Right now you cannot edit code that is tied to to a device.

Along with the IDE, we’re working on improving documentation and creating a good “getting started” guide for agents + the new IDE - It should help clarify some of the new functionality.

Remember - this is still in a (semi)closed beta, and one of the big reasons it’s still in the closed beta is because we’re not happy with where the IDE is yet :slight_smile:

Your closed beta works a lot better than many products in production. I thoroughly enjoy working on my imp projects. (Probably too much, as I really should be mowing the grass.)

Oh, and BTW, when is the next Hangout? I’d suggest advertising it heavily, as I only stumbled on the last one by accident.

Good timing - I just tweeted about that. It’s going to be next Thursday (July 25th).

I’m going to be posting about it shortly.