Pages: 1 [2] 3   Go Down
Author Topic: Uno + Ethernet Shield to PHP  (Read 8122 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

SurferTim,

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

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.

Code:
#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:
Code:
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.
« Last Edit: October 28, 2012, 08:56:18 am by cer » Logged

Miramar Beach, Florida
Offline Offline
Faraday Member
**
Karma: 138
Posts: 5833
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
@cer: Thanks! It is now included in IDE v1.0.1. 
Yeah, I saw that smiley I gave you the king's crown in my mind smiley

Quote
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.
Logged

Miramar Beach, Florida
Offline Offline
Faraday Member
**
Karma: 138
Posts: 5833
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 14
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Miramar Beach, Florida
Offline Offline
Faraday Member
**
Karma: 138
Posts: 5833
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Slider?

How often are you sending requests now? Are you still waiting the one millisecond between requests?
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

@SurferTim,

This is modified version of your code(http://www.arduino.cc/playground/Code/WebClient)

It works like a charm but I am still not sure if it is still stable. I mean your part of code is great, but what I did might be changed the stability.

What do you think?

Quote
/*
   Web client sketch for IDE v1.0.1 and w5100/w5200
   Posted October 2012 by SurferTim
*/

#include <SPI.h>
#include <Ethernet.h>
#include <TextFinder.h>

byte mac[] = {  0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x02 };
//IPAddress ip(192,168,1,12);
//IPAddress gateway(192, 168, 1, 12);
//IPAddress subnet(255, 255, 255, 0);

IPAddress server(192,168,1,12); // Google
EthernetClient client;

int totalCount = 0;
int loopCount = 0;
char pageAdd[32];
TextFinder finder( client );
void setup() {
  Serial.begin(9600);

  pinMode(4,OUTPUT);
  digitalWrite(4,HIGH);

  Ethernet.begin(mac);
  delay(2000);
  Serial.println("Ready");
}

void loop()
{
  if(loopCount < 10)
  {
    delay(10);
  }
  else
  {
    loopCount = 0;
    sprintf(pageAdd,"/data.php",totalCount);
    if(!getPage(server,pageAdd)) Serial.print("Fail ");
    else Serial.print("Pass ");
    totalCount++;
    Serial.println(totalCount,DEC);
  }    

  loopCount++;
}

byte getPage(IPAddress ipBuf,char *page)
{
  int inChar;
  char outBuf[128];

  Serial.print("connecting...");

  if(client.connect(ipBuf,80))
  {
    Serial.println("connected");

    sprintf(outBuf,"GET %s HTTP/1.0\r\n\r\n",page);
    client.write(outBuf);
  } 
  else
  {
    Serial.println("failed");
    return 0;
  }

  int connectLoop = 0;

  while(client.connected())
  {
    while(client.available())
    {
    // inChar = client.read();
   //   Serial.write(inChar);
      finder.find("<");
    long speed_1 = finder.getValue();
    Serial.print("Speed 1:");
    Serial.print(speed_1); // end get first user speed
    Serial.println("");
      
       client.flush();
    client.stop();
  /*   finder.find("<");
    long speed_1 = finder.getValue(); 
    Serial.print("Speed 1:");
    Serial.print(speed_1); // end get first user speed
    Serial.println("");*/
      connectLoop = 0;
    }
/*     if (client.available()) {
   // char c = client.read();
   // Serial.print(c);
   // Serial.println("b");
    
    finder.find("<");  // seek to the Results field
    long value = finder.getValue(); // get numeric value
    Serial.print(value);
    Serial.println("");
    client.flush();
    client.stop();
  }*/
    connectLoop++;
    if(connectLoop > 10000)
    {
      Serial.println();
      Serial.println("Timeout");
      client.stop();
    }
    delay(1);
  }

  Serial.println();

  Serial.println("disconnecting.");
  client.stop();

  return 1;
}


What does this connectLoop do by the way? It is just for testing i suppose, right?
Code:
connectLoop++;
    if(connectLoop > 100)
    {
      Serial.println();
      Serial.println("Timeout");
      client.stop();
    }
   
...and here is the serial print:
Code:
Pass 1431
connecting...connected
Speed 1:706

disconnecting.
Pass 1432
connecting...connected
Speed 1:706

disconnecting.
Pass 1433
connecting...connected
Speed 1:706

disconnecting.
Pass 1434
connecting...connected
Speed 1:706

disconnecting.
Pass 1435
connecting...connected
Speed 1:706

disconnecting.

Thank you in advance
Logged

Miramar Beach, Florida
Offline Offline
Faraday Member
**
Karma: 138
Posts: 5833
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The variable connectLoop controls the timeout. If the connection breaks, like hardware fail, it prevents the "while(client.connected()" loop from becoming an endless loop. I tested that by downloading a big webpage and pulling the CAT5 connector out of the shield halfway through the download. Then a couple minutes later, after several failed connection attempts, I plugged it back in, and it went right back to working.

edit: The "while(client.connected())" loop is exited when the server sends a close message, but not a hardware fail. If the connection breaks, the close message never gets through to the client from the server, and the client code hangs in that loop.

The connectLoop counts the iterations of that loop with no packet received. With the delay(1) in the loop, it is about a millisecond through each iteration, so I used 10000 as the number of times through the loop without a packet to exit the loop with the "client.stop()" call (no packet for 10 seconds). The other comparison loops exit normally, and the last client.stop() does nothing but return if the connection is closed.
« Last Edit: October 28, 2012, 12:21:54 pm by SurferTim » Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

@SurferTim

I got it. You thought of everything smiley That's great, you saved my day, thank you very much!

Logged

Jakarta, Indonesia
Offline Offline
Newbie
*
Karma: 0
Posts: 10
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I have a project to control 2 servos using button on php web.
I've tried to connect the arduino to PHP then serial monitor shows this:

Code:
Starting ethernet...
192.168.0.4
Ready
connecting...connected
HTTP/1.1 200 OK
Date: Thu, 04 Jul 2013 08:47:15 GMT
Server: Apache/2.2.11 (Win32) DAV/2 mod_ssl/2.2.11 OpenSSL/0.9.8i mod_autoindex_color PHP/5.2.8
Content-Length: 1404
Connection: close
Content-Type: text/html;charset=utf-8

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<html>
 <head>
  <title>Index of /</title>
 </head>
 <body>
<h1>Index of /</h1>
<pre><img src="/icons/blank.gif" alt="Icon "> <a href="?C=N;O=D">Name</a>                    <a href="?C=M;O=A">Last modified</a>      <a href="?C=S;O=A">Size</a>  <a href="?C=D;O=A">Description</a><hr><img src="/icons/folder.gif" alt="[DIR]"> <a href="Dessy/">Dessy/</a>                  04-Jul-2013 14:26    -   
<img src="/icons/folder.gif" alt="[DIR]"> <a href="Image/">Image/</a>                  01-Jul-2013 13:35    -   
<img src="/icons/folder.gif" alt="[DIR]"> <a href="TA/">TA/</a>                     15-Jun-2013 07:31    -   
<img src="/icons/folder.gif" alt="[DIR]"> <a href="bacup/">bacup/</a>                  21-Dec-2007 09:01    -   
<img src="/icons/folder.gif" alt="[DIR]"> <a href="gagal/">gagal/</a>                  14-Jun-2013 20:58    -   
<img src="/icons/folder.gif" alt="[DIR]"> <a href="includes/">includes/</a>               01-Jul-2013 13:37    -   
<img src="/icons/folder.gif" alt="[DIR]"> <a href="jadi/">jadi/</a>                   03-Jul-2013 22:00    -   
<img src="/icons/folder.gif" alt="[DIR]"> <a href="xampp/">xampp/</a>                  21-Dec-2007 09:01    -   
<hr></pre>
<address>Apache/2.2.11 (Win32) DAV/2 mod_ssl/2.2.11 OpenSSL/0.9.8i mod_autoindex_color PHP/5.2.8 Server at 192.168.0.2 Port 80</address>
</body></html>

disconnecting.
Pass 1

Is the information above shows that the arduino and php are connected? If yes, I don't know how to control the servos using button on php web.
Help me please
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Is the information above shows that the arduino and php are connected?
The Arduino and the server where the php script is running connected, the php script was executed, and generated some output. The server sent that output back to you.

Quote
If yes, I don't know how to control the servos using button on php web.
Help me please

I think you are going about this all wrong. Typically, the Arduino serves up a page, containing a form with submit items. The client renders the page, and the user does something that triggers a submit item which triggers the action script. That script usually performs another GET to the server (the Arduino) containing additional information, which the Arduino parses to determine what to do.

There is nothing in the output that you showed that indicates that you made a useful GET request from the Arduino as client. All that is is an index listing from a directory, typically generated when you do not properly specify a script name, and there is an index.php script in the directory.

We'd need to see your Arduino code, and understand how you think a client is going to get useful information from the server.
Logged

Jakarta, Indonesia
Offline Offline
Newbie
*
Karma: 0
Posts: 10
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I think you are going about this all wrong. Typically, the Arduino serves up a page, containing a form with submit items. The client renders the page, and the user does something that triggers a submit item which triggers the action script. That script usually performs another GET to the server (the Arduino) containing additional information, which the Arduino parses to determine what to do.

There is nothing in the output that you showed that indicates that you made a useful GET request from the Arduino as client. All that is is an index listing from a directory, typically generated when you do not properly specify a script name, and there is an index.php script in the directory.

We'd need to see your Arduino code, and understand how you think a client is going to get useful information from the server.

Here is my php code:
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>User Room</title>
<style type="text/css">
.alt {
font-family: "Palatino Linotype", "Book Antiqua", Palatino, serif;
color: #09C;
}
.azm {
font-family: "Palatino Linotype", "Book Antiqua", Palatino, serif;
color: #09C;
text-align: center;
font-weight: bold;
}
.move {
color: #FFF;
}
.alt {
color: #FFF;
}
.azm {
color: #FFF;
}
.btn {
font-family: "Palatino Linotype", "Book Antiqua", Palatino, serif;
}
.txt {
color: #FFF;
}
.txt .txt {
font-weight: bold;
color: #39F;
text-align: center;
}
</style>
</head>

<body bgcolor="#000000">
<form action="form1" method="get">
<p class="txt"><span class="txt">CLICK THE BUTTON TO CONTROL TEHE TELESCOPE!</span> </p>
<table width="400" border="2" align="center">
    <tr>
      <th colspan="2" class="alt" scope="row">Altitude Control</th>
      <td colspan="2" class="azm">Azimuth Control</td>
    </tr>
    <tr>
      <th scope="row"><input type="submit" name="Move" id="Move" value="Move" /></th>
      <td><input type="submit" name="Stop" id="Stop" value="Stop" /></td>
      <td><input type="submit" name="Move2" id="Move2" value="Move" /></td>
      <td><input type="submit" name="Stop2" id="Stop2" value="Stop" /></td>
    </tr>
  </table>
</form>
</body>
</html>

When the user click move button, the servo will rotates. then when button stop was clicked, the servo will stop.
I use 4 buttons because I use 2 servos for the pan and tilt.
I don't know how to make the code for the Arduino using text finder as the previous post to connect arduino to PHP.
« Last Edit: July 05, 2013, 02:49:27 am by eprilya » Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Here is my php code
That is not php code. It is nothing more than static html.

Where does that code live? If the Arduino is a client, making a GET request to a server to get that data, there is no way that the Arduino can render that code in a way that a user can interact with it.

If the Arduino is a server, serving up that page, then when the user interacts with the application that is rendering that data, the action method means that any interaction will generate a new GET request that the Arduino is supposed to deal with.

The question that you have tap danced around long enough is simple. Is the Arduino a client or is it a server? Post your code to show which it is.

If it is a client, I'll repeat my statement that you are going about this all wrong. You can't get data from a served client (a browser) from another client (the Arduino). It simply is not possible. The server doesn't even know which client it sent that page to, so there is no possible way to have the action method on that client send information to the Arduino as client.

The Arduino as client pulls data. The Arduino as server pushes data only in response to a pull request.
Logged

Jakarta, Indonesia
Offline Offline
Newbie
*
Karma: 0
Posts: 10
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

That is not php code. It is nothing more than static html.

Where does that code live? If the Arduino is a client, making a GET request to a server to get that data, there is no way that the Arduino can render that code in a way that a user can interact with it.

The Arduino as client pulls data. The Arduino as server pushes data only in response to a pull request.

I still don't understand how to make GET request. Sorry, I'm newbie smiley-cry
Before I do that, I want to ask about this.
My web is consist of some page. Then the control page is not index page.
The system is, only registered members are able to go to control page after login with username and password. How the arduino can render the code on the control page?
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
My web is consist of some page. Then the control page is not index page.
The system is, only registered members are able to go to control page after login with username and password. How the arduino can render the code on the control page?
Enough tap dancing. Your Arduino is either a client or a server. There are some things it can do as a client that it can't do as a server. There are some things it can do as a server that it can't do as a client. Post your Arduino code so we can tell which it is, since you won't directly answer the question.

I get the impression, though, that you think that a client on one machine can "see" what a browser on another machine has rendered. That is NOT possible. For any client or any browser.
Logged

Pages: 1 [2] 3   Go Up
Jump to: