OK to keep link open?

Hi -

I am using PHP to process data which I uploading from a controller using a GET conversation. At the moment, the client software opens the link to the server, sends the data, then closes the link again. As data is likely to be sent every few seconds, it would seem to be more efficient to simply open the link on startup and leave it open indefinitely. Is there any obvious problem with this?

Before anyone asks :wink: hereā€™s the code (not my own) I am using:

void SendToServer(String genin) {
  WiFiClient clientGet;
  const int httpGetPort = 80;
  const char* hostGet = "xxxxxx.com"; 
  String urlGet = "/CHrxlog.php";

  urlGet += "?src=ESP&typ=flt&logentry=" + genin;
  Serial.print(">>> Connecting to host: ");
  Serial.println(hostGet);
  
  if (!clientGet.connect(hostGet, httpGetPort)) {
    Serial.print("Connection failed: ");
    Serial.print(hostGet);
  } 
  else {
    clientGet.println("GET " + urlGet + " HTTP/1.1");
    clientGet.print("Host: ");
    clientGet.println(hostGet);
    clientGet.println("User-Agent: ESP8266/1.0");
    clientGet.println("Connection: close\r\n\r\n");
      
    unsigned long timeoutP = millis();
    while (clientGet.available() == 0) {
        
      if (millis() - timeoutP > 10000) {
        Serial.print(">>> Client Timeout: ");
        Serial.println(hostGet);
        clientGet.stop();
        return;
      }
    }

    //just checks the 1st line of the server response. Could be expanded if needed.
    while(clientGet.available()){
      String retLine = clientGet.readStringUntil('\r');
      Serial.println(retLine);
      break; 
    }

  } //end client connection if else
                    
  Serial.print(">>> Closing host: ");
  Serial.println(hostGet);
      
  clientGet.stop();
}

and this is a simple test PHP program running on the server:

    <!DOCTYPE html>
    <html>
    <body>
    <?php
      $var1 = $_GET['logentry'];

      $fileContent = $var1."\n";

      $fileStatus = file_put_contents('logfile.txt',$fileContent,FILE_APPEND);
      if($fileStatus != false) {
        echo "SUCCESS: data written to file";
      }
      else {
        echo "FAIL: could not write data to file";
      }
    ?>
    </body>
    </html>

The first section of code is Java. With it being a compiled language, it is designed to just keep continuously running without interference. There is no benefit to keeping the connection open. You want the handshake every time it sends data over to not tie anything up with resources.

The second portion just writes whatever is sent to append a file. You arenā€™t opening and closing anything, though I would refactor that PHP code and make it an API that just runs, without any html.

The first is C++ actually, but I take your point. What was concerning me was whether the time taken to open a channel, send the data and then close the channel again would be significantly more than just keeping the channel open and sending the data as it occurred.

Yes, the second portion was just to check that I could receive the data and write it to a file. This will probably be modified to write to a MySQL database - once I have worked out how to do it! As a relative newcomer to PHP, Iā€™m not clear about ā€œrefactor that PHP codeā€. I see what youā€™re getting at, but Iā€™m not sure how to run that on a server without embedding it in HTML.

I skipped over the char* pointer!

Iā€™ve used this in the past. Just set up the routes, include the database configuration to be passed in and have the client make an http request to the url.

Sponsor our Newsletter | Privacy Policy | Terms of Service