Hazrdous function of PHP POST

Hello,

I try to set up home automation by Arduino.

Local controlers by Arduino : OK :slight_smile:
Transfert of data to main Arduino controler with ethernet shield by I2C : OK :slight_smile:
TCP serveur for datadispath on local network : OK :slight_smile:
Creation of Mysql database on my website : OK :slight_smile:
Transfert of data from main Arduino controler to database using PHP Post : works every 2 to 5 times

to check I use a arduino programm that sends a Post every second to write in database a counter value that is increased at each write operation, to check if I lose messages, and I loose about 75%.

If I use same PHP code in index.php and I lauch the webside, every update of website a lign is created in database even if i push update of the webpage every second or faster.
So the connection from Arduino seem haeardous ???

someone can help me ?

my arduino code :

#include <Ethernet.h>

byte mac[] = { 0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x02 };
String data;
int No = 0;
int h;

IPAddress ip(192, 168, 0, 125);
IPAddress myDns(212, 27, 40, 240);
IPAddress gateway(192, 168, 0, 254);
IPAddress subnet(255, 255, 255, 0);

EthernetClient client;

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

  Ethernet.begin(mac,  ip, myDns, gateway, subnet);
  
  ip = Ethernet.localIP();
  // print your local IP address:
  Serial.print("My IP address: ");
  for (byte thisByte = 0; thisByte < 4; thisByte++) {
    // print the value of each byte of the IP address:
    Serial.print(ip[thisByte], DEC);
    Serial.print(".");
  }
  Serial.println();
 }

void loop() {

  data = "temp1=";
  data +=  No ;
  data +=    "&hum1=";
  data +=    19;

  if (client.connect("www.steinhilber.eu", 80)) { // REPLACE WITH YOUR SERVER ADDRESS
    Serial.println("connecte a steinhilber.eu");
    client.println("POST /add.php HTTP/1.1");
    client.println("Host: steinhilber.eu"); // SERVER ADDRESS HERE TOO
    client.println("Content-Type: application/x-www-form-urlencoded");
    client.print("Content-Length: ");
    client.println(data.length());
    client.println();
    client.print(data);
    No += 1;
  } else {
    Serial.println("not connected to steinhilber.eu");
  }

  if (client.connected()) {
    client.stop();	
    Serial.println("disconnected");
  }
  
  Ethernet.maintain();
  
  delay(1000);
}


[CODE] 

and here is PHP code in add.php file located on server

[CODE]

<?php 

   echo '<p>Index : Lancement index</p>'; 
   
   $server="steinhiltearmin.mysql.db";
   $user="steinhiltearmin";
   $pass="password";
   $db="steinhiltearmin";
      
    $temp1=$_POST["temp1"];
    $hum1=$_POST["hum1"];

    $connection = mysqli_connect($server, $user, $pass, $db);
    echo mysqli_connect_errno($connection);
    echo '<p>lancer querry</p>';

    mysqli_query($connection,"INSERT INTO `tempLog` (`temperature`, `humidity`) VALUES (".$temp1.",".$hum1.")");
     echo '<p>fin querry</p>';
     mysqli_close($connection);
        

     echo '<p>Index : sql terminee</p>'; 
    
?>

[CODE]

try a delay before disconnecting the client.
look in server logs.

Brilliant

I added a '' delay(2000); '' before ''client.stop(); ''
And now it works perfectly !!!

thanks very much !!!

Is there a way to disconnect in a controlled way once the action is finished ?

Best regards

Armin

you should have a close header in request. the server then should close the connection after reading all the data. but servers tend to hold the connection. it is better to free (close) the connection on the arduino side after small delay fort he server to read the data

      client.println(F("Connection: close"));
      client.println(F("Cache-Control: no-store"));