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
- reduce such delay?
- is it possible to send more than one variable in the code?
- 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 = __setInterval(function() {getSensorData();}, 1000); // Call the update function every set interval e.g. 1000mS or 1-sec
SETTINGS REPORT 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
- reduce such delay?
- is it possible to send more than one variable in the code?
- are there any other options?