Netduino home hardware projects downloads community

Jump to content


The Netduino forums have been replaced by new forums at community.wildernesslabs.co. This site has been preserved for archival purposes only and the ability to make new accounts or posts has been turned off.
Photo

How can I get instant (or close to) data from http...?


  • Please log in to reply
15 replies to this topic

#1 Omar (OZ)

Omar (OZ)

    Advanced Member

  • Members
  • PipPipPip
  • 564 posts

Posted 22 January 2011 - 09:27 PM

Using the Netduino plus' ethernet with this code from fred (http://forums.netdui...ted-web-server/) is great, I get to return fancy html and put data in there. But it leaves me hungry, i need a faster way to return data, with little to no timeouts or connection issues.

I am experimenting right now... and returning this html works as expected:

<html>
<head>

<script type="text/javascript">

function refresh()
{
document.getElementById("myDiv").innerHTML= "Bob"
}

</script>
</head>
<body>

<div id="myDiv">Click the button to change this text</div>

<button type="button" onclick="refresh()">Change Content</button>

</body>
</html>


so now I have to figure out how to replace bob with some data from the netduino.

So has anyone accomplished this? also feel free to throw ideas at me even if you arent sure if it will work.

#2 bill.french

bill.french

    Advanced Member

  • Members
  • PipPipPip
  • 260 posts
  • LocationPrinceton, NJ

Posted 22 January 2011 - 10:08 PM

Not entirely sure I understand what you're looking for?
1. How to get your javascript to get live data w/o refreshing the whole page?
2. Or a different way to get data from the netduino?

For #2, I don't know what you're going for.

For #1, check out jquery and the load command: (this would go in your refresh function once you have jquery included)

$('#myDiv').load('data.htm');

(to add jquery, use:
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>
in your <head>)

... in your program.cs: (i have not tested this, so it's more of an idea than anything)

namespace NetduinoPlusWebServer
{
    public class Program
    {
        public static void Main()
        {
            Listener webServer = new Listener(RequestReceived);

            OutputPort led = new OutputPort(Pins.ONBOARD_LED, false);
            while (true)
            {
                // Blink LED to show we're still responsive
                led.Write(!led.Read());
                Thread.Sleep(500);
            }

        }


        private static void RequestReceived(Request request)
        {
          if (request.URL == "data.htm")
          {
          request.SendResponse("some kind of data");
          }


        }


    }
}

Edited by bill.french, 22 January 2011 - 10:15 PM.


#3 Omar (OZ)

Omar (OZ)

    Advanced Member

  • Members
  • PipPipPip
  • 564 posts

Posted 22 January 2011 - 11:06 PM

Not entirely sure I understand what you're looking for?
1. How to get your javascript to get live data w/o refreshing the whole page?
2. Or a different way to get data from the netduino?

For #2, I don't know what you're going for.

For #1, check out jquery and the load command: (this would go in your refresh function once you have jquery included)

$('#myDiv').load('data.htm');


#1. I haven't been able to get that to work. But I'll keep trying.

#4 Chris Seto

Chris Seto

    Advanced Member

  • Members
  • PipPipPip
  • 405 posts

Posted 23 January 2011 - 02:25 AM

See AJAX and something like Jquery.

#5 Omar (OZ)

Omar (OZ)

    Advanced Member

  • Members
  • PipPipPip
  • 564 posts

Posted 23 January 2011 - 03:17 AM

See AJAX and something like Jquery.


Yeah I have been looking at AJAX too, and I used the example here: http://www.w3schools...jax/Default.Asp but it isn't working for some reason.

I am waiting on a micro SD card so I can store html files on it that way I don't have to fiddle with writing out an html page in the code. Once I get it I will get back to this and post my findings if I do figure something out.

#6 Chris Seto

Chris Seto

    Advanced Member

  • Members
  • PipPipPip
  • 405 posts

Posted 23 January 2011 - 03:49 AM

Yeah I have been looking at AJAX too, and I used the example here: http://www.w3schools...jax/Default.Asp but it isn't working for some reason.

I am waiting on a micro SD card so I can store html files on it that way I don't have to fiddle with writing out an html page in the code. Once I get it I will get back to this and post my findings if I do figure something out.

Don't bother writing your own AJAX handler, just use the one within JQuery. If you did write your own AJAX handler, you'd be reinventing the wheel 300000000000 times over. :)

#7 Omar (OZ)

Omar (OZ)

    Advanced Member

  • Members
  • PipPipPip
  • 564 posts

Posted 23 January 2011 - 04:38 AM

Don't bother writing your own AJAX handler, just use the one within JQuery. If you did write your own AJAX handler, you'd be reinventing the wheel 300000000000 times over. :)

