0

I've created an Arduino Ethernet sketch, such that on a button push, it requests a URL (http://myserver.lan/sensors/garage.php). My thought was that I could then use this PHP script to pull a separate URL that I can update without having to reflash the Arduino. This separate URL drives certain home automation events.

I tried the following in garage.php:

<?php
    $homepage = file_get_contents('http://myserver.lan/test/');
?>

This works for my browser, but not for the Arduino board. The logs for the two are as follows:

Arduino:

192.168.2.50 - - [21/Mar/2013:13:43:58 -0400] "GET /sensors/garage.php HTTP/1.1" 400 515 "-" "-"

Safari:

192.168.2.3 - - [21/Mar/2013:13:43:28 -0400] "GET /test/ HTTP/1.0" 200 2235 "-" "-"
192.168.2.65 - - [21/Mar/2013:13:43:28 -0400] "GET /sensors/garage.php HTTP/1.1" 200 293 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_3) AppleWebKit/536.28.10 (KHTML, like Gecko) Version/6.0.3 Safari/536.28.10"

For completeness, here is the code sample from my Arduino sketch:

EthernetClient client;
if (client.connect(rackserver, 80)) {
    client.println("GET /sensors/garage.php HTTP/1.1");
}
client.stop();

How can I fix the PHP script? I'm hoping to accomplish this without reflashing my Arduino.

0

1 Answer 1

1

Notice how the log of the Arduino request shows that the server responded with a response code of 400 (bad request). Your Arduino code is not sending a valid HTTP request, for two reasons:

  1. A valid HTTP request terminates with an empty line.
  2. You specified HTTP version 1.1, which mandates that a request contains a Host: header.

Try this instead:

EthernetClient client;
if (client.connect(rackserver, 80)) {
  client.println("GET /sensors/garage.php HTTP/1.0");
  client.println();
}
client.stop();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.