Pages: 1 [2] 3 4   Go Down
Author Topic: How to change size of readstring() in client.read() on Arduino Ethernet?  (Read 2283 times)
0 Members and 1 Guest are viewing this topic.
Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 551
Posts: 46256
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
    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?
Logged

0
Offline Offline
Full Member
***
Karma: 0
Posts: 167
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

i have set username as character
Code:
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
smiley

maybe I need to save it as another operator. not char
« Last Edit: December 08, 2012, 01:02:38 pm by nathanas » Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 551
Posts: 46256
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

0
Offline Offline
Full Member
***
Karma: 0
Posts: 167
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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:
"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:
 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!");        
    }
« Last Edit: December 08, 2012, 01:15:56 pm by nathanas » Logged

Miramar Beach, Florida
Online Online
Faraday Member
**
Karma: 116
Posts: 5394
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

0
Offline Offline
Tesla Member
***
Karma: 118
Posts: 8962
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
         //clearing string for next read
          readString="";
Logged

Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   smiley-cool

0
Offline Offline
Tesla Member
***
Karma: 118
Posts: 8962
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
        if (readString.length() < 150) {
Logged

Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   smiley-cool

0
Offline Offline
Full Member
***
Karma: 0
Posts: 167
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

All i'm doing is this...

 
Code:
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.
« Last Edit: December 08, 2012, 02:31:55 pm by nathanas » Logged

Miramar Beach, Florida
Online Online
Faraday Member
**
Karma: 116
Posts: 5394
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

0
Offline Offline
Full Member
***
Karma: 0
Posts: 167
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Miramar Beach, Florida
Online Online
Faraday Member
**
Karma: 116
Posts: 5394
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

0
Offline Offline
Full Member
***
Karma: 0
Posts: 167
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
/*
 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:
/*
 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:
  if(strstr(tBuf,user) != NULL  && strstr(tBuf,pwd) != NULL )//Successful login
    {
      loggedin=1;
      digitalWrite(9, HIGH);    // set pin 4 high
      Serial.println("Login Successful!");         
    }
« Last Edit: December 08, 2012, 04:19:50 pm by nathanas » Logged

Miramar Beach, Florida
Online Online
Faraday Member
**
Karma: 116
Posts: 5394
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.  smiley-sad
Logged

0
Offline Offline
Full Member
***
Karma: 0
Posts: 167
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Miramar Beach, Florida
Online Online
Faraday Member
**
Karma: 116
Posts: 5394
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Pages: 1 [2] 3 4   Go Up
Jump to: