Hello All:
I'm trying to use the Sparkfun ESP8266 Shield to read data from a simple webpage. My long term goal is to be able to use php on a webserver to receive data from the Arduino (which will have sensors attached) and then the php on the server can send very simple pages back (for example, with the word "stop" which the arduino could detect with a fairly simple "if" statement) to perform some action.
I'm trying to work on baby steps, so my first goal was to read a simple webpage and print it to the serial monitor. No code to actually understand the web page is needed. I just want to read it and print it.
I'm mostly using the demo code from the shield with a few edits. I can't just use the demo code because it points to "example.com" and someone stopped paying the bill on that site
Here's some excerpts for what I think is causing the problem:
const char destServer[] = "sugarbushdata.com";
const String httpRequest = "GET /index.php HTTP/1.1\n"
"Host: sugarbushdata.com\n"
"Connection: close\n\n";
void clientDemo()
{
// To use the ESP8266 as a TCP client, use the
// ESP8266Client class. First, create an object:
ESP8266Client client;
// ESP8266Client connect([server], [port]) is used to
// connect to a server (const char * or IPAddress) on
// a specified port.
// Returns: 1 on success, 2 on already connected,
// negative on fail (-1=TIMEOUT, -3=FAIL).
Serial.println(destServer);
int retVal = client.connect(destServer, 80);
if (retVal <= 0)
{
Serial.println(F("Failed to connect to server."));
Serial.println(retVal);
return;
}
// print and write can be used to send data to a connected
// client connection.
client.print(httpRequest);
// available() will return the number of characters
// currently in the receive buffer.
while (client.available())
Serial.write(client.read()); // read() gets the FIFO char
// connected() is a boolean return value - 1 if the
// connection is active, 0 if it's closed.
if (client.connected())
client.stop(); // stop() closes a TCP connection.
}
What I get is a 400- your browser sent a request that this server could not understand.
It's printed out pretty nicely (ok, actually fairly ugly cause its the html code) on my serial monitor.
If I navigate to www.sugarbushdata.com I get my nice simple page.
(PS- I really want to read "index.php" off of the server because my eventual goal is to do the server-side processing with php. However, I stuck an index.html page up there just to make sure that wasn't the issue. I can't get either one of them to work.
I'm going to stick the whole code in the attatchments in case the problem is somewhere else in the file, but I'm pretty sure its an issue with what I'm sending the server...
THANK YOU in advance for your help. I've been tinkering with this for 2 days and I'm out of places to turn. Help me, Arduino.cc forum, you're my only hope
TestWifiConnectbroken.ino (6.62 KB)