-->
Page 1 of 1

Very slow WebServer if use SPIFFS and ArduinoJson

PostPosted: Thu Jan 10, 2019 10:57 am
by array81
I'm working on a ESP8266 project. On this project I have 3 small json file saved on SPIFFS (by default the files do not exist, they must be created by the user by WebServer).
On LOOP function the project read these files (ONLY ONE TIME) with these functions:

Code: Select allvoid loadSettingsZoneA(){
  Serial.println("Esecuzione loadSettingsZoneA");
 
  String temp;
  String tagC;
  String tagT;
  String tagS;
  String fixWeekDay;
 
  File loadFile = SPIFFS.open("/json/zone_a.json", "r");
  if (!loadFile){
    Serial.println("Il file /json/zone_a.json non esiste");
  } else {
    size_t size = loadFile.size();
    if ( size == 0 ) {
      Serial.println("Il file /json/zone_a.json è vuoto");
    } else {
      //std::unique_ptr<char[]> buf (new char[size]);
      //loadFile.readBytes(buf.get(), size);
      StaticJsonBuffer<530> jsonBuffer;
      JsonObject& root = jsonBuffer.parseObject(loadFile);
     
      if (!root.success()) {
        Serial.println("Impossibile leggere il file /json/zone_a.json");
      } else {
        if (lastWeekDay == 0) {
          zoneA_p_active = false;
          Serial.println("Programmazione zona A NON aggiornata");
        } else {
          fixWeekDay = lastWeekDay-1;
         
          if (fixWeekDay == 0) {
            fixWeekDay = 7;
          }
 
          tagC = "cA" + String(fixWeekDay);
          tagT = "tA" + String(fixWeekDay);
          tagS = "sA" + String(fixWeekDay);
 
          if (root[tagC] == "true") {
            zoneA_p_active = true;
          } else {
            zoneA_p_active = false;
          }
           
          temp = (const char*)root[tagT];
          zoneA_p_time_hours = temp.substring(0,2).toInt();
          zoneA_p_time_minutes = temp.substring(3,5).toInt();
          zoneA_p_time_interval = root[tagS];
 
          //Serial.println(zoneA_p_active);
          //Serial.println(zoneA_p_time_hours);
          //Serial.println(zoneA_p_time_minutes);
          //Serial.println(zoneA_p_time_interval);
         
          Serial.println("Programmazione zona A aggiornata");
        }
      }
    }
    loadFile.close();
  }
}

void loadSettingsZoneB(){
  Serial.println("Esecuzione loadSettingsZoneB");
 
  String temp;
  String tagC;
  String tagT;
  String tagS;
  String fixWeekDay;
 
  File loadFile = SPIFFS.open("/json/zone_b.json", "r");
  if (!loadFile){
    Serial.println("Il file /json/zone_b.json non esiste");
  } else {
    size_t size = loadFile.size();
    if ( size == 0 ) {
      Serial.println("Il file /json/zone_b.json è vuoto");
    } else {
      //std::unique_ptr<char[]> buf (new char[size]);
      //loadFile.readBytes(buf.get(), size);
      StaticJsonBuffer<530> jsonBuffer;
      JsonObject& root = jsonBuffer.parseObject(loadFile);
     
      if (!root.success()) {
        Serial.println("Impossibile leggere il file /json/zone_b.json");
      } else {
        if (lastWeekDay == 0) {
          zoneB_p_active = false;
          Serial.println("Programmazione zona B NON aggiornata");
        } else {
          fixWeekDay = lastWeekDay-1;
         
          if (fixWeekDay == 0) {
            fixWeekDay = 7;
          }
 
          tagC = "cB" + String(fixWeekDay);
          tagT = "tB" + String(fixWeekDay);
          tagS = "sB" + String(fixWeekDay);
 
          if (root[tagC] == "true") {
            zoneB_p_active = true;
          } else {
            zoneB_p_active = false;
          }
         
          temp = (const char*)root[tagT];
          zoneB_p_time_hours = temp.substring(0,2).toInt();
          zoneB_p_time_minutes = temp.substring(3,5).toInt();
          zoneB_p_time_interval = root[tagS];

          Serial.println("Programmazione zona B aggiornata");
        }
      }
    }
    loadFile.close();
  }
}

void loadSettingsAPP(){
  Serial.println("Esecuzione loadSettingsAPP");
 
  File loadFile = SPIFFS.open("/json/settings.json", "r");
  if (!loadFile){
    Serial.println("Il file /json/settings.json non esiste");
  } else {
    size_t size = loadFile.size();
    if ( size == 0 ) {
      Serial.println("Il file /json/settings.json è vuoto");
    } else {
      //std::unique_ptr<char[]> buf (new char[size]);
      //loadFile.readBytes(buf.get(), size);
      StaticJsonBuffer<250> jsonBuffer;
      JsonObject& root = jsonBuffer.parseObject(loadFile);
     
      if (!root.success()) {
        Serial.println("Impossibile leggere il file /json/settings.json");
      } else {

        zone_m_time_interval = root["sC1"];

        Serial.println("Preferenze APP aggiornate");
      }
    }
    loadFile.close();
  }
}


However, these functions make my webserver unstable. When try to get it web browser I get very slow response, in many case the response fails and if I try to ping it I get very high % of packed fails.

If I remove these functions I don't have problem. The speed is acceptable, website is correctly loaded and ping test is ok (no packed fails or very low % of packed fails).

I can not figure out what the problem is in these functions.

Can you help me?