Developing a 3-channel lighting controller to run off a server hosted by the arduino ethernet. I have a server operational and more-or-less functioning.
One problem I have is that if I enter in invalid data into the URL to pull up the browser-based software, the controller hangs and I must reset the controller.
Examples:
-
I type in the IP address to open the controller interface
http://192.168.24.253/ -
I make setting adjustments to different channels
http://192.168.24.253/?r=255&g=255&b=255&H=Program
sets r, g, & b values. Program button applies the data
Everything works fine in this fashion
- If erroneous data is sent to the controller like this:
http://192.168.24.253/blahblahblah
the controller hangs.
Is there a way I can safeguard against bad data? Or at least prevent the controller from hanging and needing a reset?
If I have to deal with it hanging, would a watchdog timer work in resetting the controller?
I suspect the problem in the code is within this section, but honestly I'm confused as to how this portion of the code works.
Portion of "Loop"
void loop()
{
// Serial.println(str);
// Serial.print("freeMemory()=");
// Serial.println(freeMemory());
// delay(2000);
int bufLength;
Client client = server.available();
if (client) {
boolean current_line_is_blank = true;
while (client.connected()) {
if (client.available()) {
char c = client.read();
if (inString.length() < maxLength) {
//inString.append(c);
inString += c;
}
if (c == '\n' && current_line_is_blank) {
if (inString.indexOf("?") > -1) {
int Pos_r = inString.indexOf("r");
int Pos_g = inString.indexOf("g", Pos_r);
int Pos_b = inString.indexOf("b", Pos_g);
int End = inString.indexOf("H", Pos_b);
if(End < 0){
End = inString.length() + 1;
}
bufLength = ((Pos_g) - (Pos_r+2));
if(bufLength > 4){ //dont overflow the buffer
bufLength = 4;
}
inString.substring((Pos_r+2), (Pos_g-1)).toCharArray(colorBuff, bufLength); //transfer substring to buffer
r = atoi(colorBuff);
bufLength = ((Pos_b) - (Pos_g+2));
if(bufLength > 4){ //dont overflow the buffer
bufLength = 4;
}
inString.substring((Pos_g+2), (Pos_b-1)).toCharArray(colorBuff, bufLength); //transfer substring to buffer
g = atoi(colorBuff);
bufLength = ((End) - (Pos_b+2));
if(bufLength > 4){ //dont overflow the buffer
bufLength = 4;
}
inString.substring((Pos_b+2), (End-1)).toCharArray(colorBuff, bufLength); //transfer substring to buffer
b = atoi(colorBuff);
This is based off an individuals code here: http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1238498454