Using the Arduino Ethernet Shield and PHP Web

Hello,

Could somebody help me with the syntax needed for sending a parameter via the Ethernet Shield Client.print statement?

I am reading the analog value of a pin....when I send that to Serial it reads the value just fine.

When I construct the code to pass parameters to my PHP website, the code works just fine with hard coded values ie:

client.println("GET /test.php?value0=123&value1=456");

This sends the code to my website..my php script grabs it and I get a website output of 123 and 456.

When I try a little more coding, I capture the analog reading into an int variable....but when I try to pass it I get Bad Response from Apache ie;

client.print("GET /test.php?value0=");
client.print(AnalogValue);
client.print(&value1=456);
client.println(" HTTP/1.1\n");

As you see I'm trying to code the line consisting of text and the analog value....but it doesn't work.

I've looked at all kinds of examples on the web but can't seem to work out how to pass a variable within a text string?

Please help.

Thanks

try this:

client.print("GET /test.php?value0=");
client.print(AnalogValue);
client.print("&value1=");
client.print(456);
client.println(" HTTP/1.1");
client.println();

Don't forget to use the # button in the forum to post code. (Select your code, press the # button...)

The reason you get a bad request is because you haven't sent the headers.

GET [page&QueryString] [protocol]\r\n
is the first line, followed by headers, followed by
\r\n [EDIT]on a blank line[/EDIT] to signal end of headers.

(My code above has the second println to skip the HTTP headers.)

check out the RFC on the HTTP protocol for more info: RFC 1945 - Hypertext Transfer Protocol -- HTTP/1.0 (RFC1945)

Thanks for the reply and pointers.

I replaced my little snippet with your code and got the same errors.

It seems as soon as I split the GET statement onto multiple lines it fails.

If I supply one long string without variables it works just fine.

I looked at other examples on the net and they seem to be coded for different shields. I have the official Arduino Ethernet Shield. Xport examples etc are far too complicated for me to understand.

When using serial.print the string looks ok...

I need to sleep on this one.

Wait a minute - did you say apache is giving "bad response?"

Check your code on your webserver... "Bad Request" is what I would expect if the arduino code is broken.

Hi Spinlock, thanks for your continued help and patience.

This is the Serial output which shows bad request;

sorry my paste in from my Mac doesn't seem to work.

The line response from Apache amongst a whole drawl of stuff is;

HTTP/1.1 400 Bad Request

and then later in the summary;

Your browser sent a request that this server could not understand.

I do see this as related to inserting the variables and splitting the GET statement into lines?

I'm starting to go crazy now and need to hold back from messing it up even more by changing everything as I usually do.

Give this a try:
Apache may be expecting some headers, with HTTP version 1.1...

client.print("GET /test.php?value0=");
client.print(AnalogValue);
client.print("&value1=");
client.print(456);
client.println(" HTTP/[glow]1.0[/glow]");
client.println();

and then this, to see if Apache is expecting the Host to be present.

client.print("GET /test.php?value0=");
client.print(AnalogValue);
client.print("&value1=");
client.print(456);
client.println(" HTTP/1.0");
client.println("host: www.servername.com");
client.println("user-agent: arduino/somethingorother");
client.println();

I am using a POST with this exact setup, and nothing in the HTTP spec says the http request has to be in one packet... (that I can find...)

My arduino has been contacting my server (IIS though) every 15 seconds for the last month without any of what you are seeing, so I know it works.

You may also want to put a protocol monitor in the middle or on the server- see what the arduino is actually speaking. I know that helped with my setup and testing.

Spinlock, you're a star. Exactly correct.....I changed the syntax to HTTP/1.0 from HTTP/1.1 and it works!

You've saved me hours of head scratching, appreciate it.

i wand to do a similar thing but i dont know how to do this.
is there some example code?