Newbie's question

Hi,
I just bought a new IMP and have some newbie questions:)
I am working on a remote realtime control project to control the imp from my cellphone through the internet. However, I know the imp is not accessible from outside of the router. One solution I can imagine is to let the imp keep asking for new updates from a remote cloud server, say 2 sec? If user sends a control command it will be stored on the cloud and imp extract it then execute it. The downside is that the imp will send the request too often(every 2ec since my application is pretty much realtime).
Any suggestion to lower the number of request? Thank you so much.

Could you give a little more detail on what you want to do? You can send control commands through HTTP to the Imp’s agent very fast. If all you want to do is send control commands to the Imp, there would be no reason to have the Imp ask for information, right? Is this for something like robotic control or some other device?

Sorry I didn’t make things clear. I am working on a power switch type control device. What make me confused is that if I want to remotely turn the switch on and off, I have to send a command from the web server to the imp. However, the switch is behind a router, so the web server can’t reach it. What should I do?

You can’t access the Imp directly, but you can access the imp’s Agent which communicates with the Imp, so if you communicate with the Agent you are communicating with the Imp. Therefore there is no reason to use a web server, just use your phone to access the Agent

Thanks for the reply.
How about the communication between the imp and agent? Should the imp keep sending request to the agent to check the control command very frequently? Or the agent send control command to imp?
My application is realtime. I need to control the power switch in less than 2 sec delay.

You certainly can send many more control requests to the imp than 2/sec, assuming that your connection is a good one. Unless you have to have more than a response from the Imp, I would think you should be fine just sending your commands. Check out this recent thread about real-time communication with the Imp.
http://forums.electricimp.com/discussion/comment/7727#Comment_7727

Communication between the Imp and the Agent is entirely dependent on your code.

Hi jwehr:
That’s very helpful! Thank you!
In the link you mentioned, I noticed that someone said:
" That’s the basic idea. One other wrinkle is that the current request timeout for a request to an agent is only 60 seconds (but that is supposed to eventually be changed to 10 minutes). So you have to resend the long poll request every minute currently. "
Could you please explain more about how this long poll request works? Is it to establish a connection between imp and agent? As long as the connection is established, imp don’t need to send anything to agent, until 60 second passed? Imp just wait for agent to give it command?
Sorry for so many questions.

The connection between the Imp and the agent are handled by the Electric Imp servers, so you don’t need to worry about that. Your control device can talk to the agent, which in turn can talk to the Imp. As far as the request timeout, I believe that is a reference to how long the agent will wait for something to happen before it times out and closes the HTTP connection. For example, lets say you sent an “Open” command to an Imp that was connected to a garage door opener from your iPhone. The agent would receive the request, and send it to the Imp (device). Now lets say that in your code, you wanted the imp to wait until the door was up, and a sensor was flipped so that you would know that the door was completely up. So the door goes up, and the sensor is flipped. The device senses that, and sends it to the agent, and the agent responds to the original request that the door is up. However, if your door got stuck, or your sensor malfunctioned, and the device kept waiting, the request would time out after 60 seconds, and you would not be able to get a proper response from the agent. Hope that makes sense (and is technically accurate)

Thank you so much jwehr.
I have one more question, in your example, before I send the “open” command to the imp, should imp keep the connection open between itself and agent? I mean, keep listening to the agent to send command over? Is it also a HTTP connection?

Yes, it is an HTTP connection, and that is the part that you don’t have to worry about, it is all handled by the Electric Imp servers. The part that you need to worry about is the code on the the agent, and the code on the imp. It is quite easy to send information back and forth. There are some great examples on the Electric Imp GitHub page about how to accomplish what you need.

Hi jwehr:
Currently my project is in engineering prototyping phase. I am trying to evaluate different possibility. The main reason I want to understand more the communication between imp and agent is the power consumption. Not sure if imp is always sending data to agent during the “wait for control” mode or just at wifi receiving mode. The current between receiving and transimitting for wifi is quite large.

Ahh, I see. In that case you’ll have to get an answer from one of the Electric Imp folks.

thank you jwehr!

I think I understand what your concern is, which is that you think the imp may have to poll the server to receive commands (ie, there is no permanent connection between the imp and agent).

This is not the case.

The imp, when powered up, forms a TCP connection to the imp server, and does all it can to keep this connection established. When in powersave mode, the imp can take 7mA or less in this mode (without powersave on, this is about 80mA).

As this connection is present, the agent can send data to the imp at any time at all - the imp does not need to poll.

We are working to reduce this 7mA down to less than half that, but have no timescale for that improvement just yet.

Hi Hugo:
That’s exactly what I am looking for! Thank you!

By the way, Hugo, Could you tell me if electric imp has cloud service we can pay for? How many sensors it can handle?

Yes, for commercial use you need to email sales@electricimp.com. We can support a lot of devices :slight_smile:

How does one go about getting access to the “agent” functionality?

For that, email info@ and ask.

Sweet!