Go Down

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

nathanas

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: [Select]
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') {
 ....................
...............
}
}
}

SurferTim

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.

PaulS

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.

nathanas

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?

SurferTim

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.

PaulS

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.

nathanas

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: [Select]
char tBuf[64];
char offarray[] ="off";
if(strstr(offarray,tBuf)!=0 )//checks for off


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

PaulS

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.

nathanas

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

nathanas

I finally managed to do it!

Thanks PaulS once agan for your great help!   XD

PaulS

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.

nathanas

That's the right one!

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

          }

nathanas

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

PaulS

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.

nathanas

This code works
Code: [Select]

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..... :(
After it prints "St" and hangs.....i have to reset it. and goes the same.
Code: [Select]

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.

Go Up