Ethernet client : impossible to connect a perso.free.fr php web page [SOLVED]

Hi,
With a MEGA 2560 R3 + Ethernet shield W5100 I want to make a system to get data (temperature for example) and send it periodically to a php perso.free.fr web page.
My PHP URL web page is : http://lesjno.free.fr/TestArduino/testFromArduino.php
It return date, time, number of GET and POST parameters and the parameters received.

Here is the php code of this page :


<?php
$nb_get = count($_GET);
$nb_post = count($_POST);
$txt="";
$txt .= date("Y-m-d H:i:s")."\n";
$txt .= "NBGET=$nb_get,NBPOST=$nb_post\n";
$txt .= "GET :\n";
foreach ($_GET as $label => $value){

  • $txt .= "$label => '$value'\n" ;*
    }
    $txt .= "\nPOST :\n";
    foreach ($_POST as $label => $value){
  • $txt .= "$label => '$value'\n" ;*
    }
    echo str_replace("\n","
    \n",$txt);
    ?>

You can test it from a browser. Example :
http://lesjno.free.fr/TestArduino/testFromArduino.php?hhh=123456&bbb=sdsds
Will return :


2012-02-12 20:42:20
NBGET=2,NBPOST=0
GET :
hhh => '123456'
bbb => 'sdsds'
POST :


But I don’t perfrom to connect it from the Arduino card.

Here is my sketch :


#include <SPI.h>
#include <Ethernet.h>
byte mac[] = { 0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x02 };
// IP of lesjno.free.fr (IP got from a ping command in a PC command window)
byte server[] = { 212,27,63,167 };
// Initialize the Ethernet client library
EthernetClient client;
void setup() {

  • // start the serial library:*

  • Serial.begin(115200);*

  • // 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(server,80)) {*

  • Serial.println("connected");*

  • // Make a HTTP request:*

  • client.println("GET /TestArduino/testFromArduino.php?qqq=111 HTTP/1.1");*

  • client.print("Host: lesjno.free.fr\n");*

  • client.print("Referer: http://lesjno.free.fr\n\n");*

  • client.println();*

  • delay(200);*

  • }*

  • else {*

  • // if 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()) {*

  • char c = client.read();*

  • Serial.print(c);*

  • }*

  • // if the server's disconnected, stop the client:*

  • if (!client.connected()) {*

  • Serial.println();*

  • Serial.println("disconnecting.");*

  • client.stop();*

  • // do nothing forevermore:*

  • while(true);*

  • }*
    }


When I run the sketch, here is the result :

connecting...
connected
HTTP/1.1 400 Bad Request
Date: Sun, 12 Feb 2012 18:44:07 GMT
Server: Apache/ProXad [Nov 6 2010 15:36:40]
Connection: close
Cache-Control: no-cache, no-store, must-revalidate
Content-Type: text/html; charset=iso-8859-1

  • *
  • Free Pages Personnelles: Erreur 400 - La syntaxe de la requête est erronée*
  • *
  • *
  • *
  • *
  • *
  • *
  • *
  • *
  • *



Free

  • *

…etc… (in fact it’s the www.free.fr web page !)

I’ve tried several tips red in forums, but without success …
I don’t know what I can do to perform the connection !
Anyone already performe it ? Any suggestion ?
Thanks…

Normally, you should use sql format.

client.println("GET /TestArduino/testFromArduino.php?qqq=111 HTTP/1.1");

Yes, It's an error when I put the message into the forum ... my code is "GET /TestArduino/testFromArduino.php/mypage.php?qqq=111 HTTP/1.1", of course...
Strange, I test it again and the result on the monitor is now :
connecting...
connected
disconnecting.

That was my bad. There should have been only one reference to a page.
"GET /TestArduino/testFromArduino.php?qqq=111 HTTP/1.1"

This is the response I got with my web browser:

2012-02-12 22:22:17
NBGET=1,NBPOST=0
GET :
qqq => '111'

POST :

It's normal you get this result when you connect from a web browser.
I try (but I failed) to get the same from the Arduino card but there is certainly something wrong in my sketch, but what ?

I got the same page with this code on my Mega2560/ethernet shield.

#include <SPI.h>
#include <Ethernet.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[] = {  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
// Change your network settings
IPAddress ip(192,168,0,2);
IPAddress gateway(192,168,0,1);
IPAddress subnet(255,255,255,0);

IPAddress server(212,27,63,167); // Your server

// Initialize the Ethernet client library
// with the IP address and port of the server 
// that you want to connect to (port 80 is default for HTTP):
EthernetClient client;

void setup() {
  // start the serial library:
  Serial.begin(9600);
  // start the Ethernet connection:
  Ethernet.begin(mac,ip,gateway,gateway,subnet);
  
  // give the Ethernet shield a second to initialize:
  delay(2000);
  Serial.println("connecting...");

  // if you get a connection, report back via serial:
  if (client.connect(server, 80)) {
    Serial.println("connected");
    // Make a HTTP request:
    client.println("GET /TestArduino/testFromArduino.php?qqq=111 HTTP/1.0");
    client.println("Host: lesjno.free.fr");
    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:
  while(client.connected())
  {
    while(client.available()) {
      char c = client.read();
      Serial.print(c);
    }
  }
  
  // if the server's disconnected, stop the client:
  Serial.println("disconnecting.");
  client.stop();

    // do nothing forevermore:
    for(;;)
      ;
}

Thanks for the answer !
I've test with your code but that failed. Here is the result :
connecting...
connection failed
disconnecting.

Compare to your code, I've only change the network parameters :
IPAddress ip(192,168,1,140); // ip of the Ethernet shield on the local network
IPAddress gateway(192,168,1,254);
IPAddress subnet(255,255,255,0);

But After this test, I look your code and I see you wrote :
client.println("GET /TestArduino/testFromArduino.php?qqq=111 HTTP/1.0");
I've just change HTTP/1.0 to HTTP/1.1 and now I get this result :
connecting...
connected
HTTP/1.1 200 OK
Date: Sun, 12 Feb 2012 22:46:57 GMT
Server: Apache/ProXad [Nov 6 2010 15:36:40]
X-Powered-By: PHP/4.4.3-dev
Connection: close
Content-Type: text/html
2012-02-12 23:46:55
NBGET=1,NBPOST=0
GET :
qqq => '111'
POST :
disconnecting.

So, that seems to work !

I wil do other tests later ...
Thanks a lot for your help !

Mine connected and your server responded almost immediately. ??

I only changed the network settings on that code I posted. I use a public ip for testing.

If you have a dhcp server on that localnet, maybe you should use the settings it issues (like your original code).

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

Otherwise, I am not sure what else I can offer.

Edit: I see you have it working. Good deal!! :slight_smile:

Yes, I confirm now : you 're right. When the network parameters are good all works well.
I've test it with HTTP/1.0 and HTTP1.1 : I get exactly the same result.

Thanks again for your help !

I'm new on this forum. I don't know how to add "[SOLVED]" at the end of the subject. Do you know if it's possible or not ?

I think you can do that by editing the subject line on your first post.

Thanks again ! :slight_smile: