Ethernet Connection with Arduino

Hello, I am having trouble with my Arduino Ethernet Board. I am trying to connect to my XAMPP Server and trying to retrieve data from a php file that I have created.

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

byte mac[] = {0x90, 0xA2, 0xDA, 0x00, 0x7E, 0xAE}
IPAddress server { 192, 168, 1, 223 };
IPAddress ipAddress { xxx,xxx,xxx,xxx };
IPAddress myDNS {8,8,8,8};
IPAddress myGateway{192,168,1,1};
IPAddress mySubnet{255,255,255,0};

EthernetClient client;

void setup()
{
Serial.begin(9600);
Ethernet.begin(mac, ipAddress, myDNS, myGateway, mySubnet);

delay(1000);
Serial.println("connecting");

if(client.connect(server, 80))
{
    Serial.println("Connected");
    client.println("GET /localhost/xampp/rec.php?name=Mahmoud HTTP/1.1");
}
else
    Serial.println("Not Connected");

if(client.avialable())
{
    Serial.println("Client Connected");
}
else
    Serial.println("Not Connected");

}

void loop()
{

}

Now when I run that code I get on my serial monitor Not Connected, I need to know why the arduino doesn't connect to my XAMPP Server. I am running Windows Server 2003. Thank You

IS your IP address correct? Given the fact that you've redacted it, I assume it isn't a 192 series address and it rather looks as though it should be.

Client test code you can try.

//zoomkat 9-22-12
//simple client test
//for use with IDE 1.0.1
//with DNS, DHCP, and Host
//open serial monitor and send an e to test
//for use with W5100 based ethernet shields
//remove SD card if inserted

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

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address

char serverName[] = "web.comporium.net"; // zoomkat's test web page server
EthernetClient client;

//////////////////////

void setup(){

  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    // no point in carrying on, so do nothing forevermore:
    while(true);
  }

  Serial.begin(9600); 
  Serial.println("Better client test 9/22/12"); // so I can keep track of what is loaded
  Serial.println("Send an e in serial monitor to test"); // what to do to test
}

void loop(){
  // check for serial input
  if (Serial.available() > 0) //if something in serial buffer
  {
    byte inChar; // sets inChar as a byte
    inChar = Serial.read(); //gets byte from buffer
    if(inChar == 'e') // checks to see byte is an e
    {
      sendGET(); // call sendGET function below when byte is an e
    }
  }  
} 

//////////////////////////

void sendGET() //client function to send/receive GET request data.
{
  if (client.connect(serverName, 80)) {  //starts client connection, checks for connection
    Serial.println("connected");
    client.println("GET /~shb/arduino.txt HTTP/1.0"); //download text
    client.println("Host: web.comporium.net");
    client.println(); //end of get request
  } 
  else {
    Serial.println("connection failed"); //error message if no client connect
    Serial.println();
  }

  while(client.connected() && !client.available()) delay(1); //waits for data
  while (client.connected() || client.available()) { //connected or data available
    char c = client.read(); //gets byte from ethernet buffer
    Serial.print(c); //prints byte to serial monitor 
  }

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

}

@wildbill

All of my IPAddress variables are fine. When I connect the Arduino and the XAMPP server is online everything is fine and it prints that it is connected. The problem is why isn't the client available, is there some extra code I should write to make my client available? Any ideas on how to solve the problem? Thank You

Typo here:

if(client.avialable())

But over and above that, you will need to loop looking for input - your code sends a request and then immediately looks for a response. The arduino is fast, your server won't have had a chance to send you anything when you make your single check for it. Take a look at the client example in the ethernet examples in the IDE.

Part of your problem may be the format of your request. The request must end with a blank line. Also, is the file in a directory named /localhost/xampp?

if(client.connect(server, 80))
{
    Serial.println("Connected");
    client.println("GET /localhost/xampp/rec.php?name=Mahmoud HTTP/1.1");
    // send blank line after Connection: close
    client.println("Connection: close\r\n");
}

Here is my client code. http://playground.arduino.cc/Code/WebClient

@wildbill @surferTim

I have done as suferTim suggested, I put a “Serial.println(“Connection Close\r\n”);” in the HTTP request and it worked fine, I checked if the client is available and it was available. Now I have another error “HTTP/1.1 Request 400 Bad Request”. Any suggestions on how to solve that problem, and I am using the GET Method.

Thank You

Any suggestions on how to solve that problem

Besides the obvious? Make a good request.

I am using the GET Method.

Let's see it.

If it's still this:

    client.println("GET /localhost/xampp/rec.php?name=Mahmoud HTTP/1.1");

