Forum Discussion

Altera_Forum's avatar
Altera_Forum
Icon for Honored Contributor rankHonored Contributor
15 years ago

Interniche / Website Problem

Hi,

I have a little problem... and i hope you can Help me.

I have a DBC3C40 Board, on this board are Running the Webserver Projekt from interniche with the NiosII.

If i Click a Button on the Website the board is traying an Function Like a LED goes on or the Seven seg. is counting.

But now i want to press a Button on the Board and the Website must be display it.

How can i do/realize this?

I have the Quartus 10.1sp1 version.

9 Replies

  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    If you mean you want a HTML web page be instantly updated when you press the button, this is not possible. Web pages are generated or transferred at the specific client request. The server can't send a page update in an automatic way. AFAIK the only way to do something similar with HTML is using the refresh command in the <HEAD> section and this will instruct the client to periodically reload the page.

    If the webserver is not using plain HTML maybe it is possible. I don't know...

    Anyway, I own the same DBC3C40 board and the webserver example which came with it is made of simple static HTML pages. If you want to show button status, first of all you must use dynamic HTML and cgi scripts.
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    Thanks for the fast answer.

    have you a example, how can i realiz this webpage?

    That the Webpage can display the Button status? pressed or no pressed?

    thanks.
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    I made it years ago with another processor. I never used Nios/Interniche for a webserver, so I really don't know if its capabilities are similar to that I used.

    For such a function you have to generate the page dynamically, i.e. your http server must change some page contents before transmitting it to the stack.

    I did it with a template HTML page, where the info I had to replace dynamically was identified by some markers (special characters sequences). Then a special code processed the http text file during the transfer from memory to network stack, and replaced the markers with the required string (i.e. if a marker "@#4" was in the template HTML as a placeholder for the status of button 4, it was replaced with "ON" or "OFF").
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    hmm... okay but how can i send this info to the site? have you a example for this?

    My first idea was, if i press a button than change a variable x this value from 0 to 1, now came the problem, how can i transmit this to the page? or can the page read this variable from the c-code? i believe, that i must send this information to the Site, as you wrote it. But i have realy no idea how can i make this, have you an example or a snippet code?

    thanks for the Help.
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    As I said above, I never used Interniche http, so I can't give you an example.

    You must have access to the function that transfers web pages to the stack in order to do what I said. I don't know if this is allowed with Interniche: you must try to browse the source code, if you have access to it, or search the web for any reference design (try googling "dynamic html")
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    Hi w.szyja, (http://www.alteraforum.com/forum/member.php?u=34801)

    that's the problem with the webserver example from interniche. It is not a full implementation so things for dynamic html like cgi-scripts don't work. It is easy to change settings of the board through the website. I already did this. But I couldn't find a way to send information from board to the website.

    Perhaps it is just a problem that I have to make the "send-function" which is used in http.c accessible from the webserver.c which contains my application code.

    You should search for

    send(conn->fd,(void*)canned_http_response,strlen(canned_http_response),0);

    which shows the html content written in the http.c source code as a website.

    Urmel
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    Hello Users,

    Thanks for the fast answer.

    @ Slaker,

    thanks for the Project of Webserver with Ajax, that what i was looking for. I've got it to run on the DBC3C40 Board. But i don't understand all of the Send function to the Webpage.

    I will make a little example for this. it should be a counter on the website. I have search for all "Progress" entries in the "Webserver.c" and this are too many for me to understand this.

    on Line 460-464 is an Struktur that initializing the values.

    on line 952 is an strcpy(...) Function ? why ? need i this too?

    and on line 1051 is a get_funcs progress_field=....

    and on the line 1440 is a big Function for the sendprogress....

    Can you pleas Help me, and make me a little example or tell me what i must be implement or note to make a easy function to Display somthing on the website about the webserver.c file.

    thanks for the Help.

    Sincerely,

    W.Szyja

    I have edit this files and i tries to implement a counter and display this on the website but this unhappily not Work.

    my changes on the webserver.c :

    on line 460:

    
    struct
    { 
    int dbcounter;
    } dbcount;
    

    on line 1067:

    
     
    /* GET func mapping. */
    get_funcs dbcount_field =
    { 
    "/DBCOUNT",
    send_dbcount
    };
    

    on line 963:

    
     
    void test_counter( http_conn* conn )
    {
    unsignedint dbcounter;
    dbcounter = 0;
     
    if (dbcounter >= 32)
    {
    dbcounter = 1; 
    }
     
    else 
    {
    dbcounter = dbcounter << 1; 
    }
    usleep(20000);
    dbcount.dbcounter = dbcounter;
    }
    
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    on line 1053:

    
    void send_dbcount(http_conn* conn)
    {
    alt_u8* tx_wr_pos = conn->tx_buffer;
    alt_u8* dbcounter_str = (alt_u8*) malloc(512);
    alt_u8* dbcounter_str_pos = dbcounter_str;
    unsignedint content_length;
    int result;
    // Send the version string.
    tx_wr_pos += sprintf( tx_wr_pos, HTTP_VERSION_STRING );
    // Send the HTTP_OK header element.
    tx_wr_pos += sprintf( tx_wr_pos, HTTP_OK_STRING );
    // Send the content type header element.
    tx_wr_pos += sprintf( tx_wr_pos, HTTP_CONTENT_TYPE_JS );
    // Add the cache content header element.
    tx_wr_pos += sprintf( tx_wr_pos, "Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0\r\n" );
    // Now build the content string and determine it's length. This content is in JSON (Javascript Object Notation)
    // and is easily handled by any browser that understands Javascript.
    dbcounter_str_pos += sprintf( dbcounter_str, "new Object ({ 'count' : '%d'})\r\n",dbcount.dbcounter);
    content_length = strlen( dbcounter_str );
    // Add the Content-Length header element.
    tx_wr_pos += sprintf( tx_wr_pos, HTTP_CONTENT_LENGTH );
    tx_wr_pos += sprintf( tx_wr_pos, "%d\r\n", content_length );
    // Since the connection will be closed after sending this packet, add the HTTP_CLOSE header element
    // and mark the end of headers by sending HTTP_CR_LF.
    tx_wr_pos += sprintf( tx_wr_pos, HTTP_CLOSE );
    tx_wr_pos += sprintf( tx_wr_pos, HTTP_CR_LF );
    // Now, add in the progress_str content (actual content for a 'GET /PROGRESS' request.
    tx_wr_pos += sprintf( tx_wr_pos, "%s", dbcounter_str );
    // Send the reply packet.
    result = send( conn->fd, conn->tx_buffer, (tx_wr_pos - conn->tx_buffer), 0 );
    if (result < 0)
    {
    fprintf( stderr, " header send returned %d\n", result );
    conn->
    state = RESET;
    return;
    }
    free( dbcounter_str );
    }
    

    but what do the function on line 1905 ?

    
    int http_handle_get( http_conn* conn )
    {
    int ret_code = 0;
    if ( !strcmp( conn->uri, progress_field.name ))
    {
    conn->state = CLOSE;
    progress_field.
    func( conn );
    }
    else
    {
    // Find file from uri 
    ret_code = http_find_file( conn );
    }
    return ret_code;
    }
    

    and how can i edit them to display more then one data on the website ?

    I have edit the website.h likes this: (on line 199)

    
    void send_dbcount( http_conn* conn );
    

    and the Javascript file show jet so:

    
    interval_msec = 2000;
    debug = false;
    function update() {
      // Handle the various browsers... Pre-IE7 doesn't support XMLHttpRequest.
      if (window.XMLHttpRequest && !(window.ActiveXObject)) {
        try {
          req = new XMLHttpRequest();
        } catch(e) {
          req = false;
        }
      // IE/Windows ActiveX
        } else if (window.ActiveXObject) {
          try {
            req = new ActiveXObject("Msxml2.XMLHTTP");
          } catch(e) {
            try {
              req = new ActiveXObject("Microsoft.XMLHTTP");
          } catch(e) {
            req = false;
          }
        }
      }
      // If request isn't possible, just return right away.
      if (!req) return;
      req.open("GET","/DBCOUNT", false);
      req.send(null);
      if(req.status == 200) {
        // The server sent a response.
        var dbcount = eval(req.responseText);
        if(debug) {
          document.getElementById('debug_panel').innerHTML += "counter = " + dbcount.dbcounter + " "  + '<br />';
        }
        bar = document.getElementById('dbcounter');
     
        w = (dbcount.counter/100) * 180;
        bar.style.width = w + 'px';
        document.getElementById('tp').innerHTML = progress.percent + "%";
      }
      else {
        // Something didn't work....at least set an alert with useful information!
        alert("Error " + request.status + ":  " + request.statusText );
      }
    }
    function startProgress() {
      // Code needs to go here to generate the progress bar.
      interval = window.setInterval(
        function () {
          update();
        },
        interval_msec
      );
    };
     
    

    the last file i have edit is the HTML index file and they show like this:

    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <title>Nios II Example Web Server</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <link href="style.css" rel="stylesheet" type="text/css" />
    <script type="text/javascript" src="dbcounter.js"></script>
    </head>
    <body> 
         <div id="dbcount" style="width:  200px; border 1px solid black">
           <div id="dbcounter" style="width: 0px; height: 1.5ex; background-color: black; border: 0px solid white; margin-left: 0.5em; margin-right: 0.5em; margin-top: 1ex">
             &nbsp;
           </div>
     
    </body>
    </html>
     
    

    have anybody i idea ?