HTTP GET Method Fails

Any assistance is greatly appreciated. I've researched and tried all of the relevant examples I could find on this forum with no success. I hope someone can point me in the right direction.

Submitting a HTTP GET from my Arduino fails.

Here's some background:

  1. Installed latest Arduino and WiFi shield firmware
  2. The sample HTTP GET client example works perfectly (GET submit to google.com returns query results in Serial Monitor)
  3. The public facing website (a PGP page) which the GET is being submitted works perfectly when submitted from a HTML page or parameters are passed in the URL

So, based on the above facts I assume:

  1. The Arduino and WiFi are successfully connected to the LAN and the Internet
  2. The Website PGP page functions correctly

I think the issue is with the syntax of the HTTP client forming the GET request. I've tried many different combinations of the 'client.print (GET...' command.

I get no response back from the 'client.available()'

Here's my code:

if ( status != WL_CONNECTED) {
Serial.println("Couldn't get a wifi connection");
while(true);
}
else {

Serial.println("WiFi Connected");

delay(1000);

if (client.connect(server,80))

{
client.print("GET http://www.*/index.php?mytemp=84&myhumid=79&mywindspeed=0&mywind_direction=NW&mybarometric=15&mydate=2015-06-21&mytime=1200&myrainfall=0&mylocation=");
client.println(" HTTP/1.1");
client.println("Host: thejarretts.net");
client.println();

}else{
Serial.println("Connection to server failed");
}

while (client.available()) {
char c = client.read();
Serial.println(c);
}

// if the server's disconnected, stop the client:
if (!client.connected()) {
Serial.println();
Serial.println("Not connected...disconnecting from server.");
client.stop();
}

Serial.println("End of setup function");

// do nothing forevermore:
Serial.println("Ending");
while (true);
}

I think the issue is with the syntax of the HTTP client forming the GET request.

And you are spot on.

client.print("GET http://www.*********/index.php?mytemp=84&myhumid=79&mywindspeed=0&mywind_direction=NW&mybarometric=15&mydate=2015-06-21&mytime=1200&myrainfall=0&mylocation=********");
        client.println(" HTTP/1.1");

The GET request is sent to a server. The ONLY protocol that the Arduino supports is http, so that is NOT part of the GET request. The server has already been connected to, so that part is NOT part of the GET request.

Try again without the "http://www.*********" in the GET request.

Thanks for the quick response. The web site is still not responding to the GET submit from my Arduino. I believe there is something wrong with my 'GET synatx' or server connection. The serial monitor returns the following when executing the code further below:


serial monitor


WiFi Connected
Connected. Executing GET

Not connected...disconnecting from server.
End of setup function
Ending


my code


if (client.connect(server,80))

{
Serial.println("Connected. Executing GET");
client.print("GET ?mytemp=84&myhumid=79&mywindspeed=0&mywind_direction=NW&mybarometric=15&mydate=2015-06-21&mytime=1200&myrainfall=0&mylocation=Eidson");
client.println(" HTTP/1.1");
client.println("Host: *********.net");
client.println();
}else{
Serial.println("!!!!!!!!!!!!!!!!!!!!!!!!!!!");
Serial.println("Connection to server failed");
Serial.println("!!!!!!!!!!!!!!!!!!!!!!!!!!!");
}

while (client.available()) {
char c = client.read();
Serial.println(c);
}

// if the server's disconnected, stop the client:
if (!client.connected()) {
Serial.println();
Serial.println("Not connected...disconnecting from server.");
client.stop();
}
Serial.println("End of setup function");
// do nothing forevermore:
Serial.println("Ending");
while (true);
}

Please edit your post, select the code, and put it between [code][/code] tags.

You can do that by hitting the “Code” icon above the posting area. It is the first icon, with the symbol: </>

How to use this forum

GET ?mytemp

You need the path. eg.

GET /?mytemp

Any suggestions are greatly appreciated....


Same results in Serial Monitor


WiFi Connected
Connected. Executing GET

