Go Down

Topic: Hazrdous function of PHP POST (Read 612 times) previous topic - next topic



I try to set up home automation by Arduino.

Local controlers by Arduino : OK  :)
Transfert of data to main Arduino controler with ethernet shield by I2C : OK  :)
TCP serveur for datadispath on local network : OK  :) 
Creation of Mysql database on my website : OK  :)
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 :

Code: [Select]

#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() {

  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);

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: ");
    No += 1;
  } else {
    Serial.println("not connected to steinhilber.eu");

  if (client.connected()) {


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



echo '<p>Index : Lancement index</p>'

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

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

'<p>Index : sql terminee</p>'



try a delay before disconnecting the client.
look in server logs.
You can't write an Arduino sketch if you didn't learn programming. Not the language, but the concepts of programming - algorithms and data types.



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



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

Code: [Select]

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

You can't write an Arduino sketch if you didn't learn programming. Not the language, but the concepts of programming - algorithms and data types.

Go Up