Setting IP using SD. First page loads. Can't reload web page.


i have been trying set arduino read ip information sd card. far have part working. bit i've having issue refreshing web page. can load page on first attempt after page hangs.

the file "network.txt" read , parsed set mac, ip, subnet mask, gateway , dns each value on it's own line blank line @ end. far part working fine.

but server portion in loop works once , not serve page unless reset arduino.

attached code. forgive me if looks bit messy.

please note i'm using 1.0.6 ide. it's 1 find accepts sscanf valid mentioned works fine.


code: [select]

#include <sd.h>
#include <spi.h>
#include <ethernet.h>

byte mymac[6];
byte myip[4];
byte mynm[4];
byte mygw[4];
byte mydns[4];
ethernetserver server(80);


void setup() {
 serial.begin(9600);  

 pinmode(10,output);
 //digitalwrite(10,high);

 if(!sd.begin(4)) serial.println("sd fail");
 else serial.println("sd ok");

 file fh = sd.open("network.txt",file_read);
 char netbuffer[32];
 
 if(!fh)
 {
   serial.println("sd open fail");
   return;    
 }

 int chpos = 0;
 int lineno = 0;
 
 while(fh.available())
 {
   char ch = fh.read();
   if(ch == '\r') {
     chpos = 0;

     switch(lineno) {
       case 0:
           sscanf(netbuffer,"%2x:%2x:%2x:%2x:%2x:%2x",&mymac[0],&mymac[1],&mymac[2],&mymac[3],&mymac[4],&mymac[5]);  
       break;

       case 1:
           sscanf(netbuffer,"%u.%u.%u.%u",&myip[0],&myip[1],&myip[2],&myip[3]);  
       break;

       case 2:
           sscanf(netbuffer,"%u.%u.%u.%u",&mynm[0],&mynm[1],&mynm[2],&mynm[3]);  
       break;

       case 3:
           sscanf(netbuffer,"%u.%u.%u.%u",&mygw[0],&mygw[1],&mygw[2],&mygw[3]);  
       break;

       case 4:
           sscanf(netbuffer,"%u.%u.%u.%u",&mydns[0],&mydns[1],&mydns[2],&mydns[3]);  
       break;
     }

     lineno++;
   }
   else if(ch == '\n') {
      // nothing
   }
   else if(chpos < 31) {
     netbuffer[chpos] = ch;
      chpos++;
     netbuffer[chpos] = 0;
   }
 }
 
 fh.close();

 int x;
 
 serial.print("\r\nmac ");
 for(x=0;x<6;x++) {
   serial.print(mymac[x],hex);
   if(x<5) serial.print(":");
 }

 serial.print("\r\nip ");
 for(x=0;x<4;x++) {
   serial.print(myip[x],dec);
   if(x<3) serial.print(".");
 }

 serial.print("\r\nnetmask ");
 for(x=0;x<4;x++) {
   serial.print(mynm[x],dec);
   if(x<3) serial.print(".");
 }

 serial.print("\r\ngateway ");
 for(x=0;x<4;x++) {
   serial.print(mygw[x],dec);
   if(x<3) serial.print(".");
 }

 serial.print("\r\ndns ");
 for(x=0;x<4;x++) {
   serial.print(mydns[x],dec);
   if(x<3) serial.print(".");
 }

 serial.println("\r\nstarting ethernet");
 ethernet.begin(mymac,myip,mydns,mygw,mynm);
 //digitalwrite(10,low);
 server.begin();
 serial.println(ethernet.localip());
 serial.println("============================");
}

// ===================================================================================
// ===================================================================================

void loop()
{
// listen incoming clients
  ethernetclient client = server.available();
  if (client) {
    serial.println("new client");
    // http request ends blank line
    boolean currentlineisblank = true;
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        serial.write(c);
        // if you've gotten end of line (received newline
        // character) , line blank, http request has ended,
        // can send reply
        if (c == '\n' && currentlineisblank) {
          // send standard http response header
          client.println("http/1.1 200 ok");
          client.println("content-type: text/html");
          client.println("connection: close");  // connection closed after completion of response
          client.println("refresh: 5");  // refresh page automatically every 5 sec
          client.println();
          client.println("<!doctype html>");
          client.println("<html>");
          // output value of each analog input pin
          for (int analogchannel = 0; analogchannel < 6; analogchannel++) {
            int sensorreading = analogread(analogchannel);
            client.print("analog input ");
            client.print(analogchannel);
            client.print(" ");
            client.print(sensorreading);
            client.println("<br />");
          }
          client.println("</html>");
          break;
        }
        if (c == '\n') {
          // you're starting new line
          currentlineisblank = true;
        } else if (c != '\r') {
          // you've gotten character on current line
          currentlineisblank = false;
        }
      }
    }
    // give web browser time receive data
    delay(1);
    // close connection:
    client.stop();
    serial.println("client disconnected");
    serial.println("============================");
  }
}


thank can give me.

sp

attached network.txt file i'm using.

thank again.

sp


Arduino Forum > Using Arduino > Networking, Protocols, and Devices (Moderator: fabioc84) > Setting IP using SD. First page loads. Can't reload web page.


arduino

Comments

Popular posts from this blog

Flip address is out of range arduino uno r3

Arduino Uno not uploading

Indesign and MathType fonts