Not connected...disconnecting from server.
End of setup function
Ending


Here's the code:


     if (client.connect(server,80)) 
     
     {       
       Serial.println("Connected.  Executing GET");
       client.print("GET /?mytemp=84&myhumid=79&mywindspeed=0&mywind_direction=NW&mybarometric=15&mydate=2015-01-21&mytime=1200&myrainfall=0&mylocation=Eidson");
       client.println(" HTTP/1.1");
       client.println("Host: thejarretts.net");
       client.println();
     }else{
         Serial.println("!!!!!!!!!!!!!!!!!!!!!!!!!!!");
         Serial.println("Connection to server failed");
         Serial.println("!!!!!!!!!!!!!!!!!!!!!!!!!!!");
       }
       
   while (client.available()) {
   char c = client.read();
   Serial.println(c);
 }

 // if the server's disconnected, stop the client:
 if (!client.connected()) {
   Serial.println();
   Serial.println("Not connected...disconnecting from server.");
   client.stop();
 }
     if (client.connect(server,80))
     {      
       Serial.println("Connected.  Executing GET");
...
       }
   while (client.available()) {
   char c = client.read();
   Serial.println(c);
  }

You aren't giving the server time to react. You are banging out the message, seeing if something is available a millisecond later, and if not, giving up. The example web client code in the IDE does not do that. Please post all your code.

Basic client test code you can try unmodified to see if you can get a response from the dyndns server. If this works then your LAN setup is probably ok.

//zoomkat 3-1-13
//simple client checkip test
//for use with IDE 1.0.1 or later
//with DNS, DHCP, and Host
//open serial monitor and send an e to test
//for use with W5100 based ethernet shields

#include <SPI.h>
#include <Ethernet.h>

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address

char serverName[] = "checkip.dyndns.com"; // test web page server
EthernetClient client;

//////////////////////

void setup(){

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

  Serial.begin(9600); 
  Serial.println("Better client ip test 3/1/13"); // so I can keep track of what is loaded
  Serial.println("Send an e in serial monitor to test"); // what to do to test
}

void loop(){
  // check for serial input
  if (Serial.available() > 0) //if something in serial buffer
  {
    byte inChar; // sets inChar as a byte
    inChar = Serial.read(); //gets byte from buffer
    if(inChar == 'e') // checks to see byte is an e
    {
      sendGET(); // call sendGET function below when byte is an e
    }
  }  
} 

//////////////////////////

void sendGET() //client function to send/receive GET request data.
{
  if (client.connect(serverName, 80)) {  //starts client connection, checks for connection
    Serial.println("connected");
    client.println("GET / HTTP/1.0"); //download text
    client.println("Host: checkip.dyndns.com");
    client.println(); //end of get request
  } 
  else {
    Serial.println("connection failed"); //error message if no client connect
    Serial.println();
  }

  while(client.connected() && !client.available()) delay(1); //waits for data
  while (client.connected() || client.available()) { //connected or data available
    char c = client.read(); //gets byte from ethernet buffer
    Serial.print(c); //prints byte to serial monitor 
  }

  Serial.println();
  Serial.println("disconnecting.");
  Serial.println("==================");
  Serial.println();
  client.stop(); //stop client

}

Thank you all for the suggestions.

I changed the code in my program to hit the “checkup.dyndns.com” site and added two delay statements. It returns a bunch of HTML text as did the “google.com” example.

(zoomkat - I’m using an WiFi shield and couldn’t figure out how to pass the mac parameter using the begin function of the WiFi class. Not important for this topic but maybe for someone another day.)

I replaced the “checkup” site with my original website and replaced the inadvertently deleted index.php back in the path. The program doesn’t get a response back from the host or update the site, but doesn’t error either. As requested, I’ve included all of my code further down. I also included the host index.php scrip and a simple HTML form that submits a GET successfully to the site.

