Hi,
I wonder what does mean the little circking icon on chrome when clicking on actualise.
If it does mean that chrome is sending a request, how can I prevent it from stopping.
JPD
How about
const long timeoutTime = 600000;
or taking this off the HTML page
client.println(“Connection: close”); ?
JPD
Hi Jean.
I’m not sure I understand your issue.
Are you following any particular project? Can you better explain your issue?
As I’ve explained here, Google Chrome makes an extra connection that we should close after a few seconds: https://rntlab.com/question/chrome-makes-an-extra-connection/ I don’t know why that happens.
But if you’ve build your web servers using the ESPAsyncWebServer library, you won’t have this problem.
Regards,
Sara
Hi,
You can open the image.
There are extensions on Chrome like Keepalive, Keepawake that does this.
Lets say The ESP32 server wakw at 6AM. Then there is a 11 minutes delay for probing, then another 8 minutes delay for probing again before the data are ready to be sent in a html page.
Yesterday the web page appeared 23 minutes after resetting the esp32.
If the web page stop requesting information at the time the server is sending them , the whole thing sucks.
If a web page is opened for the url, but the little icon does not rolling, when the server will send the information to the browser, will it (the browser) catch the information?
Regards,
JP
Hi.
I can’t open the image.
To share an image, share a link to google drive, dropbox or imgur.
Regards,
Sara
Hi,
Right click on it and choose open in another tab.
It is a link to dropbox. I dont know why it does not work.
Hi again.
Why do you have this:
const long timeoutTime = 600000;?
Can you show me your code?
Otherwise, it is very difficult to find out what might be wrong.
Regards,
Sara
I dont have this. It is question of if could use that to keep the browser asking for data.
1 /** 2 https://rntlab.com/question/solved-esp32-web-server-drops-connection-crashes/ 3 * */ 4 5 6 #include "esp_sleep.h" 7 #include "esp_deep_sleep.h"//obsolete 8 #include "FS.h" 9 #include "SPIFFS.h" 10 #include <time.h> 11 #include <WiFi.h> 12 #include <WiFiUdp.h> //For NTP Server 13 #include <DHT_U.h> 14 #include "DHT.h" 15 #include <NTPClient.h> 16 #include <stdlib.h> /* atoi */ 17 #include <stdio.h> /* printf, fgets */ 18 #include <String.h> 19 #line 15 "DHT_Ruis_SantosV1.9"// (this ligne number +1) __LINE__ "DHT_Ruis_SantosV1.9" 20 21 // Define NTP Client to get time 22 WiFiUDP ntpUDP; 23 NTPClient timeClient(ntpUDP); 24 WiFiServer server(80); 25 26 //#define MY_DEBUG 1 27 //#if defined MY_DEBUG 28 // printf("\n------:line__%d__ ", lineNumb); 29 // Serial.print(what); 30 //#endif 31 32 #define Threshold 40 /* Greater the value, more the touch sensitivity */ 33 #define DHTTYPE DHT22 // DHT 22 (AM2302) 34 #define uS_TO_S_FACTOR 1000000ULL //for sleep only 35 #define HOURS 60 //(7200 = 120 minutes) 43200 secondes , soit 12 heures 36 #define TIME_TO_SLEEP_MAX 80ULL // MAX Time to sleep in case of repeatable unsuccessfull connection attempts 37 #define TIME_TO_SLEEP_CASE1 5400ULL //(15min = 900s) (1.5h = 5400s) in if the time is 11:00 - 12:00 38 #define TIME_TO_SLEEP_CASE2 70200ULL //3h (4h = 14400) (20h = 72000s)if time isn't 11:00 - 12:00 39 //#define DAY_PERIOD_START 360 //(6h =360min)(12h =720min)in minutes the morning 40 //#define DAY_PERIOD_END 660 //(11h =660min)(17h =1020 )(20h =1200min) in minutes later 41 #define DAY_PERIOD_START 360 //(6h =360min)(12h =720min)in minutes the morning 42 #define DAY_PERIOD_END 660 //(11h =660min)(17h =1020 )(20h =1200min) in minutes later 43 44 unsigned long timeDiff, timeDiff2;//RTC_DATA_ATTR 45 int TIME_TO_SLEEP_WiFi = 10; ////RTC_DATA_ATTR Time to sleep in case of unavailable WiFi (This is the first sleep) 46 47 long calcul5 = 0.0; 48 // Replace with your network credentials 49 const char* ssid = "23JANVIER-2012_Network_1"; 50 const char* password = "cvq1-3bhy-afp6"; 51 //ipconfig 189.143.0.107 52 //Passerelle par défaut : 189.143.0.1 53 //wroom mac address 24:6f:28:17:f9:50 54 55 // Variables to save date and time 56 long timezone = -5; 57 uint8_t daysavetime = 1; 58 struct tm tmstruct ; 59 unsigned long currentMillis; 60 const unsigned long period = 10000; //period that esp32 will try to connect with WIFI in ms 61 String formattedDate; 62 String timeStamp; 63 64 RTC_DATA_ATTR const int relay = 26; //pin dixième piton relais 65 RTC_DATA_ATTR uint8_t dryRedAlarm = 0; 66 // arrosage 2hrs + pluie 70 67 uint8_t pinToRead = 36; 68 // DHT Sensor 69 const int DHTPin = 16; 70 // Initialize DHT sensor. 71 DHT dht(DHTPin, DHTTYPE); 72 73 static char celsiusTemp[7]; 74 static char fahrenheitTemp[7]; 75 static char humidityTemp[7]; 76 77 char dayBuff[160]; 78 char clientReadBuff[160]; 79 char atoiBuff1[3]; 80 char atoiBuff2[3]; 81 82 int datCnt, maxCount = 100; 83 int decCnt = ( maxCount - 1); 84 RTC_DATA_ATTR uint8_t flag = 0, lastFlag = 0; 85 RTC_DATA_ATTR uint8_t usedFlagNum[100]; 86 RTC_DATA_ATTR int minut1Cp = -1; 87 RTC_DATA_ATTR int minut2Cp = 0; 88 int charcount = 0; 89 int val; 90 int bootCount = 0; 91 int heureNormal = 3600; //-3600 pour l'heure avancée 92 double totalDelay = 0, webPageTime = 0; 93 RTC_DATA_ATTR uint8_t newLinesCnt = 0; 94 uint8_t newLinesCnt2, newLinesCnt3, noClient = 1; 95 float hic; 96 String aNewLine;// previousNewline; 97 String datArray[150]; // our data array holding 30 elements 98 String newLinesArray[40]; 99 String newLinesArray2[40]; 100 //=== 101 // Current time 102 unsigned long currentTime = millis(); 103 // Previous time 104 unsigned long previousTime = 0; 105 // Define timeout time in milliseconds (example: 2000ms = 2s) 106 const long timeoutTime = 2000; 107 //=== 108 109 void delayFunction(long long secondes) { 110 unsigned long long previousMillis; 111 112 secondes = secondes * 1000000; 113 previousMillis = micros(); 114 while ((micros() - previousMillis) < secondes) { 115 analogRead(pinToRead); 116 } 117 } 118 119 int capacitiveProbe(uint8_t readings, long delays, uint8_t foroncer) { 120 unsigned long long previousMillis; 121 int totVal = 0, val = 0; 122 uint8_t oncer = foroncer; 123 uint8_t turns = 0; 124 125 while (turns < readings) { 126 if (oncer < 1) { 127 //lets the device warm up 5 minutes 128 analogRead(pinToRead); 129 previousMillis = millis(); 130 while ((millis() - previousMillis) < 300000) {//5 min 131 ; 132 } 133 //Serial.println("dummy reading end"); 134 oncer = 1; 135 } 136 137 val = analogRead(pinToRead); //connect sensor to Analog 11 or else 138 previousMillis = millis(); 139 while ((millis() - previousMillis) < delays) { 140 ; 141 } 142 if (isnan(val)) { 143 Serial.println("Failed to read from probe."); 144 return 0; 145 } 146 147 val = map(val, 2450, 1615, 0, 100); 148 totVal += val; 149 //printf("%d %d\n", val, totVal); 150 turns++; 151 } 152 printf("dryRedAlarm %d <--> %d (totVal/readings)\n", dryRedAlarm, (totVal / readings)); 153 return (totVal / readings); 154 } 155 156 void ampoule(int probe) { 157 158 if (probe < dryRedAlarm) { 159 digitalWrite(relay, HIGH); 160 printf("Ampoule allumée.\n"); 161 //while Function tant que val < dryRedAlarm 162 //capacitiveProbe(1, 120.0, 1)// 6E7 = 1000 min = 16.6 heures 163 while (probe < dryRedAlarm) { 164 // 1= nombre de fois, 36E2=1 heure, 0=avec réchauffement 165 probe = capacitiveProbe(5, 2000.0, 1); //1 = pas de réchauffement 166 } 167 } 168 169 digitalWrite(relay, LOW); 170 printf("Ampoule éteinte.\n"); 171 //printf("ligne_%d humidité du sol: %d\n", __LINE__, w); 172 } 173 174 void print_wakeup_reason() { 175 esp_sleep_wakeup_cause_t wakeup_reason; 176 wakeup_reason = esp_sleep_get_wakeup_cause(); 177 switch (wakeup_reason) 178 { 179 case ESP_SLEEP_WAKEUP_EXT0 : Serial.println("Wakeup caused by external signal using RTC_IO"); break; 180 case ESP_SLEEP_WAKEUP_EXT1 : Serial.println("Wakeup caused by external signal using RTC_CNTL"); break; 181 case ESP_SLEEP_WAKEUP_TIMER : Serial.println("Wakeup caused by timer"); break; 182 case ESP_SLEEP_WAKEUP_TOUCHPAD : Serial.println("Wakeup caused by touchpad"); break; 183 case ESP_SLEEP_WAKEUP_ULP : Serial.println("Wakeup caused by ULP program"); break; 184 default : printf("Wakeup was not caused by deep sleep: %d\n", wakeup_reason); break; 185 } 186 } 187 188 long longMinutStamp() { 189 190 // Variables to save date and time 191 String formattedDate; 192 String dayStamp; 193 String timeStamp, timeStamp1, timeStamp2, timeStamp3; 194 long longMinutStamp; 195 196 while (!timeClient.update()) { 197 timeClient.forceUpdate(); 198 } 199 timeClient.setTimeOffset(-18000 + heureNormal); 200 formattedDate = timeClient.getFormattedDate(); 201 int splitT = formattedDate.indexOf("T"); 202 dayStamp = formattedDate.substring(0, splitT); 203 //timeStamp = formattedDate.substring(splitT + 1, formattedDate.length() - 1); 204 timeStamp1 = formattedDate.substring(splitT + 3, formattedDate.length() - 9); 205 timeStamp2 = formattedDate.substring(splitT + 4, formattedDate.length() - 4); 206 timeStamp3 = formattedDate.substring(splitT + 7, formattedDate.length() - 1); 207 208 longMinutStamp = atoi(timeStamp1.c_str()) * 60; 209 longMinutStamp += atoi(timeStamp2.c_str()); 210 longMinutStamp += atoi(timeStamp3.c_str()) / 60; 211 //returns minutes 212 return longMinutStamp; 213 } 214 215 long timeDiffData(long minut) 216 { //returns minutes difference 217 RTC_DATA_ATTR static long previous; 218 Serial.println("_116 - previous = " + (String)previous); 219 if (previous != 0) 220 { 221 return (minut - previous); 222 } else { 223 previous = minut; 224 return (minut - longMinutStamp()); 225 } 226 } 227 228 String myDate() { 229 // Variables to save date and time 230 String formattedDate; 231 String dayStamp; 232 String timeStamp; 233 String myDate; 234 235 while (!timeClient.update()) { 236 timeClient.forceUpdate(); 237 } 238 timeClient.setTimeOffset(-18000 + heureNormal); 239 240 formattedDate = timeClient.getFormattedDate(); 241 242 int splitT = formattedDate.indexOf("T"); 243 dayStamp = formattedDate.substring(0, splitT); 244 timeStamp = formattedDate.substring(splitT + 1, formattedDate.length() - 1); // 245 246 myDate = dayStamp + " " + timeStamp; 247 //Serial.println(myDate); 248 249 return myDate; 250 } 251 252 void readData() { 253 int datCnt = 0; 254 File f = SPIFFS.open("/gardenTemp.txt", "r"); 255 if (!f) { 256 Serial.println("====== Reading from SPIFFS file FAILED ======="); 257 } else 258 Serial.println("====== Reading from SPIFFS file ======="); 259 260 while (f.available()) { 261 //Lets read line by line from the file 262 String line = f.readStringUntil('\n'); 263 Serial.print(datCnt); 264 Serial.print(" "); 265 Serial.println(line); 266 datCnt ++; 267 } 268 f.close(); 269 Serial.println("====== End reading from SPIFFS file ======="); 270 } 271 272 //============ appendFile Start ================ 273 void appendFile(fs::FS &fs, const char * path, const char * message) { 274 275 File file = fs.open(path, FILE_APPEND); 276 if (!file) { 277 Serial.println("Failed to open file for appending"); 278 return; 279 } 280 if (file.print(message)) { 281 printf("\n%s\n", message); 282 printf("->|__Appended to file: %s\n", path); 283 } else { 284 printf("\nAppend failed: %s\n", message); 285 } 286 file.close(); 287 } 288 289 double dewPoint(double celsius, double humidity) 290 { 291 // (1) Saturation Vapor Pressure = ESGG(T) 292 double RATIO = 373.15 / (273.15 + celsius); 293 double RHS = -7.90298 * (RATIO - 1); 294 RHS += 5.02808 * log10(RATIO); 295 RHS += -1.3816e-7 * (pow(10, (11.344 * (1 - 1 / RATIO ))) - 1) ; 296 RHS += 8.1328e-3 * (pow(10, (-3.49149 * (RATIO - 1))) - 1) ; 297 RHS += log10(1013.246); 298 299 // factor -3 is to adjust units - Vapor Pressure SVP * humidity 300 double VP = pow(10, RHS - 3) * humidity; 301 302 // (2) DEWPOINT = F(Vapor Pressure) 303 double T = log(VP / 0.61078); // temp var 304 return (241.88 * T) / (17.558 - T); 305 } 306 307 //=================== Listing SPIFFS Files ======= 308 void listDir(fs::FS &fs, const char * dirname, uint8_t levels) { 309 310 Serial.println("====== Listing DIR SPIFFS files ======="); 311 printf("Listing directory: %s\n", dirname); 312 313 File root = fs.open(dirname); 314 if (!root) { 315 Serial.println("Failed to open directory"); 316 return; 317 } 318 if (!root.isDirectory()) { 319 Serial.println("Not a directory"); 320 return; 321 } 322 323 File file = root.openNextFile(); 324 while (file) { 325 if (file.isDirectory()) { 326 Serial.print(" DIR : "); 327 Serial.print (file.name()); 328 time_t t = file.getLastWrite(); 329 struct tm * tmstruct = localtime(&t); 330 printf(" LAST WRITE: %d-%02d-%02d\n", (tmstruct->tm_year) + 1900, ( tmstruct->tm_mon) + 1, tmstruct->tm_mday); //,tmstruct->tm_hour , tmstruct->tm_min, tmstruct->tm_sec 331 if (levels) { 332 listDir(fs, file.name(), levels - 1); 333 } 334 } else { 335 Serial.print(" FILE: "); 336 Serial.print(file.name()); 337 Serial.print(" SIZE: "); 338 Serial.print(file.size()); 339 time_t t = file.getLastWrite(); 340 struct tm * tmstruct = localtime(&t); 341 printf(" LAST WRITE: %d-%02d-%02d %02d:%02d:%02d\n", (tmstruct->tm_year) + 1900, ( tmstruct->tm_mon) + 1, tmstruct->tm_mday, tmstruct->tm_hour , tmstruct->tm_min, tmstruct->tm_sec); 342 } 343 file = root.openNextFile(); 344 } 345 Serial.println("====== End listing DIR SPIFFS files ======="); 346 } 347 348 void writeFile(const char * path, const char * message) { 349 File file = SPIFFS.open(path, "w"); 350 if (!file) { 351 Serial.println("Failed to open file for writing" + *path); 352 file.close(); 353 return; 354 } 355 if (file.print(message)) { 356 printf("\nErasing or writing file: %s OK\n", path); 357 } else { 358 Serial.println("Write or erasing failed\n" + *path); 359 } 360 file.close(); 361 } 362 363 void callback() { 364 Serial.println(); 365 Serial.println("Awaken by pin GPIO(4 fourth pin) touch ..."); 366 Serial.println("On the boot button side of the card."); 367 Serial.println(); 368 ESP.restart(); 369 } 370 371 //========================== minuteSort ====================================== 372 373 int minuteSort(String theMinuts) { 374 //Serial.println("inString.toInt()%d inString.toInt()/%d•\n", inString.toInt(),inString.toInt()/2); 375 char atoiBuff[3]; 376 Serial.println("theMinuts•" + theMinuts + "•"); 377 printf("_%d 0=%c\n", __LINE__, theMinuts.charAt(0)); 378 printf("_%d 1=%c\n", __LINE__, theMinuts.charAt(1)); 379 printf("_%d 2=%c\n", __LINE__, theMinuts.charAt(2)); 380 381 if (theMinuts.charAt(0) == 48) { 382 atoiBuff [0] = theMinuts.charAt(1); 383 atoiBuff [1] = '\0'; 384 atoiBuff [2] = '\0'; 385 } else { 386 atoiBuff [0] = theMinuts.charAt(0); 387 atoiBuff [1] = theMinuts.charAt(1); 388 atoiBuff [2] = '\0'; 389 } 390 int minutCp5 = atoi(atoiBuff); 391 printf("0= %c\n", atoiBuff [0] ); 392 printf("1= %c\n", atoiBuff [1] ); 393 printf("2= %c\n", atoiBuff [2] ); 394 //printf("_%d- atoiBuff [0], atoiBuff [1], atoiBuff [2], atoiBuff)\n•%c•%c•%c•%c\n", __LINE__, atoiBuff [0], atoiBuff [1], atoiBuff [2], *atoiBuff); 395 return minutCp5; 396 } 397 398 void soundAlert() { 399 digitalWrite(relay, HIGH); 400 printf("Ampoule allumée.\n"); 401 delayFunction(.5); 402 digitalWrite(relay, LOW); 403 printf("Ampoule éteinte.\n"); 404 } 405 //========================== END ====================================== 406 407 //==================== SETUP BEGIN ==================================== 408 void setup() { 409 Serial.begin(115200); 410 while (!Serial) { 411 ; // wait for serial port to connect. Needed for native USB port only 412 } 413 414 //Setup interrupt on Touch Pad 3 (GPIO15) 415 touchAttachInterrupt(T3, callback, Threshold); 416 esp_sleep_enable_touchpad_wakeup();//Configure Touchpad as wakeup source 417 418 // initialize the DHT sensor 419 dht.begin(); 420 /*------------------------------------------------------------------------------------------------------*/ 421 //Print the wakeup reason for ESP32 422 print_wakeup_reason(); 423 WiFi.begin(ssid, password); 424 while (WiFi.status() != WL_CONNECTED) { 425 currentMillis = millis(); //Assign the ms have been passed since ESP32 code start running 426 Serial.println("Boot number: " + String(++bootCount)); 427 if (currentMillis > period) { 428 WiFi.disconnect(); 429 esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP_WiFi * uS_TO_S_FACTOR); 430 Serial.println("Wifi to sleep now for " + String(TIME_TO_SLEEP_WiFi) + " Seconds" + " Due to WiFi unavailability"); 431 432 while (TIME_TO_SLEEP_WiFi < TIME_TO_SLEEP_MAX) { // While the sleep time is less than the predefined limit keep double the sleep time. When the sleep time exceeds the limit then keep a constant sleep time equal to the limit. 433 TIME_TO_SLEEP_WiFi = TIME_TO_SLEEP_WiFi * 2; // Double the sleep time 434 esp_deep_sleep_start(); 435 } 436 437 TIME_TO_SLEEP_WiFi = TIME_TO_SLEEP_MAX; // Set the sleep time equal to the limit 438 esp_deep_sleep_start(); 439 } 440 delayFunction(1); 441 } 442 /*-----------------------------------------------------------------------------------------------------*/ 443 server.begin(); 444 Serial.print("Connected to "); 445 Serial.print(ssid); 446 Serial.println(" network"); 447 Serial.println(WiFi.macAddress()); 448 Serial.println(WiFi.RSSI()); 449 Serial.print("IP address: "); 450 Serial.println(WiFi.localIP()); 451 //if a temp file has already been in use, open it and load it's data to array. 452 SPIFFS.begin(); 453 if (!SPIFFS.begin()) { 454 Serial.println(" Not SPIFFS.begin()"); 455 } 456 //listDir(SPIFFS, "/", 0); 457 //erase file 458 //if (flag == 0)writeFile("/gardenTemp.txt", '\0'); 459 File f = SPIFFS.open("/gardenTemp.txt", "r"); 460 if (!f) { 461 Serial.println("file open failed"); 462 } 463 Serial.println("====== Setup Start reading from SPIFFS file ======="); 464 while (f.available() && datCnt < maxCount) { 465 //Lets read line by line from the file 466 String line = f.readStringUntil('\n'); 467 if (line != 0) { 468 if (datCnt > 9) { 469 Serial.print(datCnt); 470 Serial.print(" "); 471 Serial.println(line); 472 datArray[datCnt] = line; 473 } else { 474 Serial.print(0); 475 Serial.print(datCnt); 476 Serial.print(" "); 477 Serial.println(line); 478 //line = "0" + line; 479 datArray[datCnt] = line; 480 } 481 datCnt ++; 482 } 483 } 484 f.close(); 485 Serial.println("====== Setup End reading from SPIFFS file =======\n"); 486 /*-----------------------------------------------------------------------------------------------------*/ 487 timeClient.begin(); // Initialize a NTPClient to get time 488 // Set offset time in seconds to adjust for your timezone, for example: 489 timeClient.setTimeOffset(-18000 + heureNormal); //Cyprus Time Zone GMT -5 = -18000 490 491 pinMode(relay, OUTPUT); 492 /*-----------------------------------------------------------------------------------------------------*/ 493 // 1= nombre de fois, 36E2=1 heure, 0=avec réchauffement 494 Serial.println(myDate()); 495 Serial.print("NTP • ");//Client initialized 496 printf("+11 minutes for probing.\n"); 497 //int a = capacitiveProbe(3, 120000.0, 0);//0=réchauffe, 5min + 6 min //--- 498 totalDelay += 11; 499 //ampoule(a); //--- 500 Serial.println("setup end " + myDate()); 501 soundAlert(); 502 Serial.println("page web should be open in 19 minutes."); 503 } 504 //======================= SETUP END ==================================== 505 506 void loop() { 507 508 printf(" webPageTime = %4.1f\n", webPageTime); 509 printf(" totalDelay = %4.1f\n", totalDelay); 510 printf("loop()=>IF newLinesCnt#%d\n", newLinesCnt); 511 printf(" flag#%d at Line_%d\n", flag, __LINE__); 512 513 /*----------------------------------------------*/ 514 if (flag != 0) { //flag > lastFlag && (flag % 3) == 0, && (flag < 2) 515 flag = 0; 516 //lastFlag = flag; 517 webPageTime = 0; 518 totalDelay = 0; 519 //printf("\nIn [CASE1 CASE2] Line__%d__\n", __LINE__); 520 while (!timeClient.update()) { 521 timeClient.forceUpdate(); 522 } 523 // The formattedDate comes with the following format: 524 long long _Entre_Deux = longMinutStamp(); 525 long long calcul3 = _Entre_Deux; 526 long long calcul4 = 0.0; 527 //Serial.println(millis()); 528 //printf(" Between START & END %04lld \n", _Entre_Deux); 529 _Entre_Deux = _Entre_Deux - DAY_PERIOD_START; 530 //printf("-%d DAY_PERIOD_START %04lld \n" , DAY_PERIOD_START, _Entre_Deux); 531 calcul4 = calcul3 - DAY_PERIOD_END; 532 printf("-%d DAY_PERIOD_START_END %04lld\n", DAY_PERIOD_END, calcul4); 533 534 /*----------------------------------------------*/ 535 // Serial.println("________ newLinesArray _________"); 536 // Serial.println("0 " + newLinesArray[0]); 537 // Serial.println("1 " + newLinesArray[1]); 538 // Serial.println("2 " + newLinesArray[2]); 539 // Serial.println("3 " + newLinesArray[3]); 540 // Serial.println("4 " + newLinesArray[4]); 541 // Serial.println("5 " + newLinesArray[5]); 542 // Serial.println("________ newLinesArray _________"); 543 /*----------------------------------------------*/ 544 //Serial.println(myDate()); 545 printf("\n In for (newLinesArray[i])┐\n"); 546 for (int i = newLinesCnt ; --i >= 0 ;) { 547 Serial.print((String)i + " "); 548 Serial.println(newLinesArray[i]); 549 } 550 printf("Out for (newLinesArray[i])┘ \n"); 551 552 printf("newLinesCnt#%d flag#_%d Line_%d", newLinesCnt, flag, __LINE__); 553 webPageTime = 0; 554 totalDelay = 0; 555 556 if ((calcul3 >= DAY_PERIOD_START) && (calcul3 <= DAY_PERIOD_END)) { 557 totalDelay = (TIME_TO_SLEEP_CASE1 / 60.0); 558 Serial.println(myDate()); 559 printf("\n->CASE1-> Going to sleep for %3.0f minutes\n" , (TIME_TO_SLEEP_CASE1 / 60.0)); 560 //client.stop(); 561 esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP_CASE1 * uS_TO_S_FACTOR); 562 esp_light_sleep_start(); //esp_deep_sleep_start();// 563 } else { 564 newLinesCnt2 = (newLinesCnt - 1); 565 newLinesArray[newLinesCnt2].toCharArray(dayBuff, 130); 566 const char *result = dayBuff; 567 printf("result %s\n", result); 568 appendFile(SPIFFS, "/gardenTemp.txt", result); 569 Serial.println(myDate()); 570 printf("_%d ->CASE2-> Going to sleep for %6.0f minutes\n", __LINE__, (TIME_TO_SLEEP_CASE2 / 60.0)); 571 flag = 0; //deep sleep reset variables to 0 572 totalDelay = (TIME_TO_SLEEP_CASE2 / 60.0); 573 newLinesCnt = 0; 574 //client.stop(); 575 esp_deep_sleep_pd_config(ESP_PD_DOMAIN_RTC_PERIPH, ESP_PD_OPTION_OFF); 576 esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP_CASE2 * uS_TO_S_FACTOR); 577 //Serial.flush(); 578 esp_deep_sleep_start(); 579 } 580 } 581 /*----------------------------------------------*/ 582 printf("flag#%d before WiFiClient client at line#%d\n", flag, __LINE__); 583 Serial.println(myDate()); 584 delayFunction(12); 585 totalDelay += 0.2; 586 // listen for incoming clients 587 WiFiClient client = server.available(); 588 589 if (client) { 590 //currentTime = millis();//=== 591 //previousTime = currentTime;//=== 592 memset(clientReadBuff, 0, sizeof(clientReadBuff)); 593 charcount = 0; 594 // an http request ends with a blank line 595 boolean currentLineIsBlank = true; 596 Serial.println("------ char c start---------"); 597 //while (client.connected() && currentTime - previousTime <= timeoutTime) {//=== 598 //currentTime = millis();//=== 599 while (client.connected()) { 600 if (client.available()) { 601 char c = client.read(); 602 Serial.write(c); 603 //read char by char HTTP request 604 clientReadBuff[charcount] = c; 605 if (charcount < sizeof(clientReadBuff) - 1) charcount++; 606 // if you've gotten to the end of the line (received a newline 607 // character) and the line is blank, the http request has ended, 608 // so you can send a reply 609 //================================================================ 610 if (c == '\n' && currentLineIsBlank) { 611 Serial.println("-------- char end ---------"); 612 // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor) 613 float h = dht.readHumidity(); 614 // Read temperature as Celsius (the default) 615 float t = dht.readTemperature(); 616 double dew = dewPoint(t, h); 617 // Read temperature as Fahrenheit (isFahrenheit = true) 618 float f = dht.readTemperature(true); 619 // Check if any reads failed and exit early (to try again). 620 if (isnan(h) || isnan(t) || isnan(f)) { 621 Serial.println("Failed to read from DHT sensor!"); 622 strcpy(celsiusTemp, "Failed"); 623 strcpy(fahrenheitTemp, "Failed"); 624 strcpy(humidityTemp, "Failed"); 625 } 626 else 627 { 628 // Computes temperature values in Celsius + Fahrenheit and Humidity 629 hic = dht.computeHeatIndex(t, h, false); 630 dtostrf(hic, 6, 2, celsiusTemp); 631 float hif = dht.computeHeatIndex(f, h); 632 dtostrf(hif, 6, 2, fahrenheitTemp); 633 dtostrf(h, 6, 2, humidityTemp); 634 printf("+8 minutes for probing.\n"); 635 totalDelay += 8; 636 webPageTime += 8; 637 val = 12;//capacitiveProbe(3, 60000, 0);//8 minutes //--- 638 aNewLine = ""; 639 aNewLine += "\n"; 640 aNewLine += myDate(); 641 aNewLine += "--> " + (String)val + "% Eau dans le sol Température: " ; 642 aNewLine += (String)t + " °C Humidité: " + (String)h; 643 aNewLine += "% Ressentie: " + (String)hic + " °C Rosé " + (String)dew + "%" ; 644 } 645 646 printf("-aNewLine size %d->¬\n", aNewLine.length()); 647 Serial.println(aNewLine); 648 649 int firstNum = 1 + aNewLine.indexOf(':'); 650 int secondNum = 2 + firstNum; 651 //Serial.println("firstNum " + (String)firstNum + " secondNum " + (String)secondNum); 652 String minutSubstr = aNewLine.substring(firstNum, secondNum); 653 minut2Cp = minuteSort(minutSubstr); 654 //printf("line_%d -newLinesCnt %d minut1Cp•%d != minut2Cp•%d ?\n", __LINE__, newLinesCnt, minut1Cp, minut2Cp); 655 //delayFunction(60); //1 minute 656 657 if (minut1Cp != minut2Cp) { 658 newLinesArray[newLinesCnt] = aNewLine; 659 minut1Cp = minut2Cp; 660 usedFlagNum[newLinesCnt] = flag; 661 newLinesCnt++; 662 printf("newLinesCnt #%d Line_%d\n", newLinesCnt, __LINE__); 663 } 664 Serial.println("\n------ end newLinesArray -------------"); 665 for (int i = (newLinesCnt - 1) ; i >= 0 ; i--) { 666 printf("%d• ", i); 667 Serial.print(newLinesArray[i]); 668 } 669 Serial.println("------------- begin newLinesArray -------------"); 670 Serial.println("\n------------- end spiffDatas ------------"); 671 for (int spiffDatas = decCnt; spiffDatas >= 0; spiffDatas--) 672 { 673 if (datArray[spiffDatas] != 0) 674 { 675 Serial.println(datArray[spiffDatas]); 676 } 677 } 678 Serial.println("------------- begin spiffDatas ------------"); 679 680 // send a standard http response header 681 client.println("HTTP/1.1 200 OK"); 682 client.println("Content-Type: text/html"); 683 //client.println("Content-Type: image/gif"); 684 client.println("Connection: close"); // the connection will be closed after completion of the response 685 client.println(); 686 client.println("<!DOCTYPE HTML><html lang=\"fr\"><head><meta charset=\"UTF-8\"><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">"); 687 client.println("<meta http-equiv=\"refresh\" content=\"30\"></head>"); 688 client.println("<body style=\"color:#f3d6e1;\"><div style=\"font-size: 3.5rem;color:green;\"><p style=\"text-align:center;\">Météo</p>"); 689 if (atoi(celsiusTemp) >= 25) { 690 client.println("<div style=\"color: #930000;\"><p style=\"text-align:center;\">"); 691 } 692 else if (atoi(celsiusTemp) < 25 && atoi(celsiusTemp) >= 5) { 693 client.println("<div style=\"color: #006601;\"><p style=\"text-align:center;\">"); 694 } 695 else if (atoi(celsiusTemp) < 5) { 696 client.println("<div style=\"color: #009191;\">"); 697 } 698 client.println("<p style=\"text-align:center;font-size:14pt;\">"); 699 client.println(myDate()); 700 client.println("</p><p></p><p style=\"text-align:center;font-size:14pt;\">"); 701 client.println("Page en ligne pour "); 702 client.println((webPageTime + 21)); 703 client.println(" minutes."); 704 client.println("</p><p></p><p style=\"text-align:center;\">"); 705 client.print(celsiusTemp); 706 client.print("°C "); 707 //client.print(fahrenheitTemp); 708 //client.println(" °F</p><p style=\"text-align:center;\">"); 709 client.println("</p><p style=\"text-align:center;\">"); 710 client.print(humidityTemp); 711 client.print("° % Humidité "); 712 client.print(dew); 713 client.println("° Rosé</p><p style=\"text-align:center;\">"); 714 client.print(hic); 715 client.print("° Ressenti "); 716 client.print(val); 717 client.print("%° Humidité du sol</p><p style=\"text-align:center;font-size:12pt;\">"); 718 for (int i = newLinesCnt ; i >= 0 ; i--) { 719 client.print(newLinesArray[i] + "<br>"); 720 } 721 client.print("<br>-------------------------------------------<br>"); 722 for (int spiffDatas = decCnt; spiffDatas >= 0; spiffDatas--) { 723 if (datArray[spiffDatas] != 0) { 724 client.print("<br>" + datArray[spiffDatas] + "<br>"); 725 } 726 } 727 728 client.print("</p></div>"); 729 client.print("</body></html>"); 730 break; 731 } 732 if (c == '\n') { 733 // you're starting a new line 734 currentLineIsBlank = true; 735 memset(clientReadBuff, 0, sizeof(clientReadBuff)); 736 charcount = 0; 737 } else if (c != '\r') { 738 // you've gotten a character on the current line 739 currentLineIsBlank = false; 740 } 741 } 742 } 743 flag += 1; 744 //------------------- 745 Serial.print("page web opened at "); 746 Serial.println(myDate()); 747 webPageTime += 0.5;//1/2min for browser 748 webPageTime += 20; 749 printf("for %3.0f minutes.\n", webPageTime); 750 delayFunction(30); 751 totalDelay += 0.5; 752 // close the connection: 753 client.stop(); 754 totalDelay += 20; 755 noClient = 1; 756 delayFunction(1200);// in seconds 757 //timeDiff = longMinutStamp(); 758 //------------------- 759 } else { 760 delayFunction(30); 761 Serial.println("\n----------\n!client #" + (String)(noClient++)); 762 Serial.println("Is the browser is stopped ?\n"); 763 Serial.print("WiFi.status() "); 764 Serial.println(WiFi.status() == WL_CONNECTED); 765 if (!client) { 766 Serial.println("No client?\n"); 767 } 768 769 Serial.println("----------"); 770 // close the connection: 771 //client.stop(); 772 } 773 } 774 775 /* 776 This is somehow problematic because it doesn’t 777 allow any client to connect until that random 778 connection is closed. To solve that issue, we 779 create a timer that closes all the connections 780 after a predetermined number of seconds. 781 782 */
———————- END/FIN ———————-
Hi again.
If you want your browser to keep asking for data, I recommend following Stéphane tutorial about websockets to synchronize everything when readings are ready: https://rntlab.com/question/full-tutorial-esp32-remote-control-with-websocket/
Alternatively, you can also use server-sent events to send readings to the browser when they are available :https://github.com/me-no-dev/ESPAsyncWebServer/#async-event-source-plugin
However, as I’ve told previously, this is efficiently implemented when using the AyncWebServer library: https://github.com/me-no-dev/ESPAsyncWebServer/
Thank you.
I dont know if I could rewrite the whole code in an asynchroneous web server….
JPD
We have many tutorials with asynchronous web servers. It’s way easier that your method.
You can start with this one: https://randomnerdtutorials.com/esp32-dht11-dht22-temperature-humidity-web-server-arduino-ide/
Regards,
Sara
Thank you. I will surely follow your links. I am lazy, so I tried this that seems to work. I will give you a yes or no answer soon. …
I did this:
Start your code here
double Whyling = 0.0;
client = server.available();
Serial.println(true == client);
// listen for incoming clients
while (true != client ) {
Serial.println((String)(Whyling / 60.0) + "minutes, Whyling for a client at line 589.");
delayFunction(6);
totalDelay += 0.1;
Whyling += 6;
client = server.available();
}
if (client) {