How to change size of readstring() in client.read() on Arduino Ethernet?

The strstr() function returns a pointer, not an integer. When you get over expecting strstr() to return an integer, you'll be happier.

I need characters from an array to be found & printed. not integers.

Thats the final conclusion...
too many strstr is getting you a headache. Is this something of the chips memory or something?

if(strstr(tBuf,"off") != NULL) {
       loggedin=0;
 digitalWrite(9, LOW);    // set pin 4 high
      Serial.println("Led Off!");

}

    if(strstr(tBuf,"admin") == NULL  || strstr(tBuf,"pass") == NULL ) {

      loggedin=0;
      Serial.println("Error: Wrong username or password...");    
    }


    if(strstr(tBuf,"admin") != NULL  && strstr(tBuf,"pass") != NULL )//Successful login
    {
      loggedin=1;
      digitalWrite(9, HIGH);    // set pin 4 high
      Serial.println("Login Successful!");         
    }

If I remove the code below everything works....

"if(strstr(tBuf,"off") != NULL) {
       loggedin=0;
 digitalWrite(9, LOW);    // set pin 4 high
      Serial.println("Led Off!");

}"

But when I add it to this one it needs to be reseted. it stops in the first loop.it doesn't do anything else.arduino freezes none of the serials print on monitor....

  if(strstr(tBuf,"admin") == NULL  || strstr(tBuf,"pass") == NULL ) {

      loggedin=0;
      Serial.println("Error: Wrong username or password...");    
    }


    if(strstr(tBuf,"admin") != NULL  && strstr(tBuf,"pass") != NULL )//Successful login
    {
      loggedin=1;
      digitalWrite(9, HIGH);    // set pin 4 high
      Serial.println("Login Successful!");         
    }

Why don't you just look for what must be there to authenticate. I would use logic like this:

char tBuf[64] = "GET /?user=admin&pwd=mypasswd&submit=Log+In HTTP/1.1";

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

  if(strstr(tBuf,"user=admin") != NULL && strstr(tBuf,"pwd=mypasswd") !=NULL) {
    Serial.println("Logged in");
    // do the rest of your stuff here
  }
  else Serial.println("Login failed");  
}

void loop() {
  
}

I tried to find the readstring function to modify its size but it just says "size" in the Ethernetclient.cpp file. How can I set it? Where is it stored? Does anybody know?

In your origional posted code, I don't see any attempt to do some cleanup like below. Did you inclued this?

          //clearing string for next read
          readString="";

Also, I think there is a limit to the number of characters in a "get" request, maybe something like 128. Are you exceeding this number?

        if (readString.length() < 150) {

All i'm doing is this...

char tBuf[64];
  void loop()
{

  Serial.println("11");   
  EthernetClient client = server.available();
  if(client) {
    Serial.println("12");   
    boolean currentLineIsBlank = true;
    boolean currentLineIsGet = true;
    int tCount = 0;
    char tBuf[64];
   

    Serial.print("Client request: ");

    while (client.connected()) {
      Serial.println("13");   
      while(client.available()) {
        Serial.println("14");   
        char c = client.read();

        if(currentLineIsGet && tCount < 63)
        {

          tBuf[tCount] = c;
          tCount++;
          tBuf[tCount] = 0;          
        }


        if (c == '\n' && currentLineIsBlank) {
          // send a standard http response
          .....CODE
}
}
}
}

With 2x if (strstr...) statements it works ok, but if I add another one it stacks.
It only prints "11" once.

Try this: I used user and pwd because they are shorter, and password is a common packet sniffer target.

if (c == '\n' && currentLineIsBlank) {
  if(strstr(tBuf,"user=admin") != NULL && strstr(tBuf,"pwd=mypasswd") !=NULL) {
    Serial.println("Logged in");
    // do the rest of your stuff here
  }
  else Serial.println("Login failed");  
}

It just freezes and needs to be reseted every time..
Is there another command so I can check for "user" in an char array?
Or is something wrong with my "if strstr" ??

Memory check required. Maybe you are running out of SRAM. Add this function to your sketch.

int freeRam() {
  extern int __heap_start,*__brkval;
  int v;
  return (int)&v - (__brkval == 0 ? (int)&__heap_start : (int) __brkval);  
}

At the end of your setup function, and anywhere you want to check available SRAM, like just before where the sketch fails, add this:

Serial.print("SRAM free: ");
Serial.println(freeRam());

Thats my code...

Whats wrong? Just try to upload it, test it via serial monitor. You don;t need ethernet.It stacks from start.

Then remove the strstr commands near the bottom and it all works!!!

NON-WORKING CODE

/* 
 Web server sketch for IDE v1.0.1 and w5100/w5200
 Posted October 2012 by SurferTim
 */

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

byte mac[] = { 
  0x00, 0x00, 0xAA, 0xBB, 0xCC, 0xDD }; //physical mac address
byte ip[] = { 
  192, 168, 10, 110 }; // ip in lan
byte gateway[] = { 
  192, 168, 10, 254 }; // internet access via router
byte subnet[] = { 
  255, 255, 255, 0 }; //subnet mask
EthernetServer server(8070);

char user[ ] = "admin"; // YOUR Username for Login
char pwd[ ] = "123abc"; // YOUR Password for Login
int loggedin = 0;
//////////////////////

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

  // disable w5100 while setting up SD
  // uncomment next 5 lines if using a microSD card

  //  pinMode(10,OUTPUT);
  //  digitalWrite(10,HIGH);
  // Serial.print("Starting SD..");
  // if(!SD.begin(4)) Serial.println("failed");
  // else Serial.println("ok");

  Ethernet.begin(mac, ip, gateway, gateway, subnet);

  delay(2000);
  server.begin();
  Serial.println("Ready");
}

void loop()
{

  Serial.println("11");   
  EthernetClient client = server.available();
  if(client) {
    Serial.println("12");   
    boolean currentLineIsBlank = true;
    boolean currentLineIsGet = true;
    int tCount = 0;
    char tBuf[64];
    
    Serial.print("Client request: ");

    while (client.connected()) {
      Serial.println("13");   
      while(client.available()) {
        Serial.println("14");   
        char c = client.read();

        if(currentLineIsGet && tCount < 63)
        {

          tBuf[tCount] = c;
          tCount++;
          tBuf[tCount] = 0;          
        }


        if (c == '\n' && currentLineIsBlank) {
          // send a standard http response
          Serial.println(tBuf);
          Serial.print("POST data: ");
          while(client.available()) Serial.write(client.read());
          Serial.println();



          Serial.println("Sending response");
          client.write("HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n<html>");

          client.write("<head><script type=\"text/javascript\">");
          client.write("function show_alert() {alert(\"This is an alert\");}");
          client.write("</script>");
          client.write("<TITLE>NaThAN Home Server Login Page</TITLE>");
          client.write("  </head");


          client.write("<body>");

          client.println("<CENTER>");
          client.println("<img src=\"https://dl.dropbox.com/u/28103209/HomeServer_files/login_nathan_homeserver.gif\"  width=\"460\" height=\"36\" >");
          client.println("</CENTER>");

          client.println("<CENTER>");

          client.println("<text STYLE=\"position:relative; TOP:50px; LEFT:50px;  WIDTH:50px; HEIGHT:50px\">");
          client.println("<FORM ACTION=\"http://nathanas.dyndns.info:8070\" method=get >");  //SET THE DOMAIN
          client.println("Username: <INPUT TYPE=TEXT NAME=\"username\" VALUE=\"\" SIZE=\"25\" MAXLENGTH=\"50\">
");
          client.println("Password: <INPUT TYPE=TEXT NAME=\"password\" VALUE=\"\" SIZE=\"25\" MAXLENGTH=\"50\">
");
          client.println("<text STYLE=\"position:relative; RIGHT:33px; TOP:10px; \">  ");
          client.println("<INPUT TYPE=SUBMIT NAME=\"submit\" VALUE=\"Log In\">");
          client.println("</FORM>");
          client.println("</CENTER>");



          client.write("</body></html>\r\n\r\n");
          client.stop();
        }
        else if (c == '\n') {
          currentLineIsBlank = true;
          currentLineIsGet = false;
        } 
        else if (c != '\r') {
          currentLineIsBlank = false;
        }
      }
    }

    Serial.println("done");


   
if(strstr(tBuf,"off") != NULL) {
       loggedin=0;
 digitalWrite(9, LOW);    // set pin 4 high
      Serial.println("Led Off!");

}

    if(strstr(tBuf,user) == NULL  || strstr(tBuf,pwd) == NULL ) {

      loggedin=0;
      Serial.println("Error: Wrong username or password...");    
    }


    if(strstr(tBuf,user) != NULL  && strstr(tBuf,pwd) != NULL )//Successful login
    {
      loggedin=1;
      digitalWrite(9, HIGH);    // set pin 4 high
      Serial.println("Login Successful!");         
    }






  }



}

WORKING CODE:

/* 
 Web server sketch for IDE v1.0.1 and w5100/w5200
 Posted October 2012 by SurferTim
 */

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

byte mac[] = { 
  0x00, 0x00, 0xAA, 0xBB, 0xCC, 0xDD }; //physical mac address
byte ip[] = { 
  192, 168, 10, 110 }; // ip in lan
byte gateway[] = { 
  192, 168, 10, 254 }; // internet access via router
byte subnet[] = { 
  255, 255, 255, 0 }; //subnet mask
EthernetServer server(8070);

char user[ ] = "admin"; // YOUR Username for Login
char pwd[ ] = "123abc"; // YOUR Password for Login
int loggedin = 0;
//////////////////////

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

  // disable w5100 while setting up SD
  // uncomment next 5 lines if using a microSD card

  //  pinMode(10,OUTPUT);
  //  digitalWrite(10,HIGH);
  // Serial.print("Starting SD..");
  // if(!SD.begin(4)) Serial.println("failed");
  // else Serial.println("ok");

  Ethernet.begin(mac, ip, gateway, gateway, subnet);

  delay(2000);
  server.begin();
  Serial.println("Ready");
}

void loop()
{

  Serial.println("11");   
  EthernetClient client = server.available();
  if(client) {
    Serial.println("12");   
    boolean currentLineIsBlank = true;
    boolean currentLineIsGet = true;
    int tCount = 0;
    char tBuf[64];
    
    Serial.print("Client request: ");

    while (client.connected()) {
      Serial.println("13");   
      while(client.available()) {
        Serial.println("14");   
        char c = client.read();

        if(currentLineIsGet && tCount < 63)
        {

          tBuf[tCount] = c;
          tCount++;
          tBuf[tCount] = 0;          
        }


        if (c == '\n' && currentLineIsBlank) {
          // send a standard http response
          Serial.println(tBuf);
          Serial.print("POST data: ");
          while(client.available()) Serial.write(client.read());
          Serial.println();



          Serial.println("Sending response");
          client.write("HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n<html>");

          client.write("<head><script type=\"text/javascript\">");
          client.write("function show_alert() {alert(\"This is an alert\");}");
          client.write("</script>");
          client.write("<TITLE>NaThAN Home Server Login Page</TITLE>");
          client.write("  </head");


          client.write("<body>");

          client.println("<CENTER>");
          client.println("<img src=\"https://dl.dropbox.com/u/28103209/HomeServer_files/login_nathan_homeserver.gif\"  width=\"460\" height=\"36\" >");
          client.println("</CENTER>");

          client.println("<CENTER>");

          client.println("<text STYLE=\"position:relative; TOP:50px; LEFT:50px;  WIDTH:50px; HEIGHT:50px\">");
          client.println("<FORM ACTION=\"http://nathanas.dyndns.info:8070\" method=get >");  //SET THE DOMAIN
          client.println("Username: <INPUT TYPE=TEXT NAME=\"username\" VALUE=\"\" SIZE=\"25\" MAXLENGTH=\"50\">
");
          client.println("Password: <INPUT TYPE=TEXT NAME=\"password\" VALUE=\"\" SIZE=\"25\" MAXLENGTH=\"50\">
");
          client.println("<text STYLE=\"position:relative; RIGHT:33px; TOP:10px; \">  ");
          client.println("<INPUT TYPE=SUBMIT NAME=\"submit\" VALUE=\"Log In\">");
          client.println("</FORM>");
          client.println("</CENTER>");



          client.write("</body></html>\r\n\r\n");
          client.stop();
        }
        else if (c == '\n') {
          currentLineIsBlank = true;
          currentLineIsGet = false;
        } 
        else if (c != '\r') {
          currentLineIsBlank = false;
        }
      }
    }

    Serial.println("done");


   



    if(strstr(tBuf,user) != NULL  && strstr(tBuf,pwd) != NULL )//Successful login
    {
      loggedin=1;
      digitalWrite(9, HIGH);    // set pin 4 high
      Serial.println("Login Successful!");         
    }






  }



}

With the working code i get "SRAM free: 58585858585858585" when we enter loop for the first time
if i remove these lines from the working code the available sram is 1996. So its something wrong with my "strstr"
The code i remove is:

  if(strstr(tBuf,user) != NULL  && strstr(tBuf,pwd) != NULL )//Successful login
    {
      loggedin=1;
      digitalWrite(9, HIGH);    // set pin 4 high
      Serial.println("Login Successful!");         
    }

This is a symptom of no SRAM remaining:

It just freezes and needs to be reseted every time..

I don't see the freeRam function in either one of those sketches. You could be running out of SRAM. The Arduino Ethernet has only 2K SRAM. With the ethernet stuff, and a lot of strings, it can disappear fast. :frowning:

I have the sram function tested but the code doesn;t contain it in the post.

OK. Just to insure it is working on your board, try this test sketch:

int freeRam() {
  extern int __heap_start,*__brkval;
  int v;
  return (int)&v - (__brkval == 0 ? (int)&__heap_start : (int) __brkval);  
}

void setup() {
  Serial.begin(9600);
  Serial.print("SRAM free: ");
  Serial.println(freeRam());
}

void loop() {

}

It should be somewhere around 1400 or so on your device. As long as I have SRAM remaining, the output is accurate. I have not run out of memory yet. I have a Mega2560/Ethernet shield.

the sram function is working.
Please see again my previous post with the 3 codes (Working & nonworking).
there i explain a lot.
the problem is the strstr command as i describe there.
I edited that previous post, that's why i kindly ask you to take a look again.

I just compiled your code on my Mega. It has 8K SRAM. With minimal code (the test I posted), it shows 7459 bytes free. With your code, it shows 5747 bytes free. That would be using more SRAM than your device has.

Try this one and check the sram:

/* 
 Web server sketch for IDE v1.0.1 and w5100/w5200
 Posted October 2012 by SurferTim
 */

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

byte mac[] = { 
  0x00, 0x00, 0xAA, 0xBB, 0xCC, 0xDD }; //physical mac address
byte ip[] = { 
  192, 168, 10, 110 }; // ip in lan
byte gateway[] = { 
  192, 168, 10, 254 }; // internet access via router
byte subnet[] = { 
  255, 255, 255, 0 }; //subnet mask
EthernetServer server(8070);

char user[ ] = "admin"; // YOUR Username for Login
char pwd[ ] = "123abc"; // YOUR Password for Login
int loggedin = 0;
//////////////////////

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

  // disable w5100 while setting up SD
  // uncomment next 5 lines if using a microSD card

  //  pinMode(10,OUTPUT);
  //  digitalWrite(10,HIGH);
  // Serial.print("Starting SD..");
  // if(!SD.begin(4)) Serial.println("failed");
  // else Serial.println("ok");

  Ethernet.begin(mac, ip, gateway, gateway, subnet);

  delay(2000);
  server.begin();
  Serial.println("Ready");
}

void loop()
{

  Serial.println("11");   
  EthernetClient client = server.available();
  if(client) {
    Serial.println("12");   
    boolean currentLineIsBlank = true;
    boolean currentLineIsGet = true;
    int tCount = 0;
    char tBuf[64];
    
    Serial.print("Client request: ");

    while (client.connected()) {
      Serial.println("13");   
      while(client.available()) {
        Serial.println("14");   
        char c = client.read();

        if(currentLineIsGet && tCount < 63)
        {

          tBuf[tCount] = c;
          tCount++;
          tBuf[tCount] = 0;          
        }


        if (c == '\n' && currentLineIsBlank) {
          // send a standard http response
          Serial.println(tBuf);
          Serial.print("POST data: ");
          while(client.available()) Serial.write(client.read());
          Serial.println();



          Serial.println("Sending response");
          client.write("HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n<html>");

          client.write("<head><script type=\"text/javascript\">");
          client.write("function show_alert() {alert(\"This is an alert\");}");
          client.write("</script>");
          client.write("<TITLE>NaThAN Home Server Login Page</TITLE>");
          client.write("  </head");


          client.write("<body>");

          client.println("<CENTER>");
          client.println("<img src=\"https://dl.dropbox.com/u/28103209/HomeServer_files/login_nathan_homeserver.gif\"  width=\"460\" height=\"36\" >");
          client.println("</CENTER>");

          client.println("<CENTER>");

          client.println("<text STYLE=\"position:relative; TOP:50px; LEFT:50px;  WIDTH:50px; HEIGHT:50px\">");
          client.println("<FORM ACTION=\"http://nathanas.dyndns.info:8070\" method=get >");  //SET THE DOMAIN
          client.println("Username: <INPUT TYPE=TEXT NAME=\"username\" VALUE=\"\" SIZE=\"25\" MAXLENGTH=\"50\">
");
          client.println("Password: <INPUT TYPE=TEXT NAME=\"password\" VALUE=\"\" SIZE=\"25\" MAXLENGTH=\"50\">
");
          client.println("<text STYLE=\"position:relative; RIGHT:33px; TOP:10px; \">  ");
          client.println("<INPUT TYPE=SUBMIT NAME=\"submit\" VALUE=\"Log In\">");
          client.println("</FORM>");
          client.println("</CENTER>");



          client.write("</body></html>\r\n\r\n");
          client.stop();
        }
        else if (c == '\n') {
          currentLineIsBlank = true;
          currentLineIsGet = false;
        } 
        else if (c != '\r') {
          currentLineIsBlank = false;
        }
      }
    }

    Serial.println("done");


   



    if(strstr(tBuf,user) != NULL  && strstr(tBuf,pwd) != NULL )//Successful login
    {
      loggedin=1;
      digitalWrite(9, HIGH);    // set pin 4 high
      Serial.println("Login Successful!");         
    }






  }



}

Then try removing this from the above and then check the sram again...isn't that odd?

if(strstr(tBuf,user) != NULL  && strstr(tBuf,pwd) != NULL )//Successful login
    {
      loggedin=1;
      digitalWrite(9, HIGH);    // set pin 4 high
      Serial.println("Login Successful!");         
    }

I was using your working code. When I put that statement in, the SRAM goes down to 5709.

I have to try it differently...:frowning:
Do you know how to search for a word in a char array and get something if the word contains "example"???

nathanas:
Do you know how to search for a word in a char array and get something if the word contains "example"???

You couldn't figure that out from my examples above?

if(strstr(tBuf,"example") != NULL) {
  // example exists
}

Try using the F() function with your strings. That will save you some SRAM. I used it on just your response to the client, and it appears to get you under the limit. :slight_smile:

Give me a minute to switch to my Ubuntu box. I'm typing on one, and programming on the other. I'll post an example.

edit: Here is an example, and it got me over 6000 bytes free.

          client.println(F("<text STYLE=\"position:relative; TOP:50px; LEFT:50px;  WIDTH:50px; HEIGHT:50px\">"));
          client.println(F("<FORM ACTION=\"http://nathanas.dyndns.info:8070\" method=get >"));  //SET THE DOMAIN
          client.println(F("Username: <INPUT TYPE=TEXT NAME=\"username\" VALUE=\"\" SIZE=\"25\" MAXLENGTH=\"50\">
"));
          client.println(F("Password: <INPUT TYPE=TEXT NAME=\"password\" VALUE=\"\" SIZE=\"25\" MAXLENGTH=\"50\">
"));
          client.println(F("<text STYLE=\"position:relative; RIGHT:33px; TOP:10px; \">  "));
          client.println(F("<INPUT TYPE=SUBMIT NAME=\"submit\" VALUE=\"Log In\">"));
          client.println(F("</FORM>"));
          client.println(F("</CENTER>"));

Do that to the rest, and you should be good to go!

I mean another command which is lighter than

if(strstr(tBuf,"example") != NULL) {
  // example exists
}

As for the F() function...I haven't seen any like that. What does the F do?