I have access to the server logs (Go Daddy hosted), but as so eloquently stated earlier in this topic, " really don’t know what I’m doing…" so any assistance is greaty appreciated.
*****************
Serial Monitor Output
*****************
Attempting to connect to WPA network…
Firmware=1.1.0
SSID: xxxxxx
IP Address: 192.168.1.126
WiFi Connected
Connected. Executing GET
End of setup function
Ending
***********
Arduino Code
***********
```
*#include <WiFi.h>
#include <WiFiClient.h>

#include <WiFiUdp.h>

#include <SPI.h>

#include <Wire.h>
;
WiFiClient client;
char server = “xxxxxxxxx”;

void setup()
{
  Serial.begin(9600);

char ssid = “xxxxxx”;    //  your network SSID (name)
char pass = “xxxxxxxxxx”;    // your network password
int status = WL_IDLE_STATUS;    // the Wifi radio’s status

// attempt to connect using WPA2 encryption:
  Serial.println(“Attempting to connect to WPA network…”);
  status = WiFi.begin(ssid, pass);

//WiFi LAN info for trouble shooting

Serial.println(“Firmware=”);
  Serial.println(WiFi.firmwareVersion());
 
    // print the SSID of the network you’re attached to:
  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());

// print your WiFi shield’s IP address:
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);
//*/

delay(10000);

// if you’re not connected, stop here:
  if ( status != WL_CONNECTED) {
    Serial.println(“Couldn’t get a wifi connection”);
    while(true);
  }
  else {
 
    Serial.println(“WiFi Connected”);

delay(10000);

if (client.connect(server,80))
     
      {     
        Serial.println(“Connected.  Executing GET”);
        client.print(“GET /index.php/?mytemp=84&myhumid=79&mywindspeed=0&mywind_direction=NW&mybarometric=15&mydate=2015-01-21&mytime=1200&myrainfall=0&mylocation=xxxxx”);
        client.println("/ HTTP/1.1");
        client.println(“Host: xxxxxxxxx”);
        client.println(); //end of get request
      }
      else{
          Serial.println("!!!");
          Serial.println(“Connection to server failed”);
          Serial.println("!!!");
        }

delay(10000);
       
    while (client.available()) {
    char c = client.read();
    Serial.println(c);
  }

// if the server’s disconnected, stop the client:
  while(client.connected() && !client.available()) delay(1); //waits for data
  while (client.connected() || client.available()) { //connected or data available
    char c = client.read(); //gets byte from ethernet buffer
    Serial.print(c); //prints byte to serial monitor

}
    Serial.println(“End of setup function”);
    // do nothing forevermore:
  Serial.println(“Ending”);
  while (true);
}

Wire.begin();
}*
* _*********_ *index.php* _*********_ *
*

<?PHP //database connection variables $servername = "xx.xx.xx.xx"; $username = "xxxxxxxxxxx"; $password = "xxxxxxxxxxx"; $dbname = "xxxxxxxxxxxxx"; //weather variables from GET $location = $_GET['mylocation']; $temperature = $_GET['mytemp']; $humidity = $_GET['myhumid']; $datein = $_GET['mydate']; $timein = $_GET['mytime']; $windspeed = $_GET['mywindspeed']; $wind_direction = $_GET['mywind_direction']; $barometric = $_GET['mybarometric']; $rainfall = $_GET['myrainfall']; //create DB connection $conn = new mysqli($servername, $username, $password, $dbname); //verify connection if ($conn->connect_error) { die("connection failed: " . $conn->connect_error); } $sql = "INSERT INTO Weather (Barometric, Date, Humidity, Location, Rainfall, Temperature, Time, Windspeed, Wind_direction) VALUES ($barometric, '$datein', $humidity, '$location', $rainfall, $temperature, '$timein', $windspeed, '$wind_direction')"; if ($conn->query($sql) === TRUE) {     echo "New weather record created successfully"; } else {     echo "Error: " . $sql . " " . $conn->error; } $conn->close(); ?>*

* _****************_ *Update HTML page* _****************_ *
*

My Temp:

My Humidity:

My Wind Speed:

My Wind Direction

My Barometric Pressure

My Date

My Time

My Rainfall

Location:

* *```*

I mistyped the path (again)- corrected path returns same results as stated above
.


corrected path


client.print("GET /index.php?mytemp=84...."
        client.print("GET /index.php/?mytemp=84&myhumid=79&mywindspeed=0&mywind_direction=NW&mybarometric=15&mydate=2015-01-21&mytime=1200&myrainfall=0&mylocation=xxxxx");

Why is there now a / after the script name, before the '?'?

Perhaps a simpler script that took ONE argument...

What are you expecting the PHP script to return? I see nothing in it that is going to return anything.

Where is html page being used? It has nothing to do with the Arduino making a GET request.

Thank you for spotting that. I removed the superfluous forward slash located after the script name, but it still didn't work.

However, per your suggestion, I removed all but one of the parameters knowing the host script (actually SQL Server) should throw an error and it did. This is the first response - good or bad - I've been able to coax out of the host using the Arduino.

I'll dig into the parameter formatting.

index.php takes parameters submitted by the GET. It connects to a SQL database and inserts a record. If it succeeds, it will return “New weather record created successfully”. If it fails, it will return the error message generated by the server.

The html page was a simple form for testing index.php. It has nothing to do with the Arduino. I included it for due diligence and so we could see what parameters were expected.

Until now, I wasn’t getting any message back. I suspect a hidden character or my error is malforming the parameter string.

Until now, I wasn't getting any message back. I suspect a hidden character or my error is malforming the parameter string.

The echo statement is a good one to master. Get (a lot) more out of the script.

You can also make the script log data to a file on the PC. Looking at the log files on the server might also give a clue. Is the server running Apache?

Thank you for the suggestions. I'll start using echo and logging is a good idea.

The server is Windows running IIS 8.0 hosted by Go Daddy.

The GET line should really finish with " HTTP/1.1".

Thank you Nick.

Like so?

"GET /index.php?param1=a&param2=b HTTP/1.1"

Does adding the space after the last parameter value somehow get delimited so it is not confused as part of the value for the last parameter?

Here's what I have so far (it's still not working but at least I'm getting something back from the server I can work with)

"GET /index.php?param1=a&param2=b / http/1.1"

After adding a couple of Delay() and correcting the GET syntax, the program works nearly 100% of the time with two parameters. When I add a third parameter, the program works about 50% of the time. Adding four or more, it usually doesn't work.

"The program works" is defined as the php script / GET returns the success message - a record is inserted into the database by the php script.

When it fails, there is no error message - or anything at all - returned from the host. it simply ends here:

    while (client.available()) {
    char c = client.read();
    Serial.println(c);
  }

  // if the server's disconnected, stop the client:
   while(client.connected() && !client.available()) delay(1); //waits for data
  while (client.connected() || client.available()) { //connected or data available
    char c = client.read(); //gets byte from ethernet buffer
    Serial.print(c); //prints byte to serial monitor 

  }
    Serial.println("End of setup function");
    // do nothing forevermore:
   Serial.println("Ending");
   while (true);

I've tried reordering the fields in the script and in the GET statement to see if there is one or more fields creating an issue in the script or in the database, but with no success. Once I try more than two parameters, the success rate plummets.

Thoughts?

I'm headed out of town for a few days and wont get back to this for a week or more. Thanks for your help...I made a lot of progress.

This is probably why. This becomes an endless loop if the server stalls or the connection breaks. I'm surprised it doesn't print a bunch of "funny y" characters. I don't see a client.stop call in your code either. That will cause the loss of a socket.

  while (client.connected() || client.available()) { //connected or data available
    char c = client.read(); //gets byte from ethernet buffer
    Serial.print(c); //prints byte to serial monitor
  }

Try this client code. It has the correct code to send a request and wait for a response, or time out if no response is returned.
http://playground.arduino.cc/Code/WebClient

Change the sprintf line in loop to your page and add your variables. Insure that the pageAdd array is large enough to hold your request string.