Ajax auto refresh speed (slow) ESP32

Dear experts,

need your help here. The project which i am working on is a measurement of temperature and humidity with DHT22 unit and it is a pretty straight forward if you use the standard tutorials on the web. The data get easily refreshed on the browser using AJAX and it runs smoothly.

But as i build my program, i have added more functions calling from loop to check the pattern of increase and decrease of temperature "with out using delays". i am using millis() function. What i have observed is the XML - Ajax data refresh rate has become slower and slower and stopped updating as the program builds up.

I tried to use ESP Async concept but the challenge is i am already in state which i cant rewrite the entire code, secondly i am not very much familiar with that library as well.

My question is how can we

  1. reduce such delay?
  2. is it possible to send more than one variable in the code?
  3. are there any other options?

Code snippet of the loop below;


void loop(void) {
server.handleClient(); // Keep checking for a client connection
if ((WiFi.softAPgetStationNum() > 0) && (station_count_connect == 0) && (station_count_disconnect == 1)) {
station_count_disconnect = 0;
digitalWrite(BUZPIN, HIGH);
delay(200);
digitalWrite(BUZPIN, LOW);
delay(200);
digitalWrite(BUZPIN, HIGH);
delay(200);
digitalWrite(BUZPIN, LOW);
}
if ((WiFi.softAPgetStationNum() == 0))
{
if ((station_count_connect == 1) && (station_count_disconnect == 0))
{
station_count_connect = 0;
station_count_disconnect = 1;
digitalWrite(BUZPIN, HIGH);
delay(600);
digitalWrite(BUZPIN, LOW);
} else
{
station_count_connect = 0;
station_count_disconnect = 1;
}
}
//******************* hours SYS running ****************************
time_inter_current_millis = millis() / 60000;

allSeconds = millis() / 1000;
runHours = allSeconds / 3600;
secsRemaining = allSeconds % 3600;
runMinutes = secsRemaining / 60; //
runSeconds = secsRemaining % 60;
if (autoruntime_token == 0) {
sprintf(buf, "%02d:%02d:%02d ", runHours, runMinutes, runSeconds);
}
//******************* hours running ****************************
if (autoruntime_token == 1) {
Auto_run_allSeconds = (millis() - start_run_millis) / 1000;
Auto_runHours = Auto_run_allSeconds / 3600;
Auto_secsRemaining = Auto_run_allSeconds % 3600;
Auto_runMinutes = Auto_secsRemaining / 60;
Auto_runSeconds = Auto_secsRemaining % 60;
sprintf(auto_runtime_buf, "%02d:%02d:%02d", Auto_runHours, Auto_runMinutes, Auto_runSeconds);
}
/**************** TO AVOID USING DELAY IN RECORDING DATA ***********************************************/
Time_difference = (millis() - Last_runSeconds) / 1000;
if (save_button == 1) {
lcd.setCursor(15, 0);
lcd.write(0);
if ((Time_difference >= interval_time))//&& (val==1))
{
Last_runSeconds = millis();
File dataFile = SPIFFS.open("/log.txt", "a");
if (dataFile) {
Serial.println("The file was opened successfully.");
dataFile.print(buf);
dataFile.print(" - ");
dataFile.print("Temp - ");
dataFile.print(t);
dataFile.print(" dC ");
dataFile.print("Moist - ");
dataFile.print(h);
dataFile.println(" %");
// countSD = 0;f
}
else {
Serial.println("failed to log into LOG.txt");
}
dataFile.close();
}
}
else
{
lcd.setCursor(15, 0);
lcd.print(" ");
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

unsigned long measure_currentMillis = millis();

if (measure_currentMillis - measure_previousMillis >= 2000) {
measure_previousMillis = measure_currentMillis;
h = dht.readHumidity();
// Read temperature as Celsius (the default)
t = dht.readTemperature();
// Read temperature as Fahrenheit (isFahrenheit = true)

// Check if any reads failed and exit early (to try again).
if (isnan(h) || isnan(t)) {
  Serial.println(F("Failed to read from DHT sensor!"));
  return;
}
if (start_monitor_flag == 0) {
  file_name_write(last_temp_file, String(t));
  file_name_write(last_moist_file, String(h));
}

}
/***************************************************************/
moist_value_check = global_temp_moist_perc.toFloat();
max_moist_value_check = global_temp_max_moist_perc.toFloat();

if (h < moist_value_check) {
buzzer_status = 1;
// threshold_off = false;
}
else {
buzzer_status = 0;
digitalWrite(BUZPIN, LOW);
// threshold_on = false;
}
if ((buzzer_status == 1) && (observe == 0)) {
buzzer_beep(BUZPIN, buzzer_interval);
}
/***************************************************************/
observation_data();
newprogram_selection();
if (debug_mode == 0) {
screenprint();
}
else
{
debug_screen_value();
}
checkbutton(); // for selection of preset values
autoruntime(); // calling a function to sense the varaince in the data sensed
}

The code for the html page which i am doing auto refreshing is given below

THE TEMPERATURE IS = __

THE MOISTURE IS = __

THE CURRENT RUNNING TIME IS = __

SETTINGS REPORT

setInterval(function() {getSensorData();}, 1000); // Call the update function every set interval e.g. 1000mS or 1-sec
   function getSensorData() {
      var xhttp = new XMLHttpRequest();
      xhttp.onreadystatechange = function() {
      if (this.readyState == 4 && this.status == 200) {
        document.getElementById("TEMPvalue").innerHTML = this.responseText;
      }
    };
    xhttp.open("GET", "TEMPread", true);
    xhttp.send();
    //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    var xhttp = new XMLHttpRequest();
    xhttp.onreadystatechange = function() {
      if (this.readyState == 4 && this.status == 200) {
        document.getElementById("HUMIvalue").innerHTML = this.responseText;
      }
    };
    xhttp.open("GET", "HUMIread", true);
    xhttp.send();
    //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    var xhttp = new XMLHttpRequest();
    xhttp.onreadystatechange = function() {
      if (this.readyState == 4 && this.status == 200) {
        document.getElementById("time-state").innerHTML = this.responseText;
      }
    };
    xhttp.open("GET", "timestatus", true);
    xhttp.send();
  }
</script>

Can you please help on suggesting why the AJAX becomes non responsive or slow?

My question is how can we

  1. reduce such delay?
  2. is it possible to send more than one variable in the code?
  3. are there any other options?

Edit your post and insert code tags!

Post complete code!

But there are serveral delay() calls in your code.

I guess buzzer_beep() does the equivalent of a delay() call.

Hi Pylon

Please find attached codes.

The delay is used for buzzer pin, only when the buzzer is activated after reaching the set parameter valule. Normal conditions it never runs. My concern is when during the idle condition, it is supposed to throw the value out to the webpage. The sensor does read value and can be seen in serial monitor and also in my LCD screen too.

One more thing i observed yesterday i was checking the Browser debug screen to see the timing and response. Please find below the screen shot. Only the Temperature readings actually displayed. rest on very random situations only.

data.h (1.5 KB)
index.h (3.0 KB)
setting.h (3.8 KB)
TEST6.ino (47.3 KB)

Thanks for your time.

Stay safe..

Raghu

I'm impressed that the TEMPread request actually works. Don't reuse the same variable name for variables that must exist for a longer time because the promise isn't fulfilled yet.

You do dozens of writes to the flash file system. Don't do that. Your flash will wear out very fast. There is no optimization of writes being distributed over several cells (as p.e. most SD card drivers will do), so after a few days of continuous operation you should be prepared for the first cells to break (a cell may be rewritten about 10'000 times).

BTW such a write operation to flash is quite slow.

Hi Pylon

Thanks for the input.

i changed the variable name in the javascript separately for each parameter. But still the temperature is reading more often than the other 2 parameters.
Secondly my display LCD actually shows the temperature, moisture and also the time properly though. So why the challenge here?
i stopped writing the to the spiffs now so that this should prevent wear of the cells.

But my main concern is how to display these parameters seamlessly in the browser. The previous version of the same i did in the Nodemcu v3, it worked well though. But not able to understand on why ESP32 has a struggle in refreshing it.

Please advise

Raghu

The LCD doesn't show the same variables. You might start there.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.