Arduino Ethernet Web

Hello

I am doing a project using Arduino Uno+Ethernet.

What I am trying to do is there are couple of menu on top of web page and when one of the menus is clicked then go to designated page(URI)
I found a good example but I am stuck in following problem

Here is my code.

#define NUM_PAGES 5

// HTTP Request message
PROGMEM prog_char content_404 = “HTTP/1.1 404 Not Found\nServer: arduino\nContent-Type: text/html\n\nArduino Web Server - Error 404

Error 404: Sorry, that page cannot be found!

”;
PGM_P page_404 PROGMEM = { content_404 }; // table with 404 page

// HTML Header for pages
PROGMEM prog_char content_main_header = “HTTP/1.0 200 OK\nServer: arduino\nCache-Control: no-store, no-cache, must-revalidate\nPragma: no-cache\nConnection: close\nContent-Type: text/html\n”;
PROGMEM prog_char content_main_top = “Arduino Web Server<style type=“text/css”>table{border-collapse:collapse;}td{padding:0.25em 0.5em;border:0.5em solid #C8C8C8;}

Arduino Web Server

”;
PROGMEM prog_char content_main_menu = “<table width=“500”><td align=“center”><a href=”/">Page 1<td align=“center”><a href=“page2”>Page 2<td align=“center”><a href=“page3”>Page 3<td align=“center”><a href=“page4”>Page 4";
PROGMEM prog_char content_main_footer = “”;
PGM_P contents_main PROGMEM = { content_main_header, content_main_top, content_main_menu, content_main_footer }; // table with 404 page
#define CONT_HEADER 0
#define CONT_TOP 1
#define CONT_MENU 2
#define CONT_FOOTER 3
////////////////////////////////////////////////////////////////////////////////////////////////////////

char hour[]={‘0’,‘1’};// this value will be sent from another module,but for testing I just put any number.

// Page 1
PROGMEM prog_char http_uri1 = “/”;
PROGMEM prog_char content_title1 = “

Page 1

”;
PROGMEM prog_char content_page1 = "

Content of Page 1

Nothing… yet.

";

// Page 2
PROGMEM prog_char http_uri2 = “/page2”;
PROGMEM prog_char content_title2 = “

Page 2

”;
PROGMEM prog_char content_page2 = “

Content of Page 2

Write the hour variable

”;//<------I want to put variable between

hour

I don’t put every code but what I want to do is when page2 is called I want to put variable between

so that display the hour. the hour variable is declared above.
So basically adding hour string to content_page2 string.

First I try to us strcat(),but compile error occurred, which is type conversion is needed.

what I did using strcat() is
strcat(content_page2, hour);

I guess prog_char is not same as char,so that’s why the compiler spit it out. I think I need a certain function similar with strcat(),which accept prog_char type.
However, I cant find a function that I want as well as I am not sure I am right to use strcat() to insert string to PROGMEM prog_char.

If anyone knows a good example or how to add string to prog_char string,please answer for me.

Most of the string functions have PROGMEM-specific versions, like strcpy_P. Google will help.

AWOL: Most of the string functions have PROGMEM-specific versions, like strcpy_P. Google will help.

I already looked at that function. char * strcat_P (char *, PGM_P), so char * is dest and PGM_P is src. but I want to wrtie value to PGM_P not the other way. Read hour[] and put the values into PGM_P.

but I want to wrtie value to PGM_P

Not possible.

AWOL:

but I want to wrtie value to PGM_P

Not possible.

Thanks for answering. If I can't use strcat_p(), then how can I add a string value(char[]) to prog_char array? Seems like I cant modify pro_char, once it is defined

Again, Here is html stored in flash, I want to display value when the page2 is called. the page2 is stored in flash like the above code

Again, Here is html stored in flash, I want to display value when the page2 is called. the page2 is stored in flash like the above code

The data sent to the client does not all have to come from progmem. Send the stuff before the value from progmem, send the value, then send the rest of the stuff from progmem.

Thanks for replying to me.

Probably I din’t explain well what I need to do.

Here is my code

char hour[]={'0','1'};// this value will be sent from another module,but for testing I just put any number.

PROGMEM prog_char content_page2[] = "<hr /><h3>Content of Page 2</h3><p>Write the hour variable</p>";//<------I want to put variable between <p>hour</p>

As you see, I try to display hour string when content_page2(html) is called
there are couple of web pages linked on menu so when one of menu is clicked, diplay designated web page .That’s why I stored those in flash.

the hour will be changed,so when content_page2 is called, the hour string should be placed in between

in html
So, I need to add hour string into html string as page2 is called

I am not sure how I can do that

hello

I posted my problem yesterday. but I can’t get a right answer that I am looking for.
Maybe I didn’t explain whole codes well, so I post code that I am trying to modify.

I found an example from Arduino playground.

What I like to do is there is a main web page and there are couple of menu calling different html pages.

this code use flash memory,so every page are stored in flash. However, as the page2 menu is called or clicked, I want to display variable,so it will change and display every time as page2 is called.

#define STRING_BUFFER_SIZE 128
char buffer[STRING_BUFFER_SIZE];

// to store data from HTTP request
#define STRING_LOAD_SIZE 128
char load[STRING_LOAD_SIZE];

// POST and GET variables
#define STRING_VARS_SIZE 128
char vars[STRING_VARS_SIZE];

/**********************************************************************************************************************
*                                   Strings stored in flash of the HTML we will be transmitting
***********************************************************************************************************************/

#define NUM_PAGES 3

// HTTP Request message
PROGMEM prog_char content_404[] = "HTTP/1.1 404 Not Found\nServer: arduino\nContent-Type: text/html\n\n<html><head><title>Arduino Web Server - Error 404</title></head><body><h1>Error 404: Sorry, that page cannot be found!</h1></body>";
PGM_P page_404[] PROGMEM = { content_404 }; // table with 404 page


#define CONT_HEADER 0
#define CONT_TOP 1
#define CONT_MENU 2
#define CONT_FOOTER 3

// Page 1
PROGMEM prog_char http_uri1[] = "/";
PROGMEM prog_char content_title1[] = "<h2>Page 1</h2>";
PROGMEM prog_char content_page1[] = "<hr /><h3>Content of Page 1</h3><p>Nothing... yet.</p>
<form action=\"/login\" method=\"POST\"><input type=\"text\" name=\"prova\"><input type=\"submit\" value=\"post\"></form>";
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
[color=blue][b]Page 2 will display time
// Page 2
PROGMEM prog_char http_uri2[] = "/page2";
PROGMEM prog_char content_title2[] = "<h2>Page 2</h2>";
PROGMEM prog_char content_page2[] = "<hr /><h3>Content of Page 2</h3><p>Nothing here.</p>";/////<--------instead of "Nothing here." display hour[/b][/color]
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Page 3
PROGMEM prog_char http_uri3[] = "/page3";
PROGMEM prog_char content_title3[] = "<h2>Page 3</h2>";
PROGMEM prog_char content_page3[] = "<hr /><h3>Content of Page 3</h3><p>No no no, white page here.</p>";


// declare tables for the pages
PGM_P contents_titles[] PROGMEM = { content_title1, content_title2, content_title3, content_title4, content_title5 }; // titles
PGM_P http_uris[] PROGMEM = { http_uri1, http_uri2, http_uri3, http_uri4, http_uri5 }; // URIs
PGM_P contents_pages[] PROGMEM = { content_page1, content_page2, content_page3, content_page4, content_page5 }; // real content

/**********************************************************************************************************************
*                                        define HTTP return structure ID for parsing HTTP header request
***********************************************************************************************************************/
struct HTTP_DEF {
  int pages;
  char vars[20];
} ;

/**********************************************************************************************************************
*                                                        Shared variable
***********************************************************************************************************************/
Server server(80);

void setup() {
  Ethernet.begin(mac, ip);
  server.begin();

  Serial.begin(9600); // DEBUG
}

/**********************************************************************************************************************
*                                                           Main loop
***********************************************************************************************************************/

void loop() {
  Client client = server.available();
  if (client) { // now client is connected to arduino

    // read HTTP header request... so select what page client are looking for
    HTTP_DEF http_def = readHTTPRequest(client);

    if (http_def.pages > 0) {
      sendPage(client,http_def);
    } else {
      contentPrinter(client,(char*)pgm_read_word(&(page_404[0])));
    }

    // give the web browser time to receive the data
    delay(1);
    client.stop();
  }

}

/**********************************************************************************************************************
*                                              Method for read HTTP Header Request from web client
***********************************************************************************************************************/
struct HTTP_DEF readHTTPRequest(Client client) {
  char c;
  int i;

  // use buffer, pay attention!
  int bufindex = 0; // reset buffer
  int loadindex = 0; // reset load

  int contentLength = 0; // reset POST content length
  char compare[50]; // page comparison (URI selection)

  HTTP_DEF http_def; // use the structure for multiple returns

  http_def.pages = 0; // default page selection... error

  // reading all rows of header
  if (client.connected() && client.available()) { // read a row
    buffer[0] = client.read();
    buffer[1] = client.read();
    bufindex = 2;
    // read the first line to determinate the request page
    while (buffer[bufindex-2] != '\r' && buffer[bufindex-1] != '\n') { // read full row and save it in buffer
      c = client.read();
      if (bufindex<STRING_BUFFER_SIZE) buffer[bufindex] = c;
      bufindex++;
    }

    // select the page from the buffer (GET and POST) [start]
    for(i = 0; i < NUM_PAGES; i++) {
      strcpy_P(load, (char*)pgm_read_word(&(http_uris[i])));
      // GET
      strcpy(compare,"GET ");
      strcat(compare,load);
      strcat(compare," ");
      Serial.print("GET compare: "); // DEBUG
      Serial.println(compare); // DEBUG
      if (strncmp(buffer,compare, strlen(load)+5)==0) {
        http_def.pages = i+1;
        break;
      }

      // POST
      strcpy(compare,"POST ");
      strcat(compare,load);
      strcat(compare," ");
      Serial.print("POST compare: "); // DEBUG
      Serial.println(compare); // DEBUG
      if (strncmp(buffer,compare, strlen(load)+6)==0) {
        http_def.pages = i+1;
        break;
      }

    }
    // select the page from the buffer (GET and POST) [stop]

    // read other stuff (for POST requests) [start]
    if (strncmp(buffer, "POST /", 5)==0) {
      processRequest:
      loadindex = 2; // reset load

      memset(load,0,STRING_LOAD_SIZE);
      load[0] = client.read();
      load[1] = client.read();
      while (load[loadindex-2] != '\r' && load[loadindex-1] != '\n') {
        c = client.read();
        if (loadindex<STRING_BUFFER_SIZE) load[loadindex] = c;
        loadindex++;
      }
      if (strncmp(load, "Content-Length: ",16)==0) {
        loadindex = 16;
        for(i = loadindex; i< strlen(load) ; i++) {
          if (load[i] != ' ' && load[i] != '\r' && load[i] != '\n') {
            vars[i-loadindex] = load[i];
          } else {
            break;
          }
        }

        contentLength = atoi(vars);
        memset(vars,0,STRING_VARS_SIZE);
        client.read(); client.read(); // read null line
        i = 0;
        while (i<contentLength) {
          c = client.read();
          vars[i] = c;
          ++i;
        }

      } else {
      	goto processRequest;
      }
    }
    // read other stuff (for POST requests) [stop]


    // clean buffer for next row
    bufindex = 0;
  }

//      delay(10); // removing this nothing work... if you understand why mail me a bebbo [at] bebbo [dot] it
  Serial.print("Grepped page: "); // DEBUG
  Serial.println(http_def.pages); // DEBUG

  strncpy(http_def.vars,vars,STRING_VARS_SIZE);

  return http_def;
}

on top of the code…

PROGMEM prog_char content_page2[] = “


Content of Page 2

Nothing here.

”;/////<--------instead of “Nothing here.” display hour

I try to change prog_char but I haven’t figured it out for a long time. I think, I can not modify PROGMEM cause it is read only.

are there any possible solutions for that? if not, I need another example to implement multi web pages.

I will appreciate answer my question based on code or let me know the another good example.

I have spent so much time but still stuck in the school to figure it out.

I posted my problem yesterday. but I can’t get a right answer that I am looking for.

The answer that you are looking for is “Here is some code to do exactly what you want”.

I have spent so much time but still stuck in the school to figure it out.

You are in school to learn. So, take this opportunity to learn.

PROGMEM prog_char content_page2[] = "<hr /><h3>Content of Page 2</h3><p>Nothing here.</p>";/////<--------instead of "Nothing here." display hour

Split this into two parts. The declaration:

PROGMEM prog_char content_page2B[] = "<hr /><h3>Content of Page 2</h3><p>";
PROGMEM prog_char content_page2A[] = "</p>";

The sending:

// Send content_page2B
// Send hour
// Send content_page2A

It is up to you to figure out exactly how to do the sending.

If you are going to start a new thread with the same question, close this one, so no one else wastes time trying to help you here.

PaulS: If you are going to start a new thread with the same question, close this one, so no one else wastes time trying to help you here.

Thanks for helping me. I tried to delete the previous question before post this question,but I am not authrized to delete. Sorry I just joined the forum 2 days ago, so I might be missing the function to delete

I like learning,but that's depending on time base, my project due is 15th,Dec. I am kind of chasing by time. In the school I have learned using HCS12,so web interfacing is new to me

I implemented individual page and they works well, but my project need to look like just like regular web page. I put every page into one file and it didn't work, the server is pending without no error.

that's why I am looking for another approach.

I will think how to implement based on your answer, but I am not sure the sample code's structure and the way of using flash memory. Anyhow I will try, Thanks for helping me.