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

I have been trying to set up my arduino to read the IP information from an SD card. So far I have that part working. Bit I’ve having an issue with refreshing a web page. I can load the page on the first attempt but after that the page just hangs.

The file “network.txt” is read and parsed to set up the MAC, IP, Subnet Mask, Gateway and the DNS with each value on it’s own line with a blank line at the end. So far that part is working fine.

But the server portion in the loop works just once and will not serve up another page unless I reset the Arduino.

Attached is my code. Forgive me if it looks a bit messy.

Please note that I’m using the 1.0.6 IDE. It’s the only one I could find that accepts sscanf as valid which I mentioned works fine.

#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') {
      // do 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 for incoming clients
  EthernetClient client = server.available();
  if (client) {
    Serial.println("new client");
    // an http request ends with a blank line
    boolean currentLineIsBlank = true;
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        Serial.write(c);
        // if you've gotten to the end of the line (received a newline
        // character) and the line is blank, the http request has ended,
        // so you can send a reply
        if (c == '\n' && currentLineIsBlank) {
          // send a standard http response header
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println("Connection: close");  // the connection will be closed after completion of the response
          client.println("Refresh: 5");  // refresh the page automatically every 5 sec
          client.println();
          client.println("<!DOCTYPE HTML>");
          client.println("<html>");
          // output the 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(" is ");
            client.print(sensorReading);
            client.println("
");
          }
          client.println("</html>");
          break;
        }
        if (c == '\n') {
          // you're starting a new line
          currentLineIsBlank = true;
        } else if (c != '\r') {
          // you've gotten a character on the current line
          currentLineIsBlank = false;
        }
      }
    }
    // give the web browser time to receive the data
    delay(1);
    // close the connection:
    client.stop();
    Serial.println("client disconnected");
    Serial.println("============================");
  }
}

Thank you for any help you can give me.

SP

Attached is the network.txt file I’m using.

Thank you again.

SP

network.txt (75 Bytes)

I redid the SD network settings a while back to work without sscanf.

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

byte myMac[6];
byte myIP[4];
byte myNM[4];
byte myGW[4];
byte myDNS[4];

void setup() {
  Serial.begin(115200); 

  pinMode(10,OUTPUT);
  digitalWrite(10,HIGH);

  delay(2000);
 
  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 == '\n') {
      chPos = 0;

      switch(lineNo) {
        case 0:
          if(getMAC(netBuffer,myMac)) Serial.println(F("mac ok"));
          break;

        case 1:
          if(getIP(netBuffer,myIP)) Serial.println(F("ip ok"));
          break;

        case 2:
          if(getIP(netBuffer,myNM)) Serial.println(F("netmask ok"));
          break;

        case 3:
          if(getIP(netBuffer,myGW)) Serial.println(F("gateway ok"));
          break;

        case 4:
          if(getIP(netBuffer,myDNS)) Serial.println(F("dns ok"));
          break;
      }

      lineNo++;
    }
    else if(ch == '\r') {
      // do 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);
 
  Serial.println(Ethernet.localIP());
}

void loop() {
}

byte getMAC(char* macBuf, byte* thisMAC) {
  byte thisLen = strlen(macBuf);
  byte thisOctet = 1;

  thisMAC[0] = strtol(&macBuf[0],NULL,16);
 
  for(int x = 0; x<thisLen; x++) {
      if(macBuf[x] == ':') {
        thisMAC[thisOctet] = strtol(&macBuf[x+1],NULL,16);
        thisOctet++;
      }
  }

  if(thisOctet == 6) return(1);
  else return(0);
 
}

byte getIP(char* ipBuf, byte* thisIP) {
  byte thisLen = strlen(ipBuf);
  byte thisOctet = 1;

  thisIP[0] = atoi(&ipBuf[0]);
 
  for(int x = 0; x<thisLen; x++) {
      if(ipBuf[x] == '.') {
        thisIP[thisOctet] = atoi(&ipBuf[x+1]);
        thisOctet++;
      }
  }

  if(thisOctet == 4) return(1);
  else return(0);
}

I use this server code now.
http://playground.arduino.cc/Code/WebServerST

Thank you SurferTim. The code you posted works perfectly. I added in the web server portion of my original code into the loop routine and the page reloads every time.

I am though, still at a loss as to why the original code didn't work. But at least I have some working code and can move forward with my project.

Thank you again!