Uno + Ethernet Shield to PHP

Hello there,

I have a setup where Arduino Uno + Ethernet Shield connects to my localhost and receives data from data.php through GET method.
Localhost only serves the data and Arduino codes takes it into loop and receives it in delay(1);

My problem with it is that in the beginning or after some seconds of the beginning Arduino or Ethernet Shield hangs for a moment. then continues to stream the data from localhost. This issue is really unstable, it sometimes hangs couple of times and sometimes it doesn't even hang.. sometimes it hangs first then doesn't hang ever until a reset.. I left the serial monitor open all night and in the morning I saw 250.000 lines in the serial monitor although I couldn't figure out if it's hanged or not..

So anybody have experience on this kind of thing? talk to server through GET via php-mysql in realtime.

*changing delay from 1 to 100 doesn't make any difference then the mili second it self.

I am also open for any suggestions including the same setup over usb connection or wifi.

in the mean time I am so close to my deadline.

Arduino codes takes it into loop and receives it in delay(1);

I don't think so. Nothing happens in the sitOnYourAssAndDoNothing() function. Certainly not anything like getting data from a server or client.

Some code we could look at?

What version of the IDE are you using to program the Arduino?

PaulS is right. You should post your code if you want help.

I left the serial monitor open all night and in the morning I saw 250.000 lines in the serial monitor...

That is a lot. Is this going into a MySQL database? How many entries are there?

Here is a topic that covers PHP/MySQL and the ethernet shield.
http://arduino.cc/forum/index.php/topic,124289.0.html
Reply #13 has the php and reply #15 has the Arduino code I use.

So this is the code I am using, it's made up from the google example.

I am using 1.0.1 and mysql entries will be flushed in every 60 seconds or something..

My actual project is controlling two different motors over two different iphones in local area network. iphones will open a web page in 192.168.1.2/whatever.php then there will be a slider there. when an iphone slides it between 0-1023 I set the speed of motors. There is no problem with controlling the motor I am only trying to make it as stable as it can be before jumping to the motor part (which is already working.) So I need somekind of realtime data stream. It doesn't have to be 1ms but it should be fast enough to see the reaction of motor when I use the slider on iphone or whatever.

(By the way I am having issue with HTTP/1.0 when it is written in the GET line, so I removed it.)

#include <SPI.h>
#include <Ethernet.h>
#include <TextFinder.h>
// Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
byte mac[] = { 0x90, 0xA2, 0xDA, 0x0D, 0x43, 0xC4 };


char serverName[] = "192.168.1.2";


EthernetClient client;
TextFinder finder( client );
void setup() {
 // Open serial communications and wait for port to open:
  Serial.begin(9600);
   while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }


  // start the Ethernet connection:
  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    // no point in carrying on, so do nothing forevermore:
    while(true);
  }
  // give the Ethernet shield a second to initialize:
  delay(1000);
  Serial.println("connecting...");

  // if you get a connection, report back via serial:
  
  if (client.connect(serverName, 80)) {
    Serial.println("connected");
    // Make a HTTP request:
    client.println("GET /data.php");
   // client.println();
  } 
  else {
    // kf you didn't get a connection to the server:
    Serial.println("connection failed");
  }
}

void loop()
{
  // if there are incoming bytes available 
  // from the server, read them and print them:
  if (client.available()) {
    
    finder.find("<");  // seek to the Results field
    long value = finder.getValue(); // get numeric value
    Serial.print(value);
    client.stop();
    
  }
  delay(1);
  
    if (!client.connected()) {
    Serial.println();
    client.stop();

  }
  if(client.connect(serverName, 80))
 { 
    client.println("GET /data.php");
    client.println();
 }
  
}

any thoughts?

@SurferTim I will take a look at your example now.

Pay close attention to how my code reads the response from the server and closes the connection. The server will close the connection when it is finished sending packets, then you close your end.

It is basically this client code in the playground.
http://www.arduino.cc/playground/Code/WebClient

so I changed the last two delays to 1ms to fake the data stream, it hangs on pass 25 waits a bit then continues. is this a logical stress test?

*Edit: second hang on pass 302

