posted data not read by php script

Hopefully someone will be kind enough to tell me what i am doing wrong.

I have a sketch that connects to my server, and is supposed to POST the variable to my php script. In my console everything looks to be working properly, I see that it connects and I see the data that should be posting

 if (client.connect("www.mywebhost.com",80)) 
            {
              Serial.println("connected");
              client.println("POST /Arduino/sendmail.php HTTP/1.1");
              client.println("Host: 123.123.123.123");
              client.println("Content-Type: application/x-www-form-urlencoded");
              client.println("Connection: close");
              client.println();
              client.print(data);
              client.println();

Here is the part of the sketch that connects to the server and supposedly post the data.

this is the php script that collects the posted data.

    $posteddata = $_POST['data'];

now i know that the php script is working because i created another script and submitted the post to the php script and it worked just fine. it just does not work when posted from the arduino

any thoughts? let me know if i need to add some more information.

What does data contain?

Also, if you use HTTP/1.1 you cannot end a POST request with a new line, Content-Length must also be sent in the header ( length of 'data'/POST data ).

Respond using HTTP/1.0 if easier.

I have written a URI/Percent encoder so you could send your request as a GET easily: http://forum.arduino.cc/index.php?topic=202264.msg1490639#msg1490639

I also have a Base64 encoder which can be useful for POST & GET requests: http://forum.arduino.cc/index.php?topic=201007.msg1481810#msg1481810

the data variable is just a number, basically if a sensor reads a certain value it is to send the sensor number to the web page so the web page can send an email.

the differences between HTTP 1.0 and 1.1 are kind of vague to me, as I cannot seem to find any good information describing how to use them.

I have everything but the actual sending of the value to the page.

I have tested the php on the receiving side and that works just fine if the data is received from another page.

basically I don’t understand how to format the GET or POST command to work properly.

i would like to make it so i dont have to change the code for every sensor, so that i could have

client.println("GET /Arduino/sendmail.php?data='data' HTTP/1.0");

but now your talking URL encoding and and data length.

Sorry, but I am more of a PHP/MySQL guy and haven’t tried passing data with just html very much in the past.

Any help getting the sensor data, which could be up to say 5 digits long to the ?Arduino/sendmail.php script would be appreciated.

You need to have the same name/value pairs for POST data too. Using the Print library you can convert/output integers/floats as text.

I would use GET as you don't have the same security requirements as a browser where its preferable to use POST to hide the data from users viewing the URL. If you have more complex text ( not a-z, 0-9 ) you can send it with the help of the URI encoder. Binary data can be converted to text first using the base64 encoder. You do not need encoding on simple name value pairs that are just letters and numbers.

GET is easy to debug using log files ( can see data passed ), once complete converting to POST is an option. With POST you should make sure never to use ( or use carefully ) the PHP $_REQUEST global as someone could override POST data with GET style values in a URL. Stick with the $_GET and $_POST global's.

To begin with, I think this should be a simple comparison of a GET vs POST.

Get:

client.print( "GET /Arduino/sendmail.php?data=" );
client.print( data, DEC );
client.println( " HTTP/1.0" );
client.println( "Connection: close" );

Post:

client.println("POST /Arduino/sendmail.php HTTP/1.0");
client.println("Host: 123.123.123.123");
client.println("Content-Type: application/x-www-form-urlencoded");
client.print("Connection: close\r\n\r\ndata=");
client.print( data, DEC );

Here is the request for comments document regarding the HTTP/1.1 protocol: http://www.w3.org/Protocols/rfc2616/rfc2616.html

PyRo,
Thanks for the help, i am still doing something wrong.
I tried reading that HTTP protocol document before, but really don’t get it. It seems to give a lot of information, but little in the way of examples.

as for the examples you provided, apparently I have been mixing my GET and POST a bit, but your examples still do not work.
I am going to post the full code, minus the ip address for post the sketch and php, maybe you can see what I am doing wrong.

Here is the sketch

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

byte mac[] = { 0x90, 0xA2, 0xDA, 0x0E, 0xA3, 0x0A };
EthernetClient client;


int mSensor = 0;
int mValue;
int id = 1;
LiquidCrystal lcd(9, 8, 7  , 6, 5, 3);

void setup()
{
Serial.begin(9600);
Ethernet.begin(mac);
delay(1000);

        lcd.begin(16, 2);
        lcd.setCursor(0, 0);
        lcd.println("Network Address: ");  // This is sent to serial, and shows correctly 
        lcd.setCursor(2, 1);        // reset cursor position
          for (byte thisByte = 0; thisByte < 4; thisByte++) 
            {
              // print the value of each byte of the IP address:
              Serial.print(Ethernet.localIP()[thisByte], DEC);
              lcd.print(Ethernet.localIP()[thisByte], DEC);
              Serial.print(".");
              lcd.print(".");
            }
}

void loop()
  {
    mValue = analogRead(mSensor); // Read the sensor variable
    Serial.print("Sensor Reads ");
    Serial.println(mValue);
    int data = id;
      if (mValue >=10)
        {
          if (client.connect("www.mydomain.com",80)) 
            {
              client.print( "GET /Arduino/sendmail.php?data=" );
              client.print( data, DEC );
              client.println( " HTTP/1.0" );
              client.println( "Connection: close" );

              //Prints your post request out for debugging
              Serial.print( "GET /Arduino/sendmail.php?data=" );
              Serial.print( data, DEC );
              Serial.println( " HTTP/1.0" );
              Serial.println( "Connection: close" );
               delay(10000);
            }

          
          if (client.connected()) 
            {
              Serial.println();
              Serial.println("disconnecting.");
              client.stop();
              delay(2000);
          }
  
        }
      else
        {
          delay(5000);
        }  
  }

and here is the PHP script that receives the get request. remember I posted the GET variable from another page and it works correctly.

<?php
define('INCLUDE_CHECK',true);
require 'config.php';
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<?php
	$postedid = $_GET['data'];
	$query = ("SELECT * FROM arduino WHERE iD = '$postedid' ") or die(mysql_error());
	$result = mysql_query($query);
	$row = mysql_fetch_array($result);
	
	
	
	$email = $row['email'];
		if($row['iD'] == $postedid)
			{
				$to = $row['email'];
				$subject = "Test mail";
				$message = "Hello! This is a test email message.";
				$from = "me@mydomain.com";
				$headers = "From:" . $from;
				mail($to,$subject,$message,$headers);


				echo $postedid;
				echo $row['email'];
			}
?>
</body>
</html>

Let me know if you see what I am doing wrong because it still does not seem to make sense.

I'll have a read through in a sec, but can you verify using the server logs what URL is sent to the webserver ( with the GET data in it ) was what you are expecting to be sent.

EDIT: you will still need the Host portion if your webserver doesn't have a static IP.

I am actually not sure how to read the server logs as it is a hosted server. Been looking through the directories and found a logs folder, but no logs in it.

i mean other then on the cpanel logs, which shows nothing for the last few days.

Yeah the cpanel logs, if your arduino is talking to the server, there will be log entries. It logs the request, which will contain the query string ( the custom data after the ? ).

Mine has a folder called access-logs above the public_html directory, however I just use the cpanel interface. If there are no entries then you bet the Arduino isn't connecting at all.

Correct, no entries from the arduino

Try the ethernet DnsWebClient example. I tested it to connect to my hosted server.
I have attached the code connecting to my server so you can try it if you like to ensure your arduino can access the net.
Then just change the parts for your server and verify too.

Notice I set the server name variable to my address, and also sent it as part of the request using “Host: xxx”, without it you receive the hosts 404 page.

If this works, it will return the time 10 years ago ( http://arduino.genx.biz/time/?t=-10year&f )

/*
  DNS and DHCP-based Web client
 
 This sketch connects to a website (http://www.google.com)
 using an Arduino Wiznet Ethernet shield. 
 
 Circuit:
 * Ethernet shield attached to pins 10, 11, 12, 13
 
 created 18 Dec 2009
 by David A. Mellis
 modified 9 Apr 2012
 by Tom Igoe, based on work by Adrian McEwen
 
 */

#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[] = "arduino.genx.biz";


// 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() {
 // Open serial communications and wait for port to open:
  Serial.begin(9600);
   while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }


  // 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 /time/?t=-10year&f HTTP/1.0");
    client.println("Host: arduino.genx.biz");
    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);
  }
}

