Post imp data to a web page

Hello,
Was is the easiest way to post imp device data to a web page?
Can I send data to the agent URL?
I want to visualize real time temperature data in a web page in a very simple way.

Thanks in advance

You have a website that uses PHP. Your agent posts to a PHP script on your website that writes the temp and timestamp values into a file or database. Another PHP script reads that data and displays it on a web page … perhaps in a nice line chart using Google Charts? Or using HTML5?

The reverse method is to have a PHP script post to the agent and request info from the imp. JQuery / PHP makes the web page dynamic … real time.

Everything can be done using JSON, as both the imp and PHP handle JSON very easily.

You can do ajax requests from javascript in your web page. This request can be made directly to the agent.

The action must be initiated by the code in the web browser.

somethine like this…

where httpimp is the url of your agent

`

function mainPoll(){

	$.ajax({
		type: 'get'
		,url: httpimp
		,crossDomain: true
		,dataType: 'json'
		})

		.done (function(agentMsg, textstatus, xhr) {
			
			console.log(agentMsg);
			updateall(agentMsg);
				
		})
		
		.fail (function(err) {
			console.log('err'+ err.status)
			
			if (err.status == 408){
				console.log('err 408');
				
			}
		})
}

`

sorry I don’t have a more concise example. There may be others out there that can suggest something close to what you are doing.

PHP makes sense for people already really familiar with it. There might be other reasons for using PHP that I am not aware of (beginner here). I only use PHP when I desire to create a data log on my hostgator account.

There are a lot of ways you can post data. One the easiest and most interesting is to post your data to Xively, which has a free component. If you give us more information about exactly what you want to do, we can point you to the best examples.

The great thing about the imp is that you can do all of this without having to run any additional web servers if you don’t want to.

thanks guys. I think Xively will be the easiest way for me.

My project is a pool pump and light controller and a water temp data logger. It let me control the pump and light using my android phone and auto restart or reset it after few hours. When the water is cool enough, the pump activity is managed in orther to be more eco friendly.
My project was working last year, and I’m rewriting the agent side as the planner board is not supported anymore (I get over it … I was frustrated first that no backward compatibility have been proposed ).

So regarding the data, I was using Xively last year, but I was expecting someting quicker in order to get real time of my water temp with my smart phone. When I say quicker I mean : open the browser, go to the xively link if it closed, log in, or refresh … to long for me and not convenient…

So maybe the deal could be to add the temperature display on my android app directly.

What would be the easiest way in order to make my android phone request the temperature from the agent?

Thank you everybody for your help!

You can just hit the agent URL directly with your android phone; either with an app or with the android browser. I have some lights around my house (based on the becky reference design) that serve up a simple html page with buttons on it - I just bookmark this.

Hugo, that’s what I need. What do you mean by I just bookmark this?

In the IDE you’ll see the agent URL: https://agent.electricimp.com/XXXXX

Write a little bit of code to display the info/controls you want in your agent, then visit this page on your phone and bookmark it.

You could start with: https://github.com/electricimp/examples/tree/master/Cowbell …which basically just sends a message to the device any time the URL is accessed, and build up from there.

Hugo, I understand how to control device IO’s via a HTTP request. Thanks for the agent example in the documentation.

What I don’t understand is how to display data in the agent an access it through the agent URL?

I don’t know if you have a project example to suggest?

Here is my agent code. Basically, I’m transfering a IO state to the imp. And the imp is sending the temperature data to the agent.

// Log the URLs we need
server.log("Turn Pump Off: " + http.agenturl() + “?stateIO=1”);
server.log("Turn Pump On: " + http.agenturl() + “?stateIO=2”);
server.log("Turn Light Off: " + http.agenturl() + “?stateIO=3”);
server.log("Turn Light On: " + http.agenturl() + “?stateIO=4”);

function requestHandler(request, response) {
try {
// check if the user sent state IO as a query parameter
if (“stateIO” in request.query) {

  if (request.query.stateIO == "1" || request.query.stateIO == "2" || request.query.stateIO == "3" || request.query.stateIO == "4") {
    // convert the StateIO query parameter to an integer
    local IOState = request.query.stateIO.tointeger();

    // send "led" message to device, and send IOState as the data
    device.send("stateIO", IOState); 
  }
}
// send a response back saying everything was OK.
response.send(200, "ok");

} catch (ex) {
response.send(500, "Internal Server Error: " + ex);
}
}

// register the HTTP handler
http.onrequest(requestHandler);

function return_from_imp(tempF)
{
server.log(“Temp Eau=”+tempF+“F”);
}

// When we get a ‘temp’ message from the agent, call return_from_imp()

device.on(“temp”, return_from_imp);

thanks

I suspect you’re looking for an example where the flow is like this:

  1. http request arrives for a reading from the imp
  2. agent sends message to imp asking for latest data
  3. device returns latest data to agent
  4. agent answers http request with the fresh data

…right? I’ve seen this several times (beardedinventor in particular) but can’t find a link right now. This flow is possible because you do not need to complete the request in the onrequest handler: you can push the object and then complete the request when the data has been received from the imp.

To format a page etc maybe this is helpful: http://forums.electricimp.com/discussion/comment/13829#Comment_13829

Here’s a good post explaining the flow:

http://forums.electricimp.com/discussion/2383/http-response/p1

I should turn that into an article for the Community Blog so it’s a bit easier to find…

Hugo / beardeinventor, thank you for your help. You well understand my need.

I’ll read carefully the links you sent and included that in my code.