Real time receiving data from imp

hello there,
I want to send sensor readings from my imp to my program in real time, without necessarily needing to store the values somewhere.

is there a way to query the agent from a Java program without needing to upload my data to an API such as xively?
i can’t find any examples of this sort

thank you in advance.

yes, you can do this.

There is example code on this tumblr page

http://themzlab.tumblr.com/wattmeter

There are links to the agent / device/ and web app code very near the bottom of the page.

Sorry this is not a concise example. There may be other examples out there that are better. Maybe another member can chime in with an example.

The basic approach I use is to use jquery and ajax. Maybe my code will lead you to google around and find better examples.

The dropbox links used to work more seamlessly but lately it seems there is a dialog box that pops up where you click ‘just download’.

I will need to figure out a better way to link in big code.

When you say ‘program’, do you mean a web page on your website?

You can use JQuery to request the imp value over and over again, perhaps each second.

I prefer to have the imp send the data to my website continously. A PHP script takes the incoming posted data and writes it to a file. You only need to record maybe the last 60 values, one each second … and when a value is added, one drops off. Your web page then dynamically displays the data in some form of graph. Again, using javascripting.

Lastly, the agent itself can be the web page … you don’t need a website of your own at all.

So you have some options.

thanks a lot for the response!

The last thing - using the agent as a website- sounds the best to my case, because i want to avoid going through websites and cloud servers.
By program i meant Java program, than asks in some way the agent for the last values.

Is there some sample code of using the agent as a website?

You’re technically always using a cloud. The imp uses its own cloud all the time. I think you mean that you don’t wish to add another website in the mix?

There are examples somewhere on the forum or documentation part that describes putting the website in the agent (agent web server). This forum software they use is really bad … no way to search for it because the search function never finds anything, and their documentation is confusing as well.

I did find this:
http://forums.electricimp.com/discussion/1006/simple-example-of-web-server-using-agent-code/p1

That’s the only one I could find, but I know there are more.

They are attempting to make better examples, and user-submitted projects. I have no idea where any of that is. Maybe someone else can point you in the right direction. In my opinion, this forum is pretty much the weak link in the whole thing.

You can sift through these one-by-one and see if anyone uses the agent as the web server:
http://forums.electricimp.com/categories/projects

Here’s an imp project that uses the agent as the web server:

The start point for all our documentation is the Dev Center - generally, check out the Sample Code section for useful listings. Our new and growing Hints and Tips section also contains code snippets for specific situations - but not (yet) this one; I’ll be remedying that in due course.

It’s also worth bookmarking our GitHub repo, which contains a lot of useful code for hardware and web services. There are explanatory lists of the repo’s key contents here and here.

thanks a lot for the responses.
here is my project by the way, in which i intend to apply the IMP
http://www.studioany.com/#!projectanywhere/c1g1s

Hello again,
I’ve tried a simple program to see if the IMP works for real-time applications. But I get a latency of around 0.8 second…

In my case I need to get sensor readings 4-10 times per second - and though it works with a simple arduino and XBee configuration, I want to port it to WiFi.

my code goes like this:

http.onrequest(function(request, response) {
local code = 200;
local msg = “IMP[2]Response:” +"/>"+ getTime();
response.send(code, msg);
server.log(“Responded”);
}

(so the actual http request doesn’t matter, it returns the same every time)

On the other side I am sending requests for a C# program, just on the Agent URL.

IEnumerator doRequest ()
{
WWW www = new WWW (url);
yield return www;
Debug.Log (">" + (Time.time - enterTime));
}

The fastest I can get it to respond is around 0.7 of a second. and it goes up to 2.5.

Am I doing something wrong?

have you looked at other methods other than http such as mqtt or amqp/ as they could in theory deliver those sort of readings per second…

Really, HTTP is quite heavyweight (HTTPS even more so), so batching your results is a good idea.

You can stream data inbound to the agent (single request, multiple replies), but the converse is not yet supported - though it will be later this year. That removes the request setup time, so streaming data will generally have a millisecond type overhead.

@hugo that will be a great enhancement if we can keep a connection live and stream output data. I know this was an issue when trying to stream data to plot.ly for example.

@hugo, this was something I didn’t realise was supported already. Does that mean that the handler I pass to httprequest.sendasync() could be invoked multiple times if use use “Connection” : “Keep-Alive”? I just discard the handler after the first response.

When you say that the converse will be available later, does that mean that I will be able to reuse the httpresponse object multiple times?

As the docs say, you register a completion handler, and an intermediate handler. The completion handler is still called when the request completes, but the intermediate one is called as new data arrives. There’s also a timeout passed in. Seehttp://www.electricimp.com/docs/api/httprequest/sendasync/

For examples of how this is used, take a look at https://github.com/electricimp/twitter which opens a real-time stream of tweets.

We’ve not nailed down how the outbound streaming will work as yet, but it’s likely there will be a way to send a non-final data packet separate from the current final response.send()

thanks a lot for the reply. Well since I don’t have much knowledge about com protocols
is there any imp example that I could try with mqtt or amp? (or other way which could be faster than http requests?)

or is there an example to send over TCP/IP

From the agent, you can only currently use HTTP (to/from the agent). MQTT and AMQP would require an HTTP-to-MQTT or -to-AMQP proxy, so you’re basically back where you started.

At the moment, your best option is to batch the samples, and respond to the HTTP request with the ones recorded since the last time.

Well, what I am doing is this:
http://www.studioany.com/#!projectanywhere/c1g1s

so I can’t either batch or log for later. I need “real-time” sending.
at the moment I have these interactive gloves that are on XBees, and they are sending every 20-50ms. I was thinking that it would be very convenient to do it with the Imp since i won’t need XBee receivers.

My idea, although I am internet-protocol-challanged, was when I ran my app from the phone/laptop to send my IP to the IMP (via HTTP request on the Agent URL) and tell it to start. Then without requests it will just send the latest values as fast as it can.
But I have no clue if i could to TCP/IP with the Imp.

Having followed your post, it really looks like you are trying to find a way to stream data, much like netflix or spotify… or even a webcam. Talking of ip-based cameras, many subscribe to using RTSP. Here is the Internet Standard document that describes the protocol.

It took one google search to find this example using a Raspberry Pi, which demonstrates what could be done.

As mentioned previously (Hugo), it looks like we all have to wait for this (hopefully) future Imp upgrade where we will be able to stream outbound information.

But anyhow the most consistent and reliable way to stream data from any device is to hook it up using Ethernet rather than wifi.

thanks a lot for you proposal.
I am not a real programmer -I am an architect- so I require a lot of reading to follow all these things :S
(and I have no clue on linux or pi stuff)

The catch to my issue is that since the data I want to send are from point to point, they could be transferred over a LAN rather than messing with clouds (although they are convenient). The actual distance I want to send the data is from .2-1meter, and the data is just a 50 character string…

I also tried the Roving Networks RN XV module, which proved to work, but the problem there is that you have to manually program WiFi settings, and static IPs every time you change networks, so its not that consistent.

Earlier this week I ordered a couple of Bluetooth Low Energy modules, and eventually, after 3 days of a lot of work (BLE is just complicated compared to normal bluetooth) I finally made my App to work with that.
It has its limitations, but if you get your head around it, its consistent.

thank you all for your ideas!
constantinos
studioany.com

It’s all part of the development process - reading! First you diverge as you explore your options and then you eventually converge on a solution as you discover new insight while gaining more knowledge and experience.

Yes Bluetooth probably the way to go especially as you want it to work with your app. There are also WiFi modules that can work as local access points (thereby avoiding the need to link into your LAN), such as cc3200, cc3100 and gainspan.