I will try that now, the odd thing is the serial monitor states it is connecting.

connecting...
connected
GET /Arduino/sendmail.php?data=1 HTTP/1.0
Connection: close

Looks like that worked, not sure as to why it is not actually making a connection to my server. here's the serial monitor output

connecting...
connected
HTTP/1.1 200 OK
Server: nginx
Date: Mon, 16 Dec 2013 00:25:26 GMT
Content-Type: text/html
Content-Length: 26
Connection: close
Vary: Accept-Encoding
X-Powered-By: PHP/5.3.27
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache
Set-Cookie: PHPSESSID=e160d9f9390dc5561fa77672d902478a; path=/

In your last post, is the response from my server, or yours ( looks like mine because I use cache & vary headers )?

This is the response from mine, the highlighted stuff is the response from my php page, did you not receive that ( if tried )?
Also the “Connection: close” is usually last, in the middle of the headers seems strange.

connecting…
connected
HTTP/1.1 200 OK
Server: nginx
Date: Mon, 16 Dec 2013 00:29:09 GMT
Content-Type: text/html
Content-Length: 26
Vary: Accept-Encoding
X-Powered-By: PHP/5.3.27
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache
Set-Cookie: PHPSESSID=7c1dea6b50f307af08e2fd673660f4f0; path=/
X-Cache: MISS from tx22rrpep2ab
Connection: close

December 16, 2003, 8:29 am
disconnecting.

That was the response from your code.

I cut and pasted directly into the IDE and uploaded.

Man, this is frustrating. I don't understand why this is so hard.

I want to post a sensor number to a php script. if server is "www.xyz.com" and the page named sendmail.php is located int the sub-folder Arduino "Arduino/sendmail.php"

the variable 'data' = the sensor id (say sensors 1 - 100)

I have the sketch detecting if a sensor gets a reading, but not sure as to why it will not post or get that variable to the server. how would you write the get request to connect to "www.xyz.com" and send the variable data (in this case the number 1) to the page specified?

if needed I can send a PM with the actual server address to you, I just don't want it public at the moment.

Sorry, was at work. If you like, send the PM and I'll test it for you, working all day tomorrow so I may not respond till almost the same time as this post. I have a bit of spare time now, so I'll take your latest code here and I'll try it out.

I appreciate any help you can give, i am about to pull my hair out.

If anyone else can help out with this it would be greatly appreciated.

I haven’t received your PM yet, but just make sure you do have the Host portion in the headers you send, I mentioned it in reply#5 however you were quick to respond and might not have seen my edit.

I noticed it wasn’t in your code posted in reply#4 last night, but you do have it in the initial post. Hopefully this is all that is missing.

Its required for servers like mine ( and most probably yours ) which are hosted with dynamic addresses. The main server host has the static IP and it forwards connections onto our virtual hosts. You probably can buy a static IP ( I can if needed ), which would remove the need for a host section.

You can see the dynamic-ness using my server: http://genx.biz/cpanel/ << Shows misconfiguration as its pointing to my server host rather than my sites virtual server.