I can't be sure if this is because of Ethernet Shield or Arduino Uno? Which ones buffer full's up?

Using Arduino Mega by having more processor power and flash memory should solve this out?

KodiakBear:
so I changed the last two delays to 1ms to fake the data stream, it hangs on pass 25 waits a bit then continues. is this a logical stress test?

*Edit: second hang on pass 302

Are you talking about my code or yours? If mine, which two delays did you change? What does the serial monitor say about that connection hang? If it is "Timeout", that takes 10 seconds before the connection will timeout and resume. That normally happens when there is a connection break, like pulling the CAT5 cable connector out of the ethernet shield or powering cycling the router.

The ethernet shield does the buffering. If the tx buffer fills up, the write function is blocking (waits for tx buffer space before returning).

yes I am talking about your code. there are three delays in your code first one is for ethernet shield to initialize. the rest is like this ;

if(loopCount < 30)
  {
    delay(1);
  }
    delay(1);
    connectLoop++;
    if(connectLoop > 10)

I changed these delays to 1

I don't have a router, I have a cat5 cable to my mac directly, mac is the localhost. so there is no router.

Serial monitor only says connecting... if it hangs. from that second hang on 302 it is still streaming data and it is around pass 3300 between 302 and 3300 there was no hang. which is so weird.

it hangs for 302 post and doesn't hang for 3000 posts..

The first change is ok. The second is not. The server may not respond in 10 ms. You can change the delay to 1 ms, but you must wait at least a couple seconds before timing out. Like this is ten seconds:

    delay(1);
    connectLoop++;
    // if no packet for 10000 milliseconds
    if(connectLoop > 10000)

edit: Are you using Apache in your Mac? If so, what does the log say?

Maybe I wasn't clear about the timeout. That would only happen if the server does not either send another packet, or close the connection. Most of the time, the server response and close should be immediate. Then you would not wait nearly 10 seconds.

Yes I am using Mac with MAMP which is Apache..

I used the last code you sent there was no hang. then I switched back to 10ms there was no hang too..

We are also working with pc's installed WAMP, also testing there..

I believe sometimes when it starts to stream if it doesn't hang, it doesn't hang ever in that session. If I close the serial then open it back for a new session it may hang.. we have been testing this for 4 days :confused:

and I don't know how to look up to apache log.

KodiakBear:
and I don't know how to look up to apache log.

I suggest you find out about the logs. Every time your Arduino makes that request, it should be putting an entry in the log. At your request rate, the logs can get quite large in a very short time. And Apache saves four weeks of logs.

The timeout should be left alone. When you are connected directly to the Mac, this may not be a problem. But when you put a router with a computer and an internet connection into the network, things change. But that is up to you. You can test it, and if it performs ok, use it! :slight_smile:

okay I will take a look at the logs now.

whole project will be working in a local area network. there won't be any router or internet connection in any time of this project scope.

SurferTim,

Big fan of yours because of your ethernet bug solution for v1.0 IDE :slight_smile:

I am working on the same project with KodiakBear.

I wrote the following code simultaneously with him. Mine is working well. The following code is working on my computer(Windows 7) without stalling. I 've tried it on 2 different XP computers and it does not stall on them as well. But we are not sure it will not stall somewhere, sometime.

#include <SPI.h>
#include <Ethernet.h>
#include <TextFinder.h>
byte mac[] = {  0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x02 };
char serverName[] = "192.168.1.12";
EthernetClient client;
TextFinder finder( client );

void setup() 
{
       // Open serial communications and wait for port to open:
        pinMode(4,OUTPUT);
        digitalWrite(4,HIGH);
        Serial.begin(9600);
        while (!Serial) 
         {;} // wait for serial port to connect. Needed for Leonardo only


  // start the Ethernet connection:
        if (Ethernet.begin(mac) == 0) 
        {
          Serial.println("Failed to configure Ethernet using DHCP");
          // no point in carrying on, so do nothing forevermore:
          while(true);
        }
  // give the Ethernet shield a second to initialize:
        delay(1000);
        Serial.println("connecting...");

                    // if you get a connection, report back via serial:
                    
                            if (client.connect(serverName, 80)) 
                            {
                              Serial.println("connected");
                              // Make a HTTP request:
                          //    client.println("GET /data.php");
                              client.write("GET /data.php HTTP/1.0\r\n\r\n");
                            
                            } 
                            else 
                            {
                              // kf you didn't get a connection to the server:
                              Serial.println("connection failed");
                            }
}     //END OF SETUP

