How to perform a ethernet request locally

Hi there,

I’ve got a problem with the Arduino Ethernet Shield. For a school project I have to get information from my local PC using ethernet.
Here’s the code I’ve tried so far:

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

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192, 168, 1, 2 };   
byte server[] = { 192, 168, 1, 1 };

EthernetClient client;

int result;

void setup ()
{
  Ethernet.begin(mac, ip);
  Serial.begin(9600);
  delay(1000);
  
  Serial.println("Verbindung wird hergestellt...");
}

void loop()

{
  if(client.connect(server, 80)>0)
  {
    Serial.println("Die Verbindung wurde erfolgreich hergestellt.");
    client.println("GET 127,0,0,1/test/index.php?test=hallo HTTP/1.0");
    client.println();
  }
  else 
  {Serial.println("That's an error. That's all we know.");
  }
  if(client.connected())
  {if(client.find("="))
  {result = client.parseInt();
  Serial.print("Hallo und " );
  Serial.print(result);
  }
  }
  else
    Serial.println("Kein Ergebnis gefunden");
    client.stop();
    delay(1000);
}

And here’s what I get on Serial Monitor:

Verbindung wird hergestellt...
That's an error. That's all we know.
Kein Ergebnis gefunden
That's an error. That's all we know.
Kein Ergebnis gefunden
That's an error. That's all we know.
Kein Ergebnis gefunden
That's an error. That's all we know.
Kein Ergebnis gefunden
That's an error. That's all we know.
Kein Ergebnis gefunden

My config:
Arduino UNO R3 with Ethernet Shield.
IP-Address of my computer: 192.168.1.1
Arduino’s IP: 192.168.1.2
Arduino and my PC are connected using a crossover-cable

My problem is that I have to receive data from a site that is located on localhost (127.0.0.1)
Please help me.

Thank you in advance

Yours
arduino2013

The most common problem is the server's firewall. Insure the firewall in your server allows port 80 requests from the internet. In this case, the internet is one Arduino.

    client.println("GET 127,0,0,1/test/index.php?test=hallo HTTP/1.0");

Wrong. The GET request is sent to the server you are connected to. It doesn't expect that there will be some incorrectly formatted IP address up front. IP addresses are NEVER comma separated and 127.0.0.1 is the address of the local machine. The Arduino is NOT trying to run a script that is is hosting.

Additionally, I recommend that you improve your code layout. Put each { and } on separate lines with matching pairs indented by the same amount and lines between them indented one extra level. Always follow if, else, do, while, for with a compound statement enclosed in { and } even if there is only one statement in it. The tools/autoformat command can be used to correct the indentation.

Hi there,

thank you so far for your replies.

  1. The firewall is not the problem.
  2. I don’t quite understand what Paul means. My problem is that I want to test if the Arduino can connect to a site that is hosted on localhost. Or isn’t that possible? It would be great if you could give me a working example code for this problem. Where do I have to put the IP-adresses?
  3. Here’s the code again with improved style:
#include <SPI.h>

#include <Ethernet.h>

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

byte ip[] = {
192, 168, 1, 2
            };  

byte server[] = {
192, 168, 1, 1 
                };

EthernetClient client;

int result;

void setup ()
{
  Ethernet.begin(mac, ip);
  
  Serial.begin(9600);
  
  delay(1000);
  
  Serial.println("Verbindung wird hergestellt...");
}

void loop()

