Simultaneous getting data from the imp while controlling it


I am working on a project that requires me to display voltage values from the imp to a webpage and control an LED connected to the imp from the same webpage. Since, I am new to the imp and have limited knowledge of JavaScript/HTML, I’m finding a it a little difficult to do this. According to my understanding, we can only ‘get’ or ‘post’ in a single HTTP request. Given that, I’m having a hard time trying to figure out how to accommodate the two simultaneous but opposite functions. Any help will be much appreciated ! Thanks

You will need to use Javascript (Ajax) to handle both the polling, which will update your voltage values automatically at set polling intervals (say for example every 5 seconds) and then the click events (onclick method linked to an html button), which will control your LED for you. Both long polling and click methods issue “get” or “post” requests to Imp Agent. It is up to you to structure how the Agent HTTP request is handled and how the responses are returned. Usually the Imp Agent response will be in the form of a string or a json encoded object. Your javescript function then has to interpret this information and handle the web-browser screen updates.

I guess my questions are pretty much the same as gerriko.
Maybe we need to know more about what the project is for.

What are your specifications on “real time”?

Example, would it be OK if your website (AJAX) requested the voltage every 5 seconds, or 10 seconds? What is your refresh rate requirement?

At the same time it makes a request for voltage, can it also command the LED on or off?

Again, what is the response, or time requirements for the LED?

You can have a website “poll” the imp for a value, or have the imp POST the value to a website database. Not sure which direction you are going to go.

Thank you both for your answers. They were quite helpful. To answer some questions,
the priority of the project the project is LED control, so the voltage polling can be longer. However, I would definitely like some advice on which approach will be better: to have the imp post values or have the website poll values. Once again, thank you for your help.

The fastest you can control the LED from a web page would be about 1-2 seconds. That means you click the button on your page and the LED might not light until 1 second passes.

There is propagation time between your shared webhost, the cloud, the agent, and the device.

It would be faster if the agent was your webhost (webpage on the agent not your website). But then, you have to use your agent URL, which might be a deal-breaker. You usually don’t want people to know your agent URL.

So your LED control would be a button on your page that then uses PHP to POST an LED ON or LED OFF command. I use PHP CURL because it hides your agent URL from people viewing your webpage HTML source.

The voltage part could be a javascript (JQuery) loop on your web page that periodically asks the imp for the current value. Whatever value the imp returns would be written to a

on your page (for people to see). The only reason you would want the imp to post values to the website would be for data logging in an online database file.

If you want to test the LED part first …

  1. Create a form on your website. For the test, you can use your Agent URL.

something like this (but use your real agent key, not my fake one) …

`<form action="" method="post">
<input type="hidden" name="led" value="on">
<input type="submit" name="submit" value="TURN ON LED">
<form action="" method="post">
<input type="hidden" name="led" value="off">
<input type="submit" name="submit" value="TURN OFF LED">

(as always, this forum editor is crap … ignore the semi-colons in the tags)

Your simple form test using POST will not look very elegant on your web page, but it will test your agent and device code to see of they successfully receive your POSTed value.

  1. Program your agent to send the value to your device and do the LED thing.

  2. If you can get that to work, turning on or turning off the LED, let me know.
    The PHP portion to hide the URL can be done later.

The final steps would be to use JQuery, AJAX, etc. for the User Interface.

If you haven’t already, take a look at this blog post…

How to Build Interactive Web Pages with AJAX, jQuery, and Rocky

The blog post goes through a basic example of interacting with an imp (reading values, and setting values) with HTML, jQuery, and Rocky (an API framework we designed that makes HTTP Handlers a bit easier).