void loop()
{
  // if there are incoming bytes available 
  // from the server, read them and print them:
  if (client.available()) 
  {
    Serial.println("Client available");
    
    //begin get user count
    finder.find("#"); //get user count
    long user_count = finder.getValue();
    Serial.print("User Count:");
    Serial.print(user_count); 
    Serial.println("");
    //end get user count
    //begin get car choice
    finder.find("[");
    long car_type = finder.getValue(); 
    Serial.print("Car Type:");
    Serial.print(car_type); 
    Serial.println("");
    // end get car choice
    // begin get first user speed
    finder.find("<");
    long speed_1 = finder.getValue(); 
    Serial.print("Speed 1:");
    Serial.print(speed_1); // end get first user speed
    Serial.println("");
    // end get first user speed
    // begin get second user speed
    finder.find("{");
    long speed_2 = finder.getValue(); 
    Serial.print("Speed 2:");
    Serial.print(speed_2); 
    Serial.println("");
    // end get second user speed
    client.stop();
    //client.flush();
   
/*    int motor1_hiz = speed_1/4;
    digitalWrite(motor1_input_1, HIGH);
    digitalWrite(motor1_input_2, LOW);
    analogWrite(motor1_en,motor1_hiz);    
    
    int motor2_hiz = speed_2/4;
    digitalWrite(motor2_input_1, HIGH);
    digitalWrite(motor2_input_2, LOW);
    analogWrite(motor2_en,motor2_hiz); */
    }
    delay(100);

  
/*   if (!client.connected()) 
   {
    Serial.println("Client is not connected");
    client.stop();
   }*/
 
//This is your hack from couple months ago 
/*int newFree = client.free();

if(newFree < 1000)
{
   Serial.print("Low buffer ");
   Serial.println(newFree, DEC);
}   */
   
 if (client == false) // there was a bunch of code between the following and client.stop. So I wrote this. Now it is not necessary for now
 {
      
    if(client.connect(serverName, 80))
   { 
   //   client.println("GET /data.php"); 
      client.write("GET /data.php HTTP/1.0\r\n\r\n"); 
    //  client.write();
   //   client.println();
   }
 }  
    
} // END OF CODE

Here is the serial print:

connecting...
connected
Client available
User Count:2
Car Type:1
Speed 1:706
Speed 2:470
Client available
User Count:2
Car Type:1
Speed 1:706
Speed 2:470
.
.
.

So, is it possible for you to compare two different codes and figure out what the problem might be? I think I go around the problem somewhere.

Edit: added last sentence, my bad.

@cer: Thanks! It is now included in IDE v1.0.1. :slight_smile:

I use my code. I have already debugged it and put it in the playground.
http://www.arduino.cc/playground/Code/WebClient
It has a good connection fail timeout and response display. It should keep running no matter what happens. If it doesn't, the serial monitor should be helpful in troubleshooting. I have intentionally failed the connection during a download to test it. It recovers fine.

@cer: Thanks! It is now included in IDE v1.0.1.

Yeah, I saw that :slight_smile: I gave you the king's crown in my mind :slight_smile:

I use my code. I have already debugged it and put it in the playground.

I 'm now trying it. I'll share the results.

Thank you.

If you are trying the same type code, then I recommend reducing the time between requests slowly. Try it at 10 seconds apart to start instead of 30 seconds. That is fast enough to test, but slow enough so you can read the serial monitor. Once that is working, then slowly reduce the time between requests.

so I have been inspecting apache logs in realtime over mac os x terminal.

it is clear that when arduino hangs on GET, Apache simply waits for GET request. There is nothing seems to be wrong in the server side.

if I change my slider in the browser when hang happens, it does send the slider value to sql according to apache logs