Json with about 6000 bytes save

jeancarlosc
Posts: 3
Joined: Mon Feb 24, 2025 12:38 pm

Json with about 6000 bytes save

Postby jeancarlosc » Mon Mar 17, 2025 9:33 pm

I have this code below where I can insert a list of up to 20 positions of about 5000 bytes. I have 31400 bytes free on the esp to try to save a list with 24 positions when deserializing my json it returns zero.

Code: Select all

void lerJsonBanco( ) {
 
  Serial.println("executarConsulta");
  Serial.println("bombaID");
 

    // Simula a criação do URL
    String url = String(serverApi) + "?email=" + email + "&senha=" + senha ;



    // Inicia a requisição HTTP
    http.begin(client, url);
    http.setAuthorization("dosadora", "@sarosinc");
    http.addHeader("Content-Type", "application/json");
  
    int httpResponseCode = http.GET();  // Faz a requisição GET
  
    Serial.print("httpResponseCode: ");
    Serial.println(httpResponseCode);
  
    if (httpResponseCode > 0) {

        WiFiClient *stream = http.getStreamPtr();
        
        DynamicJsonDocument doc(16384);  // 16KB
        DeserializationError error = deserializeJson(doc, *stream);

        if (error) {
          Serial.println("Erro ao desserializar JSON");
          enviarLogParaDB("Loop", "Erro ao carregar JSON - Pode ser que estourou a memória. Diminua o campo Doses dia e desligue e ligue a dosadora " + String(error.c_str()), "alto");
          return;
      }else{
          Serial.println("✅ JSON recebido e desserializado com sucesso!");
          JsonArray outerArray = doc.as<JsonArray>();

          for (JsonArray innerArray : outerArray) {
            for (JsonObject item : innerArray) {

              
                String comando = item["comando"];
                int codBomba = item["idbomba"];
                String json = item["jsonstring"];
                Serial.print("Tamanho do JSON: ");
                Serial.println(json.length());

                
                String path = "/" + String(codBomba) + ".json";
        
                if (comando == "dosar-manual") {
                    double dosagem = item["dosagem"];
                    double calibracao = item["calibracao"];
                    dosarManual(codBomba, dosagem, calibracao);
                    apagarComandos(email, senha, "dosar-manual", codBomba);
                } else if (comando == "agendamento") {
                  LittleFS.remove(path);
                  File file = LittleFS.open(path, "w");
                  if (!file) {
                      enviarLogParaDB("Loop", "Erro ao abrir o arquivo para escrita!", "alto");
                      continue;
                  }

                  int length = json.length();
                  Serial.println("length");
                  Serial.println(length);
                  file.print(json);
                  file.close();
                  apagarComandos(email, senha, "agendamento", codBomba);
                  if (debugMode) Serial.println("✅ JSON salvo no LittleFS com sucesso!");
                } else if (comando == "calibrar") {
                    calibrar(codBomba);
                    apagarComandos(email, senha, "calibrar", codBomba);
                }
            }
        }
      }

        
    } else {
      Serial.println("Erro HTTP");
    }
  
    http.end();  // Finaliza a requisição e libera a memória
  
    client.stop();  // Libera recursos do WiFiClient
  
    delay(10);  // Pequeno delay para estabilizar o sistema
  
    printMemoryStatus("Fim da consulta");
}

lbernstone
Posts: 1132
Joined: Mon Jul 22, 2019 3:20 pm

Re: Json with about 6000 bytes save

Postby lbernstone » Tue Mar 18, 2025 7:05 pm

Make sure you are looking at getMaxAllocHeap, not just the total free (getFreeHeap). The deserialize method likely has some internal consumption as well.

Who is online

Users browsing this forum: No registered users and 1 guest