Go Down

Topic: Resolved: Problem sending data using PHP to a server (Read 685 times) previous topic - next topic

nrbelk

Feb 27, 2012, 03:05 am Last Edit: Feb 28, 2012, 01:54 am by nrbelk Reason: 1
I am trying to send some hard-coded data to a server a friend set up for me.

The following will enter in a value under a certain name:

http://xxxxx.xxxxxx.com/track.php?type=temp&user=nathan&value=98.6

There is a page I can check to see if it entered the database properly.  This string works from my browser but when I try to use it with my arduino code, I can't get it to work.

For now, I have just been modifying one of the examples, the ethernet DNSWebClient example.  I can get my arduino to respond as expected when I run the sample code, but when I change it to the values I need, it doesn't work.

Here is the code:

Code: [Select]

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

byte mac[] = {  0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x02 };
char serverName[] = "xxxx.xxxxx.com";

// Initialize the Ethernet client library
// with the IP address and port of the server
// that you want to connect to (port 80 is default for HTTP):
EthernetClient client;

void setup() {
 // start the serial library:
 Serial.begin(9600);
 // start the Ethernet connection:
 if (Ethernet.begin(mac) == 0) {
   Serial.println("Failed to configure Ethernet using DHCP");
   // no point in carrying on, so do nothing forevermore:
   while(true);
 }
 // give the Ethernet shield a second to initialize:
 delay(1000);
 Serial.println("connecting...");

 // if you get a connection, report back via serial:
 
 if (client.connect(serverName, 80)) {
   Serial.println("connected");
   // Make a HTTP request:
   client.println("GET /track.php?type=temp&user=nathan&value=98.6 HTTP/1.0");
   client.println();
 }
 else {
   // kf you didn't get a connection to the server:
   Serial.println("connection failed");
 }
}

void loop()
{
 // if there are incoming bytes available
 // from the server, read them and print them:
 if (client.available()) {
   char c = client.read();
   Serial.print(c);
 }

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

   // do nothing forevermore:
   while(true);
 }
}


Any ideas what could be wrong?  

Thanks


SurferTim

Maybe the server uses virtual hosting?
Code: [Select]
  if (client.connect(serverName, 80)) {
    Serial.println("connected");
    // Make a HTTP request:
    client.println("GET /track.php?type=temp&user=nathan&value=98.6 HTTP/1.0");
    client.println("Host: xxxxx.xxxxxx.com");
    client.println();
  }

PaulS

Quote
Any ideas what could be wrong? 

Are there log files on the server that you could look at?

nrbelk

I'm not sure if we can get access to the log files.  My friend's site (he made a sub domain for me on his site) is hosted by dreamhost.

Is there a way I could get the arduino to print out what errors its getting that prevents it to connect?

If it does use virtual hosting, the client.println(Host: xxx.xxxx.com will fix it?

Thanks

SurferTim

If it does use virtual hosting, the client.println(Host: xxx.xxxx.com will fix it?

Thanks


Yes, probably.  :)

nrbelk

One side note of information, when I just test with the example code, connecting to google, it stays connected for a long time.  But when I try connected to my friends site, if it does connect, it looks like it gets disconnected quickly or doesn't connect at all.

SurferTim

You are not waiting for the server to respond. In your loop(), try this code instead.
Code: [Select]
// if there are incoming bytes available
// from the server, read them and print them:
  while(client.connected())
  {
    // stay in this loop until the server closes the connection
    // that is the signal it is finished sending
    while(client.available()) {
      char c = client.read();
      Serial.write(c);
    }
  }

nrbelk

Thanks all for the help!

Using a combination of what was suggested on this site and what I found here:

http://www.jfkreuter.com/?p=9

I was able to get it to work.

I'm not exactly sure what fixed it but it now works like a charm.

Here is the code that worked (with some serial.prints to help troubleshoot and determine where I was in the code while it was executing).


Code: [Select]
#include <SPI.h>
#include <Ethernet.h>

// Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
byte mac[] = {  0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x02 };
char serverName[] = "xxx.xxxxx.com";

// Initialize the Ethernet client library
// with the IP address and port of the server
// that you want to connect to (port 80 is default for HTTP):
EthernetClient client;

void setup() {
  // start the serial library:
  Serial.begin(9600);
  // start the Ethernet connection:
  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    // no point in carrying on, so do nothing forevermore:
    while(true);
  }
  // give the Ethernet shield a second to initialize:
  delay(1000);
  Serial.println("connecting...");

  // if you get a connection, report back via serial:
 
  if (client.connect(serverName, 80)) {
    Serial.println("connected");
    // Make a HTTP request:
    client.println("GET /track.php?type=temp&user=nathan&value=120.6 HTTP/1.1");
    client.println("Host: xxx.xxxxx.com");
    client.println("User-Agent: Arduino");
    client.println("Accept: text/html");
    Serial.println("Sent stuff to server");
    client.println();
  }
  else {
    // kf you didn't get a connection to the server:
    Serial.println("connection failed");
  }
}

void loop()
{
  // if there are incoming bytes available
  // from the server, read them and print them:

   while(client.connected())
  {
    // stay in this loop until the server closes the connection
    // that is the signal it is finished sending
    Serial.print("Printing Available Data to Serial");
    Serial.println();
    while(client.available()) {
      char c = client.read();
      Serial.write(c);
    }
  }


  // if the server's disconnected, stop the client:

  if (!client.connected()) {
    Serial.println();
    Serial.println("disconnecting.");
    client.stop();

    // do nothing forevermore:
    while(true);
  }
}

Go Up