Acess an http page periodically

I am new to to electric imp. I am working on a smart weather clock project. For that I want to access “,USA&mode=json” link every minute and then parse the data I recieve. I was able to parse the data in jason correctly but my only problem is how do I access my link continuously and automatically without clicking on the agent url.
I tried looping http.get in a while inside the http.onrequest(function(req,resp) handler…but it does not seem to update continuously… plz help

Have a look at imp.wakeup

Hi, aksranka. I assume since you talk about ‘clicking on the agent URL’, you are accessing the weather data and parsing the JSON in your agent code.

I would set the agent to grab and process the weather data, used device.send() to pass the results to the imp. The imp does what it needs to then calls imp.wakeup(),as philmy suggests, to wake it up an a second or so.

When the imp wakes, use agent.send() to send a message to the agent to go and get fresh weather data. This sets the process going for another cycle, and so on, and so on.

Just remember, for each .send() in the agent/imp you need a .on() in the imp/agent to register the function you want to be called when the event notification arrives. For a guide on how all this works, check out Event-driven Programming

@aksranka Do you mean that you want the page to refresh every so often? Obviously you’ll want new data there, and you’ll want to use the comments listed above. This might not be the best option, but you can use a meta-refresh tag to have your page reload on a schedule like this.

<meta http-equiv=""refresh"" content=""30"">

If there is a better way, please let me know.

@smittytone imp.wakeup is just while loop with delay. That is exactly what i tried before. And how do I call http.onrequest(function(req,resp){);} using wakeup or agent.send. can u plz write a code snippet and post here for better understanding.

@makedeck that is exactly what i need. But how do I use that meta link in my squirrel program. can you please post the code snippet over here

Here is an example from one of my temperature monitor programs. When a request is made of the agent with no body, it responds with HTML like so:

`const html1 = @"

    <script src=""""></script>
    <script src=""""></script>
    <script src=""""></script>
    <link href=""//"" rel=""stylesheet"">
    <link href=""//"" rel=""stylesheet"">

    <title>impTherm X&sup2</title>
    <div class='container'>
        <div class='well' style='max-width: 400px; margin: 0 auto 10px; text-align:center;'>
            <h1>impTherm X&sup2<h1>
            <h1>Probe 1: ";

const html2 = @"&degF

Probe 2: “;
const html3 = @”&degF


http.onrequest(function(request, response) { if (request.body == "") { local html = format(html1 + ("%s", probe1) + html2 + ("%s", probe2) + html3); response.send(200, html); } else { response.send(500, "Internal Server Error: " + ex); } });

The forum editor is changing the code a bit, but you can see where it goes. That is a quick and dirty way to display variables from an agent hosted HTML page. Obviously you would need data in probe1 and probe2.

To clarify a minor misconception: imp.wakeup() isn’t quite the same thing as loop() plus a delay. The loop() retains full control of the processor; imp.wakeup() frees processor resources for other tasks - such as watching for messages from the agent.

Is there any way(a function or anything) by which I can execute the agent link automatically without clicking on it every time.

OK, let me get clear in my head what you are trying to do.

You are using the agent URL - something like - in a browser to trigger the agent to take a reading from the weather API, decode the JSON data coming back, and pass it on to the imp, yes?

So you want a way to automate triggering the agent without retyping and re-sending the above URL. Is that correct?

that is exactly what I am trying to do

OK, then I would use jwehr’s advice: build the following code into a text file, save it as .html and then open it from within a browser.

<meta http-equiv=""refresh"" content=""30"">

Here’s the code that I used to try it:



This calls the URL in the src= tag every 10 seconds, as per the content= setting

Thanx smittytone…That worked perfectly…but it gave me the same results as it did when I used while loop inside the http.onrequest…but I am not completely sure about this one though…I will analyze the data for a day or two and let u guys knw…but thank u evry1 for ur valuable time and suggestions.

In that case, I would ask whether you’re getting the weather data more frequently than the source site is being update.

Bit confused here. If you want to do an http request every 60 seconds, there’s no need to involve a browser to hit the agent URL repeatedly; the agent runs 24/7 and you can just get that to run repeated tasks.

Just as with the imp, code in the “root level” of an agent (ie, not a request handler etc) will run when the agent starts. If this includes setting up a repeated call, then the call will run all the time.


`function poll_weather() {
// issue request to fetch weather, process results, send to imp

// do this all again in 60 seconds
imp.wakeup(60, poll_weather);

// Start the polling loop at agent boot time

Ah, so if put imp.wakeup() in the agent code it wakes the agent not the imp. That’s a very important clarification that I’ll add to the docs.

imp.wakeup() doesn’t wake up the agent, it just runs the specified function.

I’ve been redesigning my impTherm X2 thermocouple temperature monitor to work with our new C3V0 development board. Here is an example of the web page that is hosted by the agent. I have the refresh tag set so that the page will automatically refresh every 30 seconds and display the updated temperatures. It’s extremely handy not to have to run an additional webserver. One more reason to love the Electric Imp! I think that is an example of what you are trying to do with your project.

Using refresh is not particularly efficient. The whole reason for AJAX was to prevent whole page refresh, and just dynamically update the data that changes. For small pages we can argue that their is not much saving in terms of data transfer, but the DOM reload can occasionally cause screen flash on a network delay.

On the agent “web page” side, just implement a small Javascript function that periodically calls the Agent API to get the temperature values. Here’s an example that you can include in your agent “web page”

function showTemperatureValues() {

var xmlhttp;

xmlhttp = new XMLHttpRequest();

xmlhttp.onreadystatechange = function () {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
var temperatures = JSON.parse( xmlhttp.responseText );
document.getElementById(‘airtemp’).innerHTML = temperatures.air;
document.getElementById(‘saptemp’).innerHTML =;
else {

  if (xmlhttp.readyState == 4) {
    // Could be complete but with xmlhttp.status other than 200 which indicates a failure;
    document.getElementById('airtemp').innerHTML = 'Web Query Failed';

}‘POST’, document.URL, true);

xmlhttp.send( ‘gettemp’);


function poll(){
setTimeout( poll, 30000 );


I proposed this solution for another thread where it was successfully implemented if you need a complete example: