0

I am using the ArduinoJson library version 6.17.12

The output received when I use serializeJson(doc,test_string) is {}. I have read in the documentation that it is due to a memory leak and I would need to use doc.clear() and doc.garbageCollect().

I am using both these functions but still, after the code runs for several minutes and when 5-10 json strings have been serialized my output is {}.

struct chargingStation
{
  int plugs[5];                        
  char *plug_points[5] = {0, 0, 0, 0}; 
  char *energyUnits[5] = {}; //from app for selected % charging
  char *requestID[5] = {};          
  char *chargerType[5] = {};        
  char *start_date[5] = {};         
  char *start_time[5] = {};         
  char *ApparentEnergyData[5] = {}; //This variable is used to store apparent Energy Data
};

struct chargingStation station;

void assign_plug_point(String tag)
{
  char publish_data[300];
  text_to_print_on_display("Processing please wait");
  DynamicJsonDocument root(300);
  root.clear();
  text_to_print_on_display("Please connect bike to plug: ");
  plug_no(plugPoint); //This function is used to print plug point number on display
  //Here we should perform all the task to assign the plug point and wait till the user connects his vehicle to the plug point
  delay(100); //added on 8/1/2021

  root["tag"] = tag;
  root["station_id"] = stationID;
  root["plug_point"] = (String)plugPoint;
  root["request_id"] = station.requestID[plugPoint];
  root["success"] = "true";
  root["station_status"] = "busy";
  publishDat = "";
  // serializeJson(root, publishDat);
  root.garbageCollect();
  serializeJson(root, publish_data);
  Serial.print("Data to publish is: ");
  Serial.println(publish_data);
  publishDat = publish_data;
  MQTTpublishAck(publishDat);
  station.plugs[plugPoint] = 1;
  memset(publish_data, 0, sizeof(publish_data));
  root.clear();
  charger_connection_status_flag = false;
  unix_seconds = unixT();
  unix_30_seconds = unix_seconds + 30;
  bool points_flag = 0;
  if (initiate_charging_flag == 0) //only test purpose
  {
    unix_seconds = unixT();
    charger_connected_flag = charger_connection_status(plugPoint);
    // Serial.println(charger_connected_flag);
    if ((charger_connected_flag == 1) && (initiate_charging_flag == 0))
    {
      points_flag = 1;
      charger_connected_flag = 0;
      publishDat = ""; //Need to clear string as it stores the json document
      // root.clear();    //Need to clear data in JSON document else it is not overwritten
      delay(100);
      root["tag"] = "charger_connection"; //Creating JSON document all these parameters are required
      // root["request_id"] = requestID[plugPoint];
      root["request_id"] = station.requestID[plugPoint];
      root["station_id"] = stationID;
      root["plug_point"] = (String)plugPoint;
      root["charger_connection_status"] = "true";
      random_number = random(0, 19);
      root["battery_percentage"] = (String)arr[random_number];
      root.garbageCollect(); //for testing purpose from arduinojson
      serializeJson(root, publishDat);
      Serial.println("Data to publish is: " + publishDat);
      // MQTTpublish(publishDat);
      initiate_charging_flag = 1;
      // doc.clear();
      text_to_print_on_display("Please press Start charging from app");
      // getApparentEnergy(plugPoint, dataTest);
      // Serial.print("Energy Meter data is: " + energyMeter); //Here energy Meter is taken a global variable and it stores data in string format for all energy meter data
      station.ApparentEnergyData[plugPoint] = "10";
      Serial.print(F("Apparent energy in array is: "));
      Serial.println(station.ApparentEnergyData[plugPoint]);
      unix_seconds = unixT();
      unix_30_seconds = unix_seconds + 30;
      // break;
    }
    bool send_mqtt_flag = 0;
    while (/*(initiate_charging_flag == 1)*/ (unix_seconds <= unix_30_seconds))
    {
      if (send_mqtt_flag == 0)
      {
        MQTTpublish(publishDat);
        initiate_charging_flag = 0; //only for test purpose
        send_mqtt_flag = 1;
      }
      unix_seconds = unixT();

      if (unix_seconds == (unix_30_seconds - 10))
      {
        // Serial.println("##########1");
        initiate_charging_flag = 0; //only for test purpose
        MQTTpublish(publishDat);
      }
      else if (charger_connection_flag_for_status_ack == 1)
      {
        Serial.println("Outside loop leaving charger connection ack received ");
        charger_connection_flag_for_status_ack = 0;
        free(station.request1);
        station.request1 = NULL;
        break;
      }
      else if (unix_seconds == (unix_30_seconds - 20))
      {
        // Serial.println("##########2");
        initiate_charging_flag = 0; //only for test purpose
        MQTTpublish(publishDat);
      }
      Serial_updation_for_charger();
      // Serial_updation();
    }
  }

  if (points_flag == 0)
  {
    // Serial.println("Points flag is: " + String(points_flag));
    publishDat = ""; //Need to clear string as it stores the json document
    // root.clear(); //Need to clear data in JSON document else it is not overwritten
    root["tag"] = "charger_connection"; //Creating JSON document all these parameters are required
    // root["request_id"] = requestID[plugPoint];
    root["station_status"] = "available";
    root["station_id"] = stationID;
    root["request_id"] = station.requestID[plugPoint];
    root["plug_point"] = (String)plugPoint;
    root["charger_connection_status"] = "false";
    root.garbageCollect(); //for testing purpose from arduinojson
    serializeJson(root, publishDat);
    MQTTpublish(publishDat); //Used to publish data on MQTT
    tft.writeFillRect(0, 45, 480, 275, GREEN); //In place of using one one block and filling rectangles will fill green color on screen at once
    fill_color();
    Points();
  }
  root.~BasicJsonDocument();
}

The above functions are been used in the code and they are called whenever I receive any data from MQTT.

I am adding the link of complete project code please refer it

17
  • Note that an Arduino Uno only has 2 KB of SRAM which maybe used up very quickly with JSON files. – Michel Keijzers Feb 1 at 13:05
  • I am using Arduino mega 2560 – Maaz Sk Feb 1 at 13:08
  • Does doc.clear() clear the document. I would expect to get {} for an empty document. Did you initialize the doc object with content? Don't tell us what you've read, show what you've done. The rest of the code would be helpfull. ;-) – Peter Paul Kiefer Feb 1 at 13:11
  • @PeterPaulKiefer, after doc.clear(), serializeJson(doc, Serial) prints null. – Benoit Blanchon Feb 1 at 13:29
  • 1
    Thanks @PeterPaulKiefer I will make a whole new code with the above changes and will share it with you. Working on the same now will keep you updated. – Maaz Sk Feb 2 at 11:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.