Yup, *Note to self: "srtw"* (http://www.stopreinventingthewheel.com)

#8 bill.french

bill.french

    Advanced Member

  • Members
  • PipPipPip
  • 260 posts
  • LocationPrinceton, NJ

Posted 23 January 2011 - 02:53 PM

Yup, *Note to self: "srtw"* (http://www.stopreinventingthewheel.com)


LOL... i added the quote to that page. I think I'll start a collection.

Have you tried playing with my example above? I think it should work.

jquery is definitly the place to start (and probably end) for ajax, i think VS is has intellisense for it?

#9 Omar (OZ)

Omar (OZ)

    Advanced Member

  • Members
  • PipPipPip
  • 564 posts

Posted 23 January 2011 - 03:49 PM

LOL... i added the quote to that page. I think I'll start a collection.

Have you tried playing with my example above? I think it should work.

jquery is definitly the place to start (and probably end) for ajax, i think VS is has intellisense for it?


Your example did work, now I am eagerly waiting for my sd card to arrive, because the last one I bought wasnt compatible with the netduino (Kingston fail <_< )

#10 bill.french

bill.french

    Advanced Member

  • Members
  • PipPipPip
  • 260 posts
  • LocationPrinceton, NJ

Posted 23 January 2011 - 04:00 PM

...wait, so my example worked??? with a netduino?

#11 Chris Seto

Chris Seto

    Advanced Member

  • Members
  • PipPipPip
  • 405 posts

Posted 23 January 2011 - 04:42 PM

LOL... i added the quote to that page. I think I'll start a collection.

Have you tried playing with my example above? I think it should work.

jquery is definitly the place to start (and probably end) for ajax, i think VS is has intellisense for it?

VS has intellisense for all client side web languages.

#12 Omar (OZ)

Omar (OZ)

    Advanced Member

  • Members
  • PipPipPip
  • 564 posts

Posted 23 January 2011 - 05:12 PM

...wait, so my example worked??? with a netduino?

Well I tried like 20 different html codes... but I am pretty sure yours worked, I'll have to use it again later and confirm.

#13 dlampron

dlampron

    New Member

  • Members
  • Pip
  • 1 posts

Posted 16 February 2011 - 02:59 AM

Hi, this a note/solution if someone end up with the same problems I had.

If you are having problems doing AJAX call but you can see your web pages have a look at cross-domain ajax guide . Important by doing this modification you create security holes ... please read more about XSS . Have fun,

Quick fix for me 'work with Firefox, Chrome, not working with IE and Opera... still searching', modify the HTTP header in Request.cs by adding Access-Control-Allow-Origin: *

string header = "HTTP/1.0 200 OK\r\nContent-Type: " + type + "; charset=utf-8\r\nContent-Length: " 
                + response.Length.ToString() 
                + "\r\nCache-Control: no-cache"
                + "\r\nAccess-Control-Allow-Origin: *"
                + "\r\nAccess-Control-Allow-Methods: GET"
                + "\r\nConnection: close\r\n";

Also if you want to make yourself a nice weather station have a look at those nice gauge .

Example :

Client side

<script type="text/javascript">
var speedGauge;

jQuery(document).ready(function() {
speedGauge = new Speedometer("speedometer");
setInterval( "updateGauge()", 1000 );
}); //document.ready

function updateGauge() {
jQuery.get('http://192.168.222.110/analog0', function(data) {
if( data <= 100 && data >= 0 )
speedGauge.SetMPH(data);
else
speedGauge.SetMPH(0);
});
}
</script>


Netduino side

public class Program
    {
        const string WebFolder = "\\SD\\Web";
        public static string trimpot  = "0";

        public static void Main()
        {
            OutputPort   led = new OutputPort(Pins.ONBOARD_LED, false);
            AnalogInput an0 = new AnalogInput(Pins.GPIO_PIN_A0);

            an0.SetRange(0, 100);
            trimpot =  an0.Read().ToString();

            Listener webServer = new Listener(RequestReceived);

            while (true)
            {
                // Blink LED to show we're still responsive
                led.Write(!led.Read());
                trimpot = an0.Read().ToString();
                Thread.Sleep(500);
            }

        }

        private static void RequestReceived(Request request)
        {
            // Quick'n'dirty ajax call
            if( request.URL == @"/analog0" )
                request.SendResponse(trimpot);
            else
                request.Send404();
        }

        // ...

Screenshot

Attached File  screenshot.jpg   120.15KB   87 downloads

#14 SirPoonga

SirPoonga

    Advanced Member

  • Members
  • PipPipPip
  • 96 posts

Posted 16 February 2011 - 09:35 PM

I haven't used jQuery before, I am wondering something. Here's my setup. I have the netduino plus connected to a Fonera wireless router with DD-WRT on it. My Ipod can connect to the fonera and bring up a web page that is on the Fonera. The web page has 4 buttons - next color 1, next color 2, next animation, and favorite. Next color 1 and next color 2 will tell the netduino to select the next color for 1 and 2. Next animation will select the next animation. And favorite will tell the netduino to set the colors and animations to my favorite. I would like to retrieve the current colors to display on the webpage. I want to do the minimal amount of interference on the netduino so the LEDs can animate at full speed. So, is it possible for jQuery or Javascript to create a socket connection? Or, alternatively, can jQuery send a GET or POST to the netduino IP with the command to be run and the netduino will return with the color data for the webpage to display without reloading the webpage? Edit: I think I found an answer. http://code.google.c...i/SimpleExample Edit: I might have found an answer to my own question. http://code.google.c...i/SimpleExample I can setup a simple socket connection on the netduino then. Right now, my plans were to to a simple web server on the netduino. http://192.168.101.2 to get the webpage which would be generated in code to embed the color info. The buttons would be like <a href='?C1'><img src='binary data'/></a> to change color 1. But if I can host he webpage on the wireless router that should be less data/interruptions for the netduino.

#15 SirPoonga

SirPoonga

    Advanced Member

  • Members
  • PipPipPip
  • 96 posts

Posted 16 February 2011 - 10:34 PM

I think I found an answer to my situation http://code.google.c...i/SimpleExample requires flash I think. Or HTML5 WebSocket - limited browsers though.

#16 tel001

tel001

    Member

  • Members
  • PipPip
  • 13 posts
  • LocationVancouver BC

Posted 10 October 2011 - 07:32 PM

Wow! You guys Rock!....jquery looks like fun :D http://jqueryui.com/demos/button/ Like I needed another tangental learning curve :)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

home    hardware    projects    downloads    community    where to buy    contact Copyright © 2016 Wilderness Labs Inc.  |  Legal   |   CC BY-SA
This webpage is licensed under a Creative Commons Attribution-ShareAlike License.