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