SparkFun Forums 

Where electronics enthusiasts find answers.

For the discussion of Arduino related topics.
By garlinto
#136731
I have an EthernetPro that I've been using to open/close an electric gate via SMS. The sketch works as expected, letting me send gate commands and getting responses back. However, after 10 or so requests to the arduino, the board seems to hang. It will work fine after a reset, but then will hang again. I am using the 1.0 release with it's new client.find() and client.findUntil() functions. I've looked around the 'nets and have seen where others have had hanging issues, but a lot of those threads are old.

Is there a buffer that I need to be clearing, or something else that needs to be done after each request to keep the board running? I am not sure why it's crashing, but if someone has experienced similar issues, I'd love to hear how you resolved them. I don't mind posting the sketch, although it is kinda long.
By garlinto
#136748
As a test to rule out any of the support functions that loop() calls on to execute a request, I stripped down the sketch to include just the setup(), loop(), and printMsg() functions and let it run all night. Found it locked up/hung/crashed this morning. Could it be that I just have a bad board? I've included the sketch for you all to look at just in case there is something telling therein.
Code: Select all
#include <SPI.h>
#include <Ethernet.h>

byte mac[] = { 0x00, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };  
IPAddress ip(192, 168, 0, 130);
IPAddress gateway(192, 168, 0, 1);
IPAddress subnet(255, 255, 255, 0);

int command;
int eventId;

boolean hasCommand = false;
boolean hasEventId = false;

EthernetServer server = EthernetServer(80);

void setup()
{
  Ethernet.begin(mac, ip, gateway, subnet);
  server.begin();
  
  Serial.begin(9600);
  printMsg("Setup completed");
}

void printMsg(char* msg)
{
  Serial.print("Debug: ");
  Serial.print(msg);
  Serial.println();
}

void loop()
{
  EthernetClient client = server.available();
  if (true == client) {
    while (client.connected()) {
      if (client.available()) {
        if (client.find("GET /")) {
          
          if (client.findUntil("command=", "&")) {
            printMsg("Found command");
            command = client.parseInt();
            hasCommand = true;
          }
          
          if (client.findUntil("eventId=", "\n\r")) {
            printMsg("Found eventId");
            eventId = client.parseInt();
            hasEventId = true;
          }
          
          if (true == hasCommand && true == hasEventId) {
            printMsg("Sending response");
            client.println("HTTP/1.1 200 OK");
            client.println("Content-Type: text/html");
            client.println();
            client.print("<p>");
            client.print("The command is: ");
            client.print(command);
            client.println("</p>");
            // give the web browser time to receive the data
            delay(3);
            // close the connection:
            client.stop();
            printMsg("Done!");
          }
        }
      }
    }
  }
} // end loop()
By Harold65
#136822
I have had the same problem with the client.find() function in Arduino 1.0
It turns out that the find() is not working properly.
The find function uses timedread() which can be found in stream.cpp:
Code: Select all
// private method to read stream with timeout
int Stream::timedRead()
{
  int c;
  _startMillis = millis();
  do {
    c = read();
    if (c >= 0) return c;
  } while(millis() - _startMillis < _timeout);
  return -1;     // -1 indicates timeout
}
My arduino was hanging at the read() if there was no data available from the socket.
I changed this function to the following:
Code: Select all
// private method to read stream with timeout
int Stream::timedRead()
{
  _startMillis = millis();
  while(millis() - _startMillis < _timeout)
  {
    if (available() > 0) {
      return read();
    }
  }
  return -1; // -1 indicates timeout
}
Since then no more hangups :D
Can you test this change and confirm the outcome?
The read function should not hang without data but for some reason it does...