I am wanting to eliminate all Delays in my code.
I have a function in my main loop that accepts some data, uploads it to my web site ( php page that adds data to an online log file ), and then closes the connection.
From experience, I have found that I have to add a short delay ( in this example, a delay of 250 ms ) before client.stop so that the preceding code completes fully to the web based php page. The code at present is working as required.
void loop(void){
unsigned long currentMillis = millis();
void LogItWeb(int Location, float LogTxt){
if (DSLcount > 0){ // if there was a previous failure, then wait 10 loops before trying again
DSLcount++;
if (DSLcount > 10) DSLcount = 0; // now try again
}
if (DSLcount == 0){
DSLcount = 1; // if the following connect fails, then DSLcount will be 1 and activate the 10 loop delay.
if (client.connect(myserver, 80)) {
DSLcount = 0; // if the connect succeeds, then set the DSLcount to 0, so no delay loop next time
client.print("GET http://www.xxx.com/xxx.php?data=");
client.print(Location);
client.print("--");
client.print(LogTxt);
client.println(" HTTP/1.1");
client.println("Host: www.xxx.com");
client.println();
delay(250);
client.stop();
}
}
}
LogItWeb(1,tempC2); // tempC2 contains a float value from a sensor reading
} // end of loop
Would it work to remove the client.stop to outside the function like this ? :
Is the function LogItWeb able to change the value of variable ClientStopMillis for use outside the function ?
void loop(void){
unsigned long currentMillis = millis();
// add a new variable
long ClientStopMillis = 0;
void LogItWeb(int Location, float LogTxt){
if (DSLcount > 0){ // if there was a previous failure, then wait 10 loops before trying again
DSLcount++;
if (DSLcount > 10) DSLcount = 0; // now try again
}
if (DSLcount == 0){
DSLcount = 1; // if the following connect fails, then DSLcount will be 1 and activate the 10 loop delay.
if (client.connect(myserver, 80)) {
DSLcount = 0; // if the connect succeeds, then set the DSLcount to 0, so no delay loop next time
client.print("GET http://www.xxx.com/xxx.php?data=");
client.print(Location);
client.print("--");
client.print(LogTxt);
client.println(" HTTP/1.1");
client.println("Host: www.xxx.com");
client.println();
// set the Stop time as the current millis + 250 ms
ClientStopMillis = currentMillis + 250;
// delay(250);
// client.stop();
}
}
}
// add this code to the general section of the Main loop
if(ClientStopMillis != 0 && ClientStopMillis <= currentMillis){
client.stop();
ClientStopMillis = 0;
}
LogItWeb(1,tempC2); // tempC2 contains a float value from a sensor reading
} // end of loop