That is a bad request. The Arduino (localhost) is NOT where the php script runs.

@PaulS

Hello, I am a newbie in the Arduino Ethernet. Please explain more your suggestion. Here are some information. I am using XAMPP Server on Windows 2003 Server. I store my php file in /xampp/htdocs/xampp. And I test run these files with my Google Chrome Web Browser and they work. Now I really don’t understand how to make a good request, and how I wrote that request it is written in the Arduino Ethernet Example http://arduino.cc/en/Tutorial/WebClient. Could you please type how a good request is made? Thank you.

And I test run these files with my Google Chrome Web Browser and they work.

How? Post the EXACT URL.

and how I wrote that request it is written in the Arduino Ethernet Example http://arduino.cc/en/Tutorial/WebClient.

That would be this line:

    client.println("GET /search?q=arduino HTTP/1.1");

You are not making a request to search. You are calling the search script in a directory on the server, relative to the top level directory.

You should probably be using:

client.println("GET /rec.php?name=Mahmoud HTTP/1.1");

@PaulS

When I try to test my PHP files. I place the files in xampp/htdocs/xampp. Then I go to Google Chrome Web Browser and type in http://localhost/xampp/*filename*. After that it executes the PHP script in the file. I have tried now client.println("GET /rec.php?name=Mahmoud HTTP/1.1"); which is still giving me a HTTP/1.1 400 Bad Request in my Serial Monitor. The Arduino is connects to the XAMPP Server fine and it prints that in the Serial Monitor. My PHP file that I want to access and read from with the Arduino is located at /xampp/htdocs/xampp/. Any other suggestions?

Any other suggestions?

Look at the log files that Apache creates. There is bound to be more information available.

I just made one minor change is change the name of the php file to print.php but it basically does the same thing. I also removed the "?name=Mahmoud"" Just to make sure everything is working fine.

I've opened the Apache access.log here is what I found in it: "192.168.1.224 - - [12/Jun/2013:23:33:05 +0300] "GET /print.php HTTP/1.1" 400 226"

I've also opened the Apache apache_error.log I found in it: "[Wed Jun 12 23:33:05 2013] [error] [client 192.168.1.224] client sent HTTP/1.1 request without hostname (see RFC2616 section 14.23): /print.php"

Hello, I have solved the problem. Here is the Software and Hardware information:
Software

  • Windows Server 2003
  • WAMP Server (latest version)

Hardware

  • Arduino UNO
  • Ethernet Sheild

Code for the Arduino

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

byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED}; //needs to be replaced with your own Ethernet shield mac address
//The following is based on your configuration of the DNS and the DHCP of the Windows Server 2003, you have to change the data below
//according to your configurations

IPAddress server { 192,168,1,100}; 
IPAddress ipAddress {192,168,1,101};
IPAddress myDNS {8,8,8,8};
IPAddress myGateway {192,168,1,1};
IPAddress mySubnet {255,255,255,0};

EthernetClient client;

void setup()
{
     Ethernet.begin(mac, ipAddress, myDNS, myGateway, mySubnet);
     Serial.begin(9600);

     delay(1000);
     Serial.println("Connecting");

     if(client.connect(server, 80))
     {
          Serial.println("Connected");
          client.println("GET /print.php");
          client.println("HTTP/1.1");
          client.println("Host: /localhost");
          client.println("user-agent: arduino/somethingorother");
          client.println();
          delay(1000);
     }
}

void loop()
{
    if(client.available())
    {
            char c = client.read();
            Serial.print(c);
            delay(1000);
    }
    else
    {
           Serial.println("Cannot Connect");
           delay(1000);
    }
}

Code for the php file (I named mine “print.php” and you will place it in the www directory of the WAMP)

<?php
echo "Hello World";
?>

There is only one thing left. If you uploaded the code in the Arduino and created the php file, it will give you the following error “client denied by server configuration: C:/wamp/www/print.php” (The C drive is where I installed the WAMP Sever also this depends on where you installed your WAMP Server). In order to solve this problem you need to find the httpd.conf file and look for the following:
<Directory “c:/wamp/www”

onlineofflinetag tag - don’t remove
Order Deny Allow
Deny from all
Allow from 127.0.0.1

You will only need to adjust the line of “Deny from all” to “Allow from all” (without quotes). All of the above worked fine for me, I hope this would be helpful for others. Thanks

So, you were trying to access a server that you did not know could be accessed from clients other than the server. Not a great idea. Before introducing the Arduino into the picture, you should have made sure that another client could access the server.

But, it’s good that you persevered and found a solution.