ARDUINO WEB CLIENT

I have a simple project posting data to a PHP page for storage in a database, however nothing seems posted though am getting a HTTP feedback .

ARDUINO CODE

int data=100;
unsigned long lastConnectionTime = 0;             // last time you connected to the server, in milliseconds
const unsigned long postingInterval = 10L * 1000L; // delay between updates, in milliseconds
void setup() 
{
  Serial.begin(9600);
   delay(1000); 
   Serial.println("setting IP.....");
//    if (Ethernet.begin(mac) == 0)
//    {
      Serial.println("dhcp failed!!!");
  Ethernet.begin(mac, ip, gateway, subnet);
//   }
  // print the Ethernet board/shield's IP address:
  Serial.print("My IP address: ");
 //Serial.println(ip);
  Serial.println(Ethernet.localIP()); 
}
void loop() {
  
  // if there's incoming data from the net connection.
  // send it out the serial port.  This is for debugging
  // purposes only:
  delay(6000);
  if (client.available()) {
    char c = client.read();
    Serial.println("cc ");
    Serial.write(c);
     Serial.println(" ");
    Serial.println("cc 2");
  }

  // if ten seconds have passed since your last connection,
  // then connect again and send data:
  if (millis() - lastConnectionTime > postingInterval)
  {
    httpRequest();
    }

}

// this method makes a HTTP connection to the server:
void httpRequest() {
  // close any connection before send a new request.
  // This will free the socket on the WiFi shield
  client.stop();
delay(24000);
  // if there's a successful connection:
  if (client.connect(server, 80)) {
    Serial.println("connected");
    
    client.println("POST /berberseat/dataget.php HTTP/1.1");
    client.println("Host: 192.168.137.1");
    client.print(adult);
    client.println("User-Agent: arduino-ethernet");        
    client.print(data);
    Serial.println("data posted!");
    client.println("Connection: close");
    
    

    // note the time that the connection was made:
    lastConnectionTime = millis();
  } else {
    // if you couldn't make a connection:
    Serial.println("connection failed");
  }
}

connectivity to the server: below is the Apache access log

192.168.137.22 - - [17/Jan/2018:20:25:15 +0300] "POST /berberseat/dataget.php HTTP/1.1" 200 508 "-" "-"
192.168.137.22 - - [17/Jan/2018:20:25:16 +0300] "Connection: close" 400 979 "-" "-"

PHP page to get data: Works perfect when accessed from a browser, though not from Arduino:

<?php

if (isset($POST['adult'])){
$adult = $_POST['adult']; 
}


		/* $Sename=$_POST['NAME'];	   */ 
				   
				   include 'dbconnect.php';
				   
					 $sql='INSERT INTO seat SET
							adult=:adult';
							
							
				
								
					$s=$pdo->prepare($sql);	
								$s->bindValue(':adult', $_POST['adult']);
								/* $s->bindValue(':total_time', $_POST['total_time']);
								$s->bindValue(':activity_start', $_POST['activity_start']);
								$s->bindValue(':activity_stop', $_POST['activity_stop']);
								$s->bindValue(':berber-identity', $_POST['berber-identity']);
								$s->bindValue(':outlet_number', $_POST['outlet_number']); */
								
								
								$s->execute();											
								
					
				  	
			   header('Location: http://192.168.137.1/berberseat/');
 
   	
   




?>

Apparently my Arduino code is not acting right. Anyone with a suggestion?

    client.println("POST /berberseat/dataget.php HTTP/1.1");
    client.println("Host: 192.168.137.1");
    client.println("User-Agent: arduino-ethernet");       
    client.println("Connection: close");
    client.println(); // empty line delimiting th request headers from the request body
    client.print(data);

@Juraj

Please put your answer in context.

Posting code without explanation only helps people learn to copy & paste. :slight_smile:

there is a comment in the code

Juraj:

    client.println("POST /berberseat/dataget.php HTTP/1.1");

client.println("Host: 192.168.137.1");
   client.println("User-Agent: arduino-ethernet");      
   client.println("Connection: close");
   client.println(); // empty line delimiting th request headers from the request body
   client.print(data);

That's not going to work:

RFC7230§3.3.2:
A user agent SHOULD send a Content-Length in a request message when
no Transfer-Encoding is sent and the request method defines a meaning
for an enclosed payload body. For example, a Content-Length header
field is normally sent in a POST request even when the value is 0
(indicating an empty payload body).

Pieter