I'm trying to connect my WiFly Shield (http://www.sparkfun.com/products/9954) to a server I set up. I connected a button to pin 2 so that it sends the request when I push the button. I also have an LED set up on pin 3 to show when the connection is open.
I think I'm very close but missing something probably trivial. The shield connects to my wifi network and successfully sends out the "GET" command. I even copied the user agent from my MacBook so that the request looks exactly the one from the browser and yet I get a "400 Bad Request."
This is the Apache log for the request from Arduino:
XXX.XXX.XXX.XXX - - [08/Mar/2012:03:06:54 +0000] "GET /hit/on/id HTTP/1.1" 400 304 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_8) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.45 Safari/535.19"
However, if I type the URL into the browser, I get this:
XXX.XXX.XXX.XXX - - [08/Mar/2012:03:08:02 +0000] "GET /hit/on/id HTTP/1.1" 200 19 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_8) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.45 Safari/535.19"
XXX.XXX.XXX.XXX - - [08/Mar/2012:03:08:02 +0000] "GET /favicon.ico HTTP/1.1" 200 3101 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_8) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.45 Safari/535.19"
The requests look identical and yet the responses are different. What am I missing? Here's my code:
// (Based on Ethernet's WebClient Example)
#include "WiFly.h"
#include "Credentials.h"const int buttonPin = 2;
const int ledPin = 3;String url_hit = "";
WiFlyClient client("mydomain.com", 80);
void setup()
{
pinMode(buttonPin, INPUT);
pinMode(ledPin, OUTPUT);Serial.begin(115200);
WiFly.begin();if (!WiFly.join(ssid, passphrase))
{
Serial.println("Association failed.");
// Hang on failure.
while (1) {}
}url_hit = "GET /hit/on/id HTTP/1.1";
}void loop()
{
if(digitalRead(buttonPin))
{
Serial.println(getHTML(url_hit));
}
}String getHTML(String url)
{
String html = "";
Serial.print("Connecting...");
if (client.connect())
{
Serial.println(" OK");
digitalWrite(ledPin, HIGH);client.println(url);
client.println("User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_8) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.45 Safari/535.19");
client.println();while(client.connected())
{
if (client.available())
{
char c = client.read();
html += c;
}
}Serial.println("\nDisconnecting.");
digitalWrite(ledPin, LOW);
client.stop();
}
else
{
Serial.println(" Connection failed");
}
return html;
}
Any help would be greatly appreciated.
-George