Web Server Password and Form for IP change- client not available

Hi ,

I would like to join 2 codes, one for web server authentication with one for showing the network parameters in the browser in order to change them. Separately they work good, but when puting them together , the code does not change the IP. it's like after these statements the client is not available anymore:

if (client.available()) {

char c = client.read();
linebuf[charcount]=c;
if (charcount<sizeof(linebuf)-1) charcount++;
Serial.write(c);
....................................
-now the client is not available?

TextFinder finder(client );
if( finder.find("GET /") ) {

void loop()
{    
    ENET();
    SDWrite();  
  }


void ENET()
{     
   // listen for incoming clients
  EthernetClient client = server.available();
  if (client) {
 
    //Serial.println("new client");
    memset(linebuf,0,sizeof(linebuf));
    charcount=0;
    authentificated=false;
    // an http request ends with a blank line
    boolean currentLineIsBlank = true;
    while (client.connected()) {
      if (client.available()) {
      
      [color=red]  char c = client.read();
        linebuf[charcount]=c;
        if (charcount<sizeof(linebuf)-1) charcount++;
        Serial.write(c);[/color]
        // 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) {
          if (authentificated)
            SendOKpage(client);
          else
            SendAuthentificationpage(client);  
          break;
        }
        if (c == '\n') {
          // you're starting a new line
          currentLineIsBlank = true;
          if (strstr(linebuf,"Authorization: Basic")>0 && strstr(linebuf,"bWVnYTptZWdh")>0)
            authentificated=true;
          memset(linebuf,0,sizeof(linebuf));
          charcount=0;
        } 
        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);
    
  }
}
void SendOKpage(EthernetClient &client)
{      TextFinder  finder(client );
    if( finder.find("GET /") ) {
          Serial.println("connection has been made");
          if (finder.findUntil("SBM", "\n\r")){
            Serial.println();
            Serial.println("found a submitted form");
            SET = finder.getValue();
              while(finder.findUntil("DT", "\n\r")){
                int val = finder.getValue(); 
                if(val >= 0 && val <= 3) {
                  ip[val] = finder.getValue();
                }
                if(val >= 4 && val <= 7) {
                  subnet[val - 4] = finder.getValue();
                }
                if(val >= 8 && val <= 11) {
                  gateway[val - 8] = finder.getValue();
                }
              }
            
            ShieldValueWrite(); // writing all the values whitin the form into EEPROM
            EEPROM.write(ID_ADDR, 0x95); //set ID to the known bit, so when you reset the Arduino is will use the EEPROM values
           
          }
          }           
        client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println();
          client.print("<html><body><table><form><input type=\"hidden\" name=\"SBM\" value=\"1\">");
          client.print("<tr><td>IP: <input type=\"text\" size=\"1\" maxlength=\"3\" name=\"DT0\" value=\"");
          client.print(IP1,DEC);
          client.print("\">.<input type=\"text\" size=\"1\" maxlength=\"3\" name=\"DT1\" value=\"");
          client.print(IP2,DEC);
          client.print("\">.<input type=\"text\" size=\"1\" maxlength=\"3\" name=\"DT2\" value=\"");
          client.print(IP3,DEC);
          client.print("\">.<input type=\"text\" size=\"1\" maxlength=\"3\" name=\"DT3\" value=\"");
          client.print(IP4,DEC);
          client.print("\"></td></tr><tr><td>MASK: <input type=\"text\" size=\"1\" maxlength=\"3\" name=\"DT4\" value=\"");
          client.print(subnet[0],DEC);
          client.print("\">.<input type=\"text\" size=\"1\" maxlength=\"3\" name=\"DT5\" value=\"");
          client.print(subnet[1],DEC);
          client.print("\">.<input type=\"text\" size=\"1\" maxlength=\"3\" name=\"DT6\" value=\"");
          client.print(subnet[2],DEC);
          client.print("\">.<input type=\"text\" size=\"1\" maxlength=\"3\" name=\"DT7\" value=\"");
          client.print(subnet[3],DEC);
          client.print("\"></td></tr><tr><td>GW: <input type=\"text\" size=\"1\" maxlength=\"3\" name=\"DT8\" value=\"");
          client.print(gateway[0],DEC);
          client.print("\">.<input type=\"text\" size=\"1\" maxlength=\"3\" name=\"DT9\" value=\"");
          client.print(gateway[1],DEC);
          client.print("\">.<input type=\"text\" size=\"1\" maxlength=\"3\" name=\"DT10\" value=\"");
          client.print(gateway[2],DEC);
          client.print("\">.<input type=\"text\" size=\"1\" maxlength=\"3\" name=\"DT11\" value=\"");
          client.print(gateway[3],DEC);
          client.print("\"></td></tr><tr><td><input type=\"submit\" value=\"SUBMIT\"></td></tr>");
          client.print("</form></table></body></html>");
  
   /*       // send a standard http response header
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
         // client.println("Connnection: close");
          client.println();
          client.println("<!DOCTYPE HTML>");
          client.println("<html>");
                     // add a meta refresh tag, so the browser pulls again every 5 seconds:
          client.println("<meta http-equiv=\"refresh\" content=\"5\">");
 
          
  
          client.println("</html>");
     
}

void SendAuthentificationpage(EthernetClient &client)
{
          client.println("HTTP/1.1 401 Authorization Required");
          client.println("WWW-Authenticate: Basic realm=\"Secure Area\"");
          client.println("Content-Type: text/html");
          client.println("Connnection: close");
          client.println();
          client.println("<!DOCTYPE HTML>");
          client.println("<HTML>  <HEAD>   <TITLE>Error</TITLE>");
          client.println(" </HEAD> <BODY><H1>401 Unauthorized.</H1></BODY> </HTML>");
}

void ShieldSetup()
{
  int idcheck = EEPROM.read(ID_ADDR);
  Serial.println(idcheck,DEC);
  if (idcheck != ID){
    //ifcheck id is not the value as const byte ID,
    //it means this sketch has NOT been used to setup the shield before
    Ethernet.begin(mac, ip, gateway, subnet); //just use the values written in the beginning of the sketch
    Serial.println("first time sketch has run");
  }
  if (idcheck == ID){
    //if id is the same value as const byte ID,
    //it means this sketch has been used to setup the shield.
    //So we will read the values out of EERPOM ans use them
    //to setup the shield.
    IP1 = EEPROM.read(IP_ADDR1);
    IP2 = EEPROM.read(IP_ADDR2);
    IP3 = EEPROM.read(IP_ADDR3);
    IP4 = EEPROM.read(IP_ADDR4);
    GW1 = EEPROM.read(GW_ADDR1);
    GW2 = EEPROM.read(GW_ADDR2);
    GW3 = EEPROM.read(GW_ADDR3);
    GW4 = EEPROM.read(GW_ADDR4);
    SUB1 = EEPROM.read(SUB_ADDR1);
    SUB2 = EEPROM.read(SUB_ADDR2);
    SUB3 = EEPROM.read(SUB_ADDR3);
    SUB4 = EEPROM.read(SUB_ADDR4);
    byte ip[] = {IP1,IP2,IP3,IP4};
    byte gateway[] = {GW1,GW2,GW3,GW4};
    byte subnet[] = {SUB1,SUB2,SUB3,SUB4};
    Ethernet.begin(mac, ip, gateway, subnet);
    Serial.println("NOT the first time sketch has run");
  }
}


void ShieldValueWrite()
{
  Serial.println("writing to EEPROM");
  for (int i = 0 ; i < 4; i++)
  {
    EEPROM.write(ip_addr[i], ip[i]);
  }
  for (int i = 0 ; i < 4; i++)
  {
    EEPROM.write(gw_addr[i], gateway[i]);
  }
  for (int i = 0 ; i < 4; i++)
  {
    EEPROM.write(sub_addr[i], subnet[i]);
  }  
}

post full code

I splitt it in 2

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

#include <TextFinder.h>
#include <EEPROM.h>
#include <avr/wdt.h>

*******************/ 
//int Sensor1Data=0;
const int chipSelect = 4;

byte IP1 = 192;//the first part of the IP-address
byte IP2 = 168; //the second part of the IP-address
byte IP3 = 14; //the thirth part of the IP-address
byte IP4 = 88; //the fourth part of the IP-address
byte GW1 = 192; //the first part of the GATEWAY
byte GW2 = 168; //the second part of the GATEWAY
byte GW3 = 14; //the thirth part of the GATEWAY
byte GW4 = 2; //the fourth part of the GATEWAY
byte SUB1 = 255; //first part of the SUBNETMASK
byte SUB2 = 255; //the second part of the SUBNETMASK
byte SUB3 = 255; //the thirth part of the SUBNETMASK
byte SUB4 = 0; //the fourth part of the SUBNETMASK
byte SET = 0;

//seting up the EthernetShield
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = {IP1,IP2,IP3,IP4};
byte gateway[] = {GW1,GW2,GW3,GW4};
byte subnet[] = {SUB1,SUB2,SUB3,SUB4};
EthernetServer server(80);//server port

const byte ID = 0x95; //used to identify if valid data in EEPROM the "know" bit, 
// if this is written in EEPROM the sketch has ran before
// We use this, so that the very first time you'll run this sketch it will use
// the values written above. 
// defining which EEPROM address we are using for what data
const byte ID_ADDR = 0; //the EEPROM adress used to store the ID (to check if sketch already has ran)
const byte IP_ADDR1 = 1; //the EEPROM adress used to store the first part of the IP-address
const byte IP_ADDR2 = 2; //the EEPROM adress used to store the second part of the IP-address
const byte IP_ADDR3 = 3; //the EEPROM adress used to store the thirth part of the IP-address
const byte IP_ADDR4 = 4; //the EEPROM adress used to store the fourth part of the IP-address
const byte GW_ADDR1 = 5; //the EEPROM adress used to store the first part of the GATEWAY
const byte GW_ADDR2 = 6; //the EEPROM adress used to store the second part of the GATEWAY
const byte GW_ADDR3 = 7; //the EEPROM adress used to store the thirth part of the GATEWAY
const byte GW_ADDR4 = 8; //the EEPROM adress used to store the fourth part of the GATEWAY
const byte SUB_ADDR1 = 9; //the EEPROM adress used to store the first part of the SUBNETMASK
const byte SUB_ADDR2 = 10; //the EEPROM adress used to store the second part of the SUBNETMASK
const byte SUB_ADDR3 = 11; //the EEPROM adress used to store the thirth part of the SUBNETMASK
const byte SUB_ADDR4 = 12; //the EEPROM adress used to store the fourth part of the SUBNETMASK
byte ip_addr[] = { IP_ADDR1, IP_ADDR2, IP_ADDR3, IP_ADDR4};
byte gw_addr[] = { GW_ADDR1, GW_ADDR2, GW_ADDR3, GW_ADDR4};
byte sub_addr[] = { SUB_ADDR1, SUB_ADDR2, SUB_ADDR3, SUB_ADDR4};

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

      
      Serial.print("Starting SD..");
      if(!SD.begin(4)) Serial.println("failed");
     else Serial.println("ok");
  
     ShieldSetup (); //Setup the Ethernet shield
     server.begin();
    
      
}

char linebuf[80];
int charcount=0;
boolean authentificated=false;

void loop()
{  
    ENET();
   
  }

void ENET()
{     
   // listen for incoming clients
  EthernetClient client = server.available();
  if (client) {
 
    //Serial.println("new client");
    memset(linebuf,0,sizeof(linebuf));
    charcount=0;
    authentificated=false;
    // an http request ends with a blank line
    boolean currentLineIsBlank = true;
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        linebuf[charcount]=c;
        if (charcount<sizeof(linebuf)-1) charcount++;
        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) {
          if (authentificated)
            SendOKpage(client);
          else
            SendAuthentificationpage(client);  
          break;
        }
        if (c == '\n') {
          // you're starting a new line
          currentLineIsBlank = true;
          if (strstr(linebuf,"Authorization: Basic")>0 && strstr(linebuf,"bWVnYTptZWdh")>0)
            authentificated=true;
          memset(linebuf,0,sizeof(linebuf));
          charcount=0;
        } 
        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 disonnected");
  }
}

And the cycle does not run into this "if"
if( finder.find("GET /") ) {

void SendOKpage(EthernetClient &client)
{      TextFinder  finder(client );
    if( finder.find("GET /") ) {
          Serial.println("connection has been made");
          if (finder.findUntil("SBM", "\n\r")){
            Serial.println();
            Serial.println("found a submitted form");
            SET = finder.getValue();
              while(finder.findUntil("DT", "\n\r")){
                int val = finder.getValue(); 
                if(val >= 0 && val <= 3) {
                  ip[val] = finder.getValue();
                }
                if(val >= 4 && val <= 7) {
                  subnet[val - 4] = finder.getValue();
                }
                if(val >= 8 && val <= 11) {
                  gateway[val - 8] = finder.getValue();
                }
              }
            Serial.print("The new IP address is:");
            Serial.print(ip[0],DEC);
            for (int i= 1; i < 4; i++){
              Serial.print(".");
              Serial.print(ip[i],DEC);
            }
            Serial.println();
            Serial.print("The new Subnet is:");
            Serial.print(subnet[0],DEC);
            for (int i= 1; i < 4; i++){
              Serial.print(".");
              Serial.print(subnet[i],DEC);
            }
            Serial.println();
            Serial.print("The new gateway is:");
            Serial.print(gateway[0],DEC);
            for (int i= 1; i < 4; i++){
              Serial.print(".");
              Serial.print(gateway[i],DEC);
            }
            Serial.println();
            ShieldValueWrite(); // writing all the values whitin the form into EEPROM
            EEPROM.write(ID_ADDR, 0x95); //set ID to the known bit, so when you reset the Arduino is will use the EEPROM values
            Serial.println("all data has been written to EEPROM");
            Serial.println("you can now reset the Arduino");
            Serial.println("and use the new ip in your browser");
            //
            // if al the data has been written to EEPROM we should reset the arduino
            // for now you'll have to use the hardware reset button
          }
          }           
        client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println();
          client.print("<html><body><table><form><input type=\"hidden\" name=\"SBM\" value=\"1\">");
          client.print("<tr><td>IP: <input type=\"text\" size=\"1\" maxlength=\"3\" name=\"DT0\" value=\"");
          client.print(IP1,DEC);
          client.print("\">.<input type=\"text\" size=\"1\" maxlength=\"3\" name=\"DT1\" value=\"");
          client.print(IP2,DEC);
          client.print("\">.<input type=\"text\" size=\"1\" maxlength=\"3\" name=\"DT2\" value=\"");
          client.print(IP3,DEC);
          client.print("\">.<input type=\"text\" size=\"1\" maxlength=\"3\" name=\"DT3\" value=\"");
          client.print(IP4,DEC);
          client.print("\"></td></tr><tr><td>MASK: <input type=\"text\" size=\"1\" maxlength=\"3\" name=\"DT4\" value=\"");
          client.print(subnet[0],DEC);
          client.print("\">.<input type=\"text\" size=\"1\" maxlength=\"3\" name=\"DT5\" value=\"");
          client.print(subnet[1],DEC);
          client.print("\">.<input type=\"text\" size=\"1\" maxlength=\"3\" name=\"DT6\" value=\"");
          client.print(subnet[2],DEC);
          client.print("\">.<input type=\"text\" size=\"1\" maxlength=\"3\" name=\"DT7\" value=\"");
          client.print(subnet[3],DEC);
          client.print("\"></td></tr><tr><td>GW: <input type=\"text\" size=\"1\" maxlength=\"3\" name=\"DT8\" value=\"");
          client.print(gateway[0],DEC);
          client.print("\">.<input type=\"text\" size=\"1\" maxlength=\"3\" name=\"DT9\" value=\"");
          client.print(gateway[1],DEC);
          client.print("\">.<input type=\"text\" size=\"1\" maxlength=\"3\" name=\"DT10\" value=\"");
          client.print(gateway[2],DEC);
          client.print("\">.<input type=\"text\" size=\"1\" maxlength=\"3\" name=\"DT11\" value=\"");
          client.print(gateway[3],DEC);
          client.print("\"></td></tr><tr><td><input type=\"submit\" value=\"SUBMIT\"></td></tr>");
          client.print("</form></table></body></html>");
  
   /*       // send a standard http response header
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
         // client.println("Connnection: close");
          client.println();
          client.println("<!DOCTYPE HTML>");
          client.println("<html>");
                     // add a meta refresh tag, so the browser pulls again every 5 seconds:
          client.println("<meta http-equiv=\"refresh\" content=\"5\">");
 
          */
        
          client.println("</html>");
     
}

void SendAuthentificationpage(EthernetClient &client)
{
          client.println("HTTP/1.1 401 Authorization Required");
          client.println("WWW-Authenticate: Basic realm=\"Secure Area\"");
          client.println("Content-Type: text/html");
          client.println("Connnection: close");
          client.println();
          client.println("<!DOCTYPE HTML>");
          client.println("<HTML>  <HEAD>   <TITLE>Error</TITLE>");
          client.println(" </HEAD> <BODY><H1>401 Unauthorized.</H1></BODY> </HTML>");
}

void ShieldSetup()
{
  int idcheck = EEPROM.read(ID_ADDR);
  Serial.println(idcheck,DEC);
  if (idcheck != ID){
    //ifcheck id is not the value as const byte ID,
    //it means this sketch has NOT been used to setup the shield before
    Ethernet.begin(mac, ip, gateway, subnet); //just use the values written in the beginning of the sketch
    Serial.println("first time sketch has run");
  }
  if (idcheck == ID){
    //if id is the same value as const byte ID,
    //it means this sketch has been used to setup the shield.
    //So we will read the values out of EERPOM ans use them
    //to setup the shield.
    IP1 = EEPROM.read(IP_ADDR1);
    IP2 = EEPROM.read(IP_ADDR2);
    IP3 = EEPROM.read(IP_ADDR3);
    IP4 = EEPROM.read(IP_ADDR4);
    GW1 = EEPROM.read(GW_ADDR1);
    GW2 = EEPROM.read(GW_ADDR2);
    GW3 = EEPROM.read(GW_ADDR3);
    GW4 = EEPROM.read(GW_ADDR4);
    SUB1 = EEPROM.read(SUB_ADDR1);
    SUB2 = EEPROM.read(SUB_ADDR2);
    SUB3 = EEPROM.read(SUB_ADDR3);
    SUB4 = EEPROM.read(SUB_ADDR4);
    byte ip[] = {IP1,IP2,IP3,IP4};
    byte gateway[] = {GW1,GW2,GW3,GW4};
    byte subnet[] = {SUB1,SUB2,SUB3,SUB4};
    Ethernet.begin(mac, ip, gateway, subnet);
    Serial.println("NOT the first time sketch has run");
  }
}


void ShieldValueWrite()
{
  Serial.println("writing to EEPROM");
  for (int i = 0 ; i < 4; i++)
  {
    EEPROM.write(ip_addr[i], ip[i]);
  }
  for (int i = 0 ; i < 4; i++)
  {
    EEPROM.write(gw_addr[i], gateway[i]);
  }
  for (int i = 0 ; i < 4; i++)
  {
    EEPROM.write(sub_addr[i], subnet[i]);
  }  
}

Once you get the blank line the HTTP header is no longer in the stream, how do you expect to find the GET ?

You should put all your EEPROM stuff in a structure and read/write it in one go. will save tons of lines of code

J-M-L:
Once you get the blank line the HTTP header is no longer in the stream, how do you expect to find the GET ?

Thank you, I understand now, but, still, if I put all the "finder" stuff before :

char c = client.read();
linebuf[charcount]=c;
if (charcount<sizeof(linebuf)-1) charcount++;
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

It will not go to autentification anymore.

J-M-L:
You should put all your EEPROM stuff in a structure and read/write it in one go. will save tons of lines of code

That's something I don't know yet to do

You should capture the GET request at the same time you listen to what’s coming