Go Down

Topic: Webdunio - Website from SD Card (Read 1 time) previous topic - next topic

DannyP

Hi,

I am using an Arduino Mega 2560 in my project with an ethernet shield. By now, every settings are done fix in the code. I'd like to make them accessible through a Web-GUI. For the webserver I use Webduino. On first try, I defined all neccessary webpages in the C-Code via the P() macro. The programm gets large.

Therefore I like to load the webpages from SD card. That works fine, as long as I don't need to output some values.
I'm using this code, to read an HTML page from SD and display it at the client's browser:

Code: [Select]
void sdHTML(WebServer &server, WebServer::ConnectionType type, char *url_tail, bool tail_complete)
{
  server.httpSuccess();
 
  File datei;
 
  datei = SD.open("/web/gui/INDEX~1.HTM", FILE_READ);
  if(datei)
  {
    int16_t c;
    while((c = datei.read()) >= 0) {
      server.print((char)c);
    }
    datei.close();
  }
  else
  {
    Serial.println("Datei wurde nicht gefunden");
  }
}


INDEX.HTML is a normal HTML page, which some <p>...</p> Tags in it. But now I want to display at this page for example the value, which comes from Pin 7 or other.
How can I write this Information to the Webpage?

Thanks in advance!
Best regards.
Daniel

PaulS

The simplest way is to create several files on the SD card, and a function to read and output one file.
Then, something like this:
Code: [Select]
sendToClient("header.htm");
sendToClient("form_head.htm");
// some code to send html statements for form objects with sensor data
sendToClient("form_foot.htm");
sendToClient("footer.htm");
The art of getting good answers lies in asking good questions.

zoomkat

One way to display pin values in a web page.

Code: [Select]

// zoomkat's meta refresh data frame test page 8/17/13
// use http://192.168.1.102:84 in your brouser for main page
// http://192.168.1.102:84/data static data page
// http://192.168.1.102:84/datastart meta refresh data page
// for use with W5100 based ethernet shields
// set the refresh rate to 0 for fastest update
// use STOP for single data updates

#include <SPI.h>
#include <Ethernet.h>

const int analogInPin0 = A0;
const int analogInPin1 = A1;
const int analogInPin2 = A2;
const int analogInPin3 = A3;
const int analogInPin4 = A4;
const int analogInPin5 = A5;

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address
byte ip[] = { 192, 168, 1, 102 }; // arduino ip in lan
byte gateway[] = { 192, 168, 1, 1 }; // internet access via router
byte subnet[] = { 255, 255, 255, 0 }; //subnet mask
EthernetServer server(84); //server port
unsigned long int x=0; //set refresh counter to 0
String readString;

//////////////////////

void setup(){
  Serial.begin(9600);
    // disable SD SPI if memory card in the uSD slot
  pinMode(4,OUTPUT);
  digitalWrite(4,HIGH);

  Ethernet.begin(mac, ip, gateway, gateway, subnet);
  server.begin();
  Serial.println("meta refresh data frame test 8/17/13"); // so I can keep track of what is loaded
}

void loop(){
  EthernetClient client = server.available();
  if (client) {
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
         if (readString.length() < 100) {
          readString += c;
         }
        //check if HTTP request has ended
        if (c == '\n') {

          //check get atring received
          Serial.println(readString);

          //output HTML data header
          //client.println("HTTP/1.1 200 OK");
          //client.println("Content-Type: text/html");
          //client.println();
         
          client.print(F("HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n"));

          //generate data page
          if(readString.indexOf("data") >0) {  //checks for "data" page
            x=x+1; //page upload counter
            client.print("<HTML><HEAD>");
            //meta-refresh page every 1 seconds if "datastart" page
            if(readString.indexOf("datastart") >0) client.print("<meta http-equiv='refresh' content='1'>");
            //meta-refresh 0 for fast data
            if(readString.indexOf("datafast") >0) client.print("<meta http-equiv='refresh' content='0'>");
            client.print("<title>Zoomkat's meta-refresh test</title></head><BODY><br>");
            client.print("page refresh number: ");
            client.print(x); //current refresh count
            client.print("<br><br>");
           
              //output the value of each analog input pin
            client.print("analog input0 is: ");
            client.print(analogRead(analogInPin0));
           
            client.print("<br>analog input1 is: ");
            client.print(analogRead(analogInPin1));
                       
            client.print("<br>analog input2 is: ");
            client.print(analogRead(analogInPin2));
           
            client.print("<br>analog input3 is: ");
            client.print(analogRead(analogInPin3));
                                   
            client.print("<br>analog input4 is: ");
            client.print(analogRead(analogInPin4));
           
            client.print("<br>analog input5 is: ");
            client.print(analogRead(analogInPin5));
            client.print("<br></BODY></HTML>");
           }
          //generate main page with iframe
          else
          {
            client.print(F("<HTML><HEAD><TITLE>Zoomkat's frame refresh test</TITLE></HEAD>"
            "Zoomkat's Arduino frame meta refresh test 8/17/13"
            "<BR><BR>Arduino analog input data frame:<BR>"
            "&nbsp;&nbsp;<a href='/datastart' target='DataBox' title=''yy''>META-REFRESH</a>"
            "&nbsp;&nbsp;&nbsp;&nbsp;<a href='/data' target='DataBox' title=''xx''>SINGLE-STOP</a>"
            "&nbsp;&nbsp;&nbsp;&nbsp;<a href='/datafast' target='DataBox' title=''zz''>FAST-DATA</a><BR>"
            "<iframe src='/data' width='350' height='250' name='DataBox'>"
            "</iframe><BR></HTML>"));
          }
          delay(1);
          //stopping client
          client.stop();
          //clearing string for next read
          readString="";
        }
      }
    }
  }
}

Google forum search: Use Google Search box in upper right side of this page.
Why I like my 2005 Rio Yellow Honda S2000  https://www.youtube.com/watch?v=pWjMvrkUqX0

DannyP

Hi,

Thank you for your hint, PaulS. I hoped, that there will be an easier way, than using multiple Files of SD Card.

zoomkat, in your example, the Website is not read from SD Card.

Best regards.

PaulS

Quote
I hoped, that there will be an easier way, than using multiple Files of SD Card.

You could use markers in one file that define when the Arduino needs to substitute some value or execute some block of code, but that is not easier than reading multiple files.
The art of getting good answers lies in asking good questions.

zoomkat

The below site may have what you are looking for.

http://startingelectronics.com/tutorials/arduino/ethernet-shield-web-server-tutorial/SD-card-gauge/
Google forum search: Use Google Search box in upper right side of this page.
Why I like my 2005 Rio Yellow Honda S2000  https://www.youtube.com/watch?v=pWjMvrkUqX0

Go Up