{
  if(client.connect(server, 80)>0)
     {
       Serial.println("Die Verbindung wurde erfolgreich hergestellt.");
       client.println("GET 127,0,0,1/test/index.php?test=hallo HTTP/1.0");
       client.println();
     }
  else 
     {
      Serial.println("That's an error. That's all we know.");
     }
     
  if(client.connected())
  
  {
    if(client.find("="))
       {
         result = client.parseInt();
         Serial.print("Hallo und " );
         Serial.print(result);
        }
  }
  
    else
    {
    Serial.println("Kein Ergebnis gefunden");
    client.stop();
    delay(1000);
    }

Who can help me with that?

Thank you in advance.

This is the line that is failing.

  if(client.connect(server, 80)>0)

You can't access 127.0.0.1 from any computer except the one the server is running on. For external requests, you must use an ip assigned to the ethernet or wireless interface.

Can you access the the website by using 192.168.1.1 on the server?

PaulS is correct. When you eventually can connect, this will fail.

// replace this
       client.println("GET 127,0,0,1/test/index.php?test=hallo HTTP/1.0");
// with this
       client.println("GET /test/index.php?test=hallo HTTP/1.0");

No I can't connect. When I access the site from my PC I always have to type localhost or 127.0.0.1 before I go to test folder.

If you can't connect to the website from the server using 192.168.1.1, but can using 127.0.0.1, it won't work anywhere else. You must work on the server setup.

Is the server Windows (IIS) or Linux (Apache)?

The site is hoste using XAMPP for test purposes. So it would be better to host it using a freehoster like bplaced?

arduino2013: The site is hoste using XAMPP for test purposes. So it would be better to host it using a freehoster like bplaced?

I recommend using your own server for testing, but that is up to you, your skill level, and where you want to access it from.

I use IIS and Apache, and the Arduino works well with both, once they are working.

localhost, 127.0.0.1, and me all mean the same thing. From the Arduino, the site you want to access is not hosted by localhost, 127.0.0.1 or me. It is hosted by a server with a a real name or IP address.

That it works from your PC is because the site IS hosted by me. Try accessing the site from another computer in your network. When that works, you'll understand how to access the site from the Arduino.

arduino2013: No I can't connect. When I access the site from my PC I always have to type localhost or 127.0.0.1 before I go to test folder.

That should indicate that the server you are connecting to is on the same pc that is making the request to the server. For the arduino to connect to the server running on the pc, you would need to use the LAN IP address of the pc (usually something similar to 192.168.1.*).

Now I decided to forget about the local connection. My problem is now that I don’t get the result on the serial monitor.
It displays me:

Verbindung wird hergestellt...
Die Verbindung wurde erfolgreich hergestellt.
Kein Ergebnis gefunden.

My Arduino code is:

#include <SPI.h>

#include <Ethernet.h>

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

char serverName[] = "web0.vps6950.alfahosting-vps.de";

EthernetClient client;

int result;

void setup ()
{
  Serial.begin(9600);
  
  if(Ethernet.begin(mac) ==0)
  {
    Serial.println("DHCP-Konfiguration fehlgeschlagen");
    
    while(true)
    ;
  }
  delay(1000);
  
  Serial.println("Verbindung wird hergestellt...");
}

void loop()

{
  if(client.connect(serverName, 80)>0)
  {
    Serial.println("Die Verbindung wurde erfolgreich hergestellt.");
    
    client.println("GET /index.php?test=hallo HTTP/1.0");
    
    client.println();
  }
  else
  {
    Serial.println("That's an error. That's all we know.");
  }
  if(client.connected())
  
  {
    if (client.find("<b>"))
   
    {
      
    if(client.find("="))
    
  {
    result = client.parseInt();
    
    Serial.print("Hallo und " );
    
    Serial.print(result);
  }
  
  }
  else
  
    Serial.println("Kein Ergebnis gefunden");
    
    client.stop();
    
    delay(1000);
}

}

The PHP-Code I’m using is:

<?php

if($_GET["test"]=="hallo") {echo "<b>=7777</b>";}
else {echo "<b>=Servus</b>";}




?>

What’s wrong here?

Thank you all in advance.

arduino2013:
My problem is now that I don’t get the result on the serial monitor.

The serial output suggests you are able to connect and send the request.

Before you try to do anything clever with the response, just print out what you are getting back to see whether it is what you expect.

Unfortunately that doesn’t work either.

  if(client.connected())
  
  {
    if (client.find("<b>"))
   
    {
      
    if(client.find("="))
    
  {
    result = client.parseInt();
    
    Serial.print(result);
  }
  
  }
  else
  
    Serial.println("Kein Ergebnis gefunden");
    
    client.stop();
    
    delay(1000);
}

}

What else could ther be?

If your server is on your LAN, the DNS request may resolve to your WAN IP address and not your internal LAN IP address where your server is running. Below is some basic client code that uses an IP address instead of using DNS. Try the code as is and see if you get a response from the server (you should get back some info about server maintenance). If that works, then change the code to use the LAN IP address of your server pc, make sure the port address is set to the port your server is using, and change the GET request line to yours.

//zoomkat 4-04-12
//simple client test
//for use with IDE 1.0
//open serial monitor and send an e to test
//for use with W5100 based ethernet shields
//note that the below bug fix may be required
// http://code.google.com/p/arduino/issues/detail?id=605 
//the arduino lan IP address { 192, 168, 1, 102 } may need 
//to be modified to work with your router.

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

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address
byte myserver[] = { 208, 104, 244, 25 }; // zoomkat web page server IP address
EthernetClient client;
//////////////////////

void setup(){

  Ethernet.begin(mac);
  Serial.begin(9600); 
  Serial.println("Better client test 4/04/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(myserver, 80)) {  //starts client connection, checks for connection
    Serial.println("connected");
    client.println("GET /~shb/arduino.txt HTTP/1.0"); //download text
    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

}

This works just fine. But what does that mean for my code?

arduino2013: This works just fine. But what does that mean for my code?

Just as a quick observation, the below from your original posted code may be incorrect in that that particular LAN IP address is probably the IP address of your router and not the LAN IP address of the pc running the web server. You need to use the LAN IP address of the pc running the web server. If the server pc is running windows, at the command prompt (of cmd.exe) enter ipconfig, which will show the pc LAN IP address.

byte server[] = { 192, 168, 1, 1 };

As I wrote I don't want to do it anymore using LAN and XAMPP but I have a real server now. I've already posted the new code but still the problem described above occurs. What to do now?

Since you are jumping all about, perhaps you really need to start over and explain what you are currently trying to do, what you have done so far, what is working, what is not working, and post your current code.