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]);
}
}