Pages: [1] 2 3 4   Go Down
Author Topic: How to change size of readstring() in client.read() on Arduino Ethernet?  (Read 2736 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Full Member
***
Karma: 1
Posts: 179
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi there,
 I have a problem with my code and sometimes stops running.Most of the times when I am sending large data strings. I think its because of the size of the client readstring().It just saves 50 charactes send from /GET.

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?

Code:
EthernetClient client = server.available();
  if (client) {
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();

        //read char by char HTTP request
        if (readString.length() < 150) {

          //store characters to string
          readString += c;
          //Serial.print(c);
        }

        //if HTTP request has ended
        if (c == '\n') {
  ....................
...............
}
}
}
« Last Edit: December 08, 2012, 02:26:22 pm by nathanas » Logged

Miramar Beach, Florida
Offline Offline
Faraday Member
**
Karma: 140
Posts: 5873
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I use a character array of a fixed size to store the GET request from a client. Try this code.
http://playground.arduino.cc/Code/WebServerST
It is the variable tBuf[] that holds the request string.
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48556
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I have a problem with my code and sometimes stops running.
And it will continue to have problems until you get rid of the String.

Quote
I tried to find the readstring function to modify its size
readString is an instance of the String class. It has methods, not functions. The readStrring instance takes care of its size by itself. You don't need to do anything to help it.
Logged

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

I managed to change all my scketch using arrays this time.
How can I search for a word in an array???
for example: if I have an array with size of 50, called char array[];
and I need to search for the word "key" how can I do it?
I am trying using if (strcmp,"key"==0) but its not the right one.

Can somebody hwlp me with the needed command?
Logged

Miramar Beach, Florida
Offline Offline
Faraday Member
**
Karma: 140
Posts: 5873
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Your "needed command" is in the web server code in the playground that I posted above. It searches for the r and s variables in the GET request.
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48556
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
How can I search for a word in an array???
There are so many assumptions in this question. How do you define a word?

You can use a number of string functions to search for a string in a string. None of them will know, or care, that the string being searched for meets your definition of a word.

Quote
if I have an array with size of 50, called char array[];
and I need to search for the word "key" how can I do it?
Is array properly NULL terminated? Have you looked at strstr()?

Quote
I am trying using if (strcmp,"key"==0) but its not the right one.
Not for finding a string in a string, unless the string being search is exactly the string be searched for, which seems unlikely. Or, it would be working.
Logged

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

I want to search for a string word in a char array!
the array is: //
and I need to search for some strings like: "username" & "test"

which is the right command?

i am trying with:
Code:
char tBuf[64];
char offarray[] ="off";
if(strstr(offarray,tBuf)!=0 )//checks for off

if(strstr(offarray,tBuf)==0 ) is always true
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48556
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Did you bother looking at the documentation for strstr()? It does not return an int, like strcmp() does.

The strstr() function returns a pointer to the string found, if one was found.

Whether that pointer is, or is not NULL, is impossible to tell from your snippets or (lack of) serial output.
Logged

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

http://www.cplusplus.com/reference/cstring/strstr/
That's the link I found.It helped me with strstr but only when tBuf just contains "off" if it contains more info it can't find off.
Why is this happening?
I am not so familiar with string manipulation so I don't know much. I am trying to do that all day with no luck.
Code:
char offarray[] ="off";
char tBuf[64];

 if(strstr(tBuf,"off") != NULL)//checks for off

          {

            digitalWrite(9,HIGH);    // set pin 4 low
            Serial.println("Led On");
            Serial.println(tBuf);
            delay(500);

          }
What tBuf contains: "GET /?username=off&password=&submit=Log+In HTTP/1.1"

"if(strstr(tBuf,"off") != NULL)" never goes TRUE
but "if(strstr(tBuf,"GET") != NULL)" does because it starts with that...
« Last Edit: December 08, 2012, 11:24:13 am by nathanas » Logged

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

I finally managed to do it!

Thanks PaulS once agan for your great help!   smiley-lol
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48556
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I finally managed to do it!
Great. It would be nice to mention how, in case anyone finds this thread later.

Quote
Thanks PaulS once agan for your great help!
All I did was kick your butt and make you think.
Logged

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

That's the right one!

Code:
char offarray[] ="off";
char tBuf[64];

 if(strstr(tBuf,offarray) != NULL)//checks for off

          {

            digitalWrite(9,HIGH);    // set pin 4 low
            Serial.println("Led On");
            Serial.println(tBuf);
            delay(500);

          }
Logged

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

Now I'm expieriencing a very strange problem.
The code from the previous link works like a charm but the code below doesn't...
Code:
 
char username[ ] = "admin"; // YOUR Username for Login
char password[ ] = "pass"; // YOUR Password for Login
int loggedin = 0;
char tBuf[64];


if((strstr(tBuf,"username") != NULL)  && (strstr(tBuf,"password") != NULL) )  {
  if((strstr(tBuf,username) == NULL)  || (strstr(tBuf,password) == NULL) ) {
    loggedin=0;
    Serial.println("Wrong");
  }
}

if((strstr(tBuf,"username") != NULL)  && (strstr(tBuf,"password") != NULL) ){
  if((strstr(tBuf,username) != NULL)  && (strstr(tBuf,password) != NULL ))//Successful login
  {
    loggedin=1;
    Serial.println("successful");  
  }    
}  

Arduino hangs and I have to Reset it...
« Last Edit: December 08, 2012, 12:11:19 pm by nathanas » Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48556
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'd use nested ifs, not compound ifs, so I could put Serial.print() statements in each block, to see which part is causing problems.

I'd share ALL the code, and the serial output.

Your mileage may vary.
Logged

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

This code works
Code:
void loop() {
Serial.println("Start...");  
.....code
.....code
 if(strstr(tBuf,username) == 0  || strstr(tBuf,password) == 0 ) {

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


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

This code doesn't work..... smiley-sad
After it prints "St" and hangs.....i have to reset it. and goes the same.
Code:

void loop(){
Serial.println("Start...");  
.....code
.....code
if(strstr(tBuf,"off") !=NULL) {
 digitalWrite(9, LOW);
     Serial.println("Led Off");  
    }


    if(strstr(tBuf,username) == 0  || strstr(tBuf,password) == 0 ) {

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


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

The sketch runs smoothly if we have just 2 ifs. If I add the third one, no matter which is it, it needs reset. and it doesn;t print anything. Strange right?
I've serial prints in every if statement but it doesn;t print not even the first one.
« Last Edit: December 08, 2012, 12:54:38 pm by nathanas » Logged

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