Pages: [1]   Go Down
Author Topic: Resolved: Problem sending data using PHP to a server  (Read 556 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 8
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
#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

« Last Edit: February 27, 2012, 07:54:36 pm by nrbelk » Logged

Miramar Beach, Florida
Offline Offline
Faraday Member
**
Karma: 138
Posts: 5833
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Maybe the server uses virtual hosting?
Code:
  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();
  }
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Any ideas what could be wrong? 
Are there log files on the server that you could look at?
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 8
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Miramar Beach, Florida
Offline Offline
Faraday Member
**
Karma: 138
Posts: 5833
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Thanks

Yes, probably.  smiley
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 8
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Miramar Beach, Florida
Offline Offline
Faraday Member
**
Karma: 138
Posts: 5833
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You are not waiting for the server to respond. In your loop(), try this code instead.
Code:
// 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);
    }
  }
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 8
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
#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);
  }
}
Logged

Pages: [1]   Go Up
Jump to: