Go Down

Topic: How to change size of readstring() in client.read() on Arduino Ethernet? (Read 3488 times) previous topic - next topic

PaulS

Code: [Select]
    if(strstr(tBuf,username) == 0  || strstr(tBuf,password) == 0 ) {
    if(strstr(tBuf,username) >0  && strstr(tBuf,password) >0 )//Successful login

Why are you back to expecting strstr() to return a numeric value?

nathanas

i have set username as character
Code: [Select]
char username[]="admin";
char password[]="pass";

if(strstr(tBuf,username) != NULL  && strstr(tBuf,password) != NULL )//Successful login  DOENS'T WORK :(


if(strstr(tBuf,"admin") != NULL  && strstr(tBuf,"pass") != NULL )//Successful login   WORKS
:)

maybe I need to save it as another operator. not char

PaulS

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

nathanas

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?
Code: [Select]
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....
Code: [Select]
"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....
Code: [Select]
 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!");        
   }

SurferTim

Why don't you just look for what must be there to authenticate. I would use logic like this:
Code: [Select]
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() {
 
}

zoomkat

Quote
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?

Code: [Select]

         //clearing string for next read
         readString="";
Google forum search: Use Google Advanced Search and use Http://forum.arduino.cc/index in the "site or domain:" box.

zoomkat

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?

Code: [Select]
        if (readString.length() < 150) {
Google forum search: Use Google Advanced Search and use Http://forum.arduino.cc/index in the "site or domain:" box.

nathanas

All i'm doing is this...

Code: [Select]
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.

SurferTim

Try this: I used user and pwd because they are shorter, and password is a common packet sniffer target.
Code: [Select]
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"); 
}


nathanas

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" ??

SurferTim

Memory check required. Maybe you are running out of SRAM. Add this function to your sketch.
Code: [Select]
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:
Code: [Select]
Serial.print("SRAM free: ");
Serial.println(freeRam());


nathanas

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
Code: [Select]
/*
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\"><BR>");
         client.println("Password: <INPUT TYPE=TEXT NAME=\"password\" VALUE=\"\" SIZE=\"25\" MAXLENGTH=\"50\"><BR>");
         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:
Code: [Select]
/*
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\"><BR>");
         client.println("Password: <INPUT TYPE=TEXT NAME=\"password\" VALUE=\"\" SIZE=\"25\" MAXLENGTH=\"50\"><BR>");
         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:
Code: [Select]
  if(strstr(tBuf,user) != NULL  && strstr(tBuf,pwd) != NULL )//Successful login
    {
      loggedin=1;
      digitalWrite(9, HIGH);    // set pin 4 high
      Serial.println("Login Successful!");         
    }

SurferTim

This is a symptom of no SRAM remaining:
Quote
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.  :(

nathanas

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

SurferTim

OK. Just to insure it is working on your board, try this test sketch:
Code: [Select]
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.

Go Up