Device.send & agent.on confusion for a newbie

I am new to this type of programming but what I want to do is to return a value from a function on a imp device from within a function inside the Agent.

To keep the communication between the device and the agent at a minimum I want to pull the imp on demand from the agent -vs- listening for events. Is this possible?

For example
// the following is a mix of Perl and C not squirrel ( I don’t know how to do it in squirrel yet )

//agent code
function GETIMPDATA(){
sensorvalue=remotefunction(1);
server.log(“the sensor value for sensor 1 =”+sensorvalue);

sensorvalue=remotefunction(2);
server.log(“the sensor value for sensor 2 =”+sensorvalue);

};

//device code

function remotefunction (sensor){
returnvalue=thesensor.sensor;
return returnvalue;
}

Thanks for your help

Listening for messages rather than polling is how things are done on the imp, but you need to register with both agent and device the relevant function they need to call when a message comes in.

At the start of the agent code:
device.on("reading.from.imp", GETIMPDATA)

At the start of the device:
agent.on("request.from.agent", remotefunction)

These calls set the message names (in quotes) and the functions the imp OS will call if those messages arrive.

Your functions need tweaking a little to accommodate this:

function remotefunction(sensornumber) { agent.send("reading.from.imp", thesensor.sensornumber) }

and

function GETIMPDATA(returnvalue) { server.log("the sensor value for sensor ="+ returnvalue); }

That handles the communication, but doesn’t initiate it, so at the end of agent code, you need to send a ‘get reading’ message to the imp:

device.send("request.from.agent", 1) device.send("request.from.agent", 2)

So, agent sends the “request.from.agent” message twice, once for each sensor. As it receives the messages, the device gets the appropriate reading, and sends it back to the agent as a “reading.from.imp” message.

The agent receives two “reading.from.imp” messages; each time it does, it calls GETIMPDATA() and passes the data it has received (the reading from thesensor.sensornumber) into GETIMPDATA()'s single parameter, returnvalue, which it displays in the log.

You should read The Interactive Imp and Event-Driven Programming which go through the agent-device communication, and message events in more detail.

Smittytone Thank you for explaining it with more detail. The example with ping & pong was blowing my mind.

My goal is to keep the bandwidth between the imp and the agent to a minimum and only transfer the result of the sensor when requested by the Agent.

In my example I had only 2 sensors but with i2c I could have several sensors the imp has access to and subscribing to each sensor may not be feasible.

Thanks for helping the imp challenged getting started.

Todd

Another option is to collated the readings from your x sensors into nested arrays or tables, and simply send that table to the agent as infrequently as your application can stand.

I can see why the imp team opted for a asynchronous communication mode -vs- a synchronous mode. Waiting for hundreds of IMP’s to communicate back in a synchronous style would definitely cause issues in their infrastructure.

But it sure makes things difficult on my end ;-(

Thanks for your Help

What are you trying to achieve that the asynchronous approach seems to be making harder? There should be a way of adapting it.

Sync would actually be easier in many ways, but it’s far less powerful. With async you can have multiple outstanding requests (from any or all of web, device to agent or agent to device) - some applications require this flexibility.

I am finding the Squirrel and API code syntax is tripping me up when I’m coding. I like to learn by reviewing examples. Can anyone point me to any good documented examples of the code syntax for using “device.send & agent.on” with tables and arrays for multiple sensor inputs for both digital and analog inputs? Also if anyone knows of some really good references for learning to program in Squirrel that would be helpful too.

The Developer Guides in the Dev Center have some outstanding information thanks to @smittytone. These are a must read for new Imp devs.

There are also loads of code examples in the Electric Imp GitHub repositories.

Thanks for the pointers team MakeDeck! Any other references are welcome.