Hi everybody!
Hope this is the right forum to post my problem. If it's not, I apologize.
For a project of mine (for my Master's thesis, university rules forbid me from telling you any details...that sucks) I have to create a custom library.
I did that and everything worked fine. Then, without any reason (at least not any I noticed), the following happened:
The library code stopped working and so I fired up the Serial Monitor to see the output of my code (some debug messages to Serial) to check what went wrong. I noticed something odd. My code has to read from a Client instance, byte by byte until a newline character, then examine the line and continue until no more data is coming in. I am using a char[] to store the incoming bytes one by one (that might not be the smartest idea, I guess - any suggestions?). I tried to print the whole buffer to Serial after each byte read and found the data I expected. But then, some odd characters appeared on the screen, the execution seemed to freeze and the setup()-method was called and executed! That happened over and over again. As you can imagine, this is absolutely not what I want to do :~
So here's my question: is it possible that either the usage of Serial.print[ln] or the usage of a char[] (I use 1024 bytes at once to buffer data) could interfere with Arduino's whole program? I mean...calling the setup()-method over and over again seems...just wrong. The funny thing is, that when I added some more Serial.printlns to my code, it started working again! That is just...odd...(Serial is using 9600 baud).
As I mentioned before, I can't give you the whole code, but here's a snippet from it, maybe it will help:
//_line_length is 1024
char buffer[_line_length];
//Read the whole answer
while((c = client.read()) != -1)
{
//Serial.print(c);
buffer[offset] = c;
offset++;
//Serial.println(buffer);
//To keep the end-of-string all the time!
buffer[offset] = '\0';
//c contains the current byte read
if(c == '\n' || offset == _line_length-1)
{
Serial.println("NEW LINE");
//New line starts here (or buffer is full)
//Examine line
//Extract status code
if(startsWith(buffer, "HTTP/1.1 200")) { statuscode = 200; }
if(startsWith(buffer, "HTTP/1.1 404")) { statuscode = 404; }
if(startsWith(buffer, "HTTP/1.1 500")) { statuscode = 500; }
//Reset buffer and offset
// I tried using memset, but that seemed to make things worse
//memset(buffer, '\0', sizeof(buffer));
//TEST - did not work either
//free(buffer);
offset = 0;
}
}
The above snippet should give you the gist of what I am doing. After each newline / when the buffer is full, I examine the line just read.
I suspect that my code might be overriding some bytes of the Arduino's own code, thus leading it into an unstable state, thus causing it to reset over and over again. But why can this be fixed by some additional Serial.printlns? I do not get it.
Has anyone ever noticed something like this in his/her own code and found a fix or workaround!? I don't dare to remove the Serial stuff from my code right now as it is finally working and I have some testing to do. But in the final version of the code, I want to remove the printing, so I need to know the root of my problem.
Any help is highly appreciated!
Greets and THX - steps