Running out of RAM using JsonObject

Hello. I use an Arduino Uno R3 board and I try to collect some data from my solar panel and send it via "printTo()" to my ESP8266 board to finally send data o database.

CODE:

...
#include <ArduinoJson.h> // pentru transmiterea datelor
.... 

setup(){
...
  Serial.begin(9600);
...
}

void loop(){
 // POWER LOGGER
...
// SOLAR TRACKER
...
// SEND DATA TO ESP 
 JsonObject& root = jbuffer.createObject();
//   root["voltage"] = loadvoltage;
//   root[ "current"] = current_mA;
//   root["power"] = power;
//   root["energy"] = energy;
   root["v"] = 12; // DUMMY DATA 
   root[ "c"] = 50;
   root["p"] = 22;
   root["e"] = 33;
   root.printTo(Serial);
   root.remove("v");
   root.remove("c");
   root.remove("p");
   root.remove("e");
   Serial.println(); // clear buffer

// DELAY TIME = 1000ms
delay(delayTime);
}

and my serial monitor looks like this:

21:33:04.585 -> {"v":12,"c":50,"p":22,"e":33}
21:33:05.608 -> {"v":12,"c":50,"p":22,"e":33}
21:33:06.632 -> {"v":12,"c":50,"p":22,"e":33}
21:33:07.611 -> {"v":12,"c":50,"p":22,"e":33}
21:33:08.632 -> {"v":12,"c":50,"p":22,"e":33}
21:33:09.611 -> {"v":12,"c":50,"p":22,"e":33}
21:33:10.637 -> {"v":12,"c":50,"p":22,"e":33}
21:33:11.609 -> {"v":12,"c":50,"p":22,"e":33}
21:33:12.632 -> {"v":12,"c":50,"p":22,"e":33}
21:33:13.611 -> {"v":12,"c":50,"p":22,"e":33}
21:33:14.636 -> {"v":12,"c":50,"p":22,"e":33}
21:33:15.613 -> {"v":12,"c":50,"p":22,"e":33}
21:33:16.637 -> {"v":12,"c":50,"p":22,"e":33}
21:33:17.612 -> {"v":12,"c":50,"p":22,"e":33}
21:33:18.637 -> {"v":12,"c":50,"p":22,"e":33}
21:33:19.615 -> {"v":12,"c":50,"p":22,"e":33}
21:33:20.635 -> {"v":12,"c":50,"p":22,"e":33}
21:33:21.608 -> {"v":12}
21:33:22.629 -> {}
21:33:23.602 -> {}
21:33:24.628 -> {}

I running out of RAM ?

Sketch uses 12874 bytes (39%) of program storage space. Maximum is 32256 bytes.
Global variables use 675 bytes (32%) of dynamic memory, leaving 1373 bytes for local variables. Maximum is 2048 bytes.

Or what is the problem? How can I simplify ?

Your problem might very well be caused by something in another part of the code. Please post your full code.

// LIBRARII
#include <Servo.h> // partea mecanica de miscare

#include <Wire.h> // LCD & INA219
#include <SPI.h> // LCD & INA219

#include <LiquidCrystal_I2C.h> // afisare LCD
#include <Adafruit_INA219.h> // cititor curent/tensiune
#include <Adafruit_SSD1306.h> // salvam date pe SD

#include <ArduinoJson.h> // pentru transmiterea datelor

// VARIABILE SI INITIALIZAM

#define servo_x_pin 9 // stanga dreapta
#define servo_y_pin 10 // sus jos
#define sensX 100 // sensibilitatea pe X
#define sensY 100
#define pasX 2  // cu cate grade modificam in fiecare pas
#define pasY 2

Adafruit_INA219 ina219; // cititor curent/tensiune
LiquidCrystal_I2C lcd(0x27,20,4); // adresa 0x27 adica 16 caractere pe 2 linii
Servo servoX, servoY;

// VARIABILE PENTRU TRANSFER DE DATA
DynamicJsonBuffer jbuffer;

// VARIABILE DE "PUTERE"
unsigned long previousMillis = 0;
unsigned long interval = 100;
unsigned long currentMillis;

// VARIABILE DE POZITIONARE MECANICA
int posX = 90; // [0;190] din punct de vedere orizontal suntem la mijloc [stanga,dreapta]
int posY = 45; // [9,90] din punct de vedere vertical suntem aprox la mix [jos,sus]
int dirX = -1 ; // 0 pentru stanga ; 1 pentru dreapta ; -1 neinitializat
int dirY = -1 ; // 0 pentru in jos ; 1 pentru in sus ; -1 neinitializat


// VARIABILE PENTRU PHOTOREZISTORI
int rightBot, leftBot;
int rightTop, leftTop;
int left, right;
int top,bot;

// delayTime
int delayTime = 1000; // in milisec

void setup(){

  Serial.begin(9600);

  // SERVO INIT
  servoX.attach(servo_x_pin);
  servoY.attach(servo_y_pin);
  servoX.write(posX);
  servoY.write(posY);

  // prima citire (citire analogica)
  rightBot = analogRead(A2);
  leftBot = analogRead(A3);
  rightTop = analogRead(A1);
  leftTop = analogRead(A0);

  right = (rightBot + rightTop) / 2;
  left  = (leftBot + leftTop) / 2;
  if ( right < left)
    dirX = 1;
   else dirX = 0;

   top = (rightTop + leftTop) / 2;
   bot  = (rightBot + leftBot) / 2;
   if ( top < bot)
    dirY = 1;
   else dirY = 0;
  
  ina219.begin(); // cititor curent/tensiune

  // LCD
  lcd.begin();
  lcd.backlight();
  lcd.clear();
}

void loop(){

   // PARTEA DE PUTERE PE PANOU
   // = POWERLOGGER
   currentMillis = millis();
   if(currentMillis-previousMillis >= interval){
      previousMillis = currentMillis;
   }

   float energy;
   float shuntvoltage = 0;
   float busvoltage = 0;
   float current_mA = 0;
   float loadvoltage = 0;
   float power =0;

   shuntvoltage = ina219.getShuntVoltage_mV();
   busvoltage = ina219.getBusVoltage_V();
   current_mA = ina219.getCurrent_mA();
   loadvoltage = busvoltage + (shuntvoltage/1000);
   power = loadvoltage*current_mA;
   energy = energy + power*(0.1/(60*60));

   // CITIREA ANALOGICA / PARTEA MECANICA
   // = SOLAR TRACKER

   leftTop = analogRead(A0);
   rightTop = analogRead(A1);
   rightBot = analogRead(A2); 
   leftBot = analogRead(A3);

       // MISCARE STANGA/DREAPTA (PE X)
      right = (rightBot+rightTop) /2;
      left = (leftBot + leftTop) /2;

          // mergem in continuare spre dreapta
          if( right < left && dirX == 1){
            if( posX + pasX < 180){
              posX+=pasX;
              servoX.write(posX);
            }
          }

          // mergem in continuare spre stanga
          if( left < right && dirX == 0){
            if(posX - pasX > 0){
              posX-=pasX;
              servoX.write(posX);
            }
          }

          // schimbam directia spre stanga
          if(left+sensX < right && dirX == 1) // chiar si cu marja de eroare adaugata
          // avem mai multa lumina in stanga dar directia era dreapta
          dirX = 0;

          // schimbam directia spre dreapta
          if(right+sensX < left && dirX == 0)
          dirX = 1;

      // MISCAM SUS/JOS (PE Y)
      top = (rightTop + leftTop)/2;
      bot = (rightBot +leftBot)/2;

        // mergem spre jos in continuare (crestem unghiul)
        if( bot < top && dirY == 0){
          if(posY+pasY <90){
            posY+=pasY;
            servoY.write(posY);
          }
        }

        //mergem spre top in continuare (scadem unghiul)
        if(top < bot && dirY == 1){
          if(posY - pasY >0){
            posY -= pasY;
            servoY.write(posY);
          }
        }

        // schimbam directia spre top
        if( top+sensY < bot && dirY == 0) // chiar si cu marja de eroare adaugata
        // avem mai multa lumina in top si directia in jos 
          dirY = 1;

        //schimbam directia spre bot
        if(bot + sensY < top && dirY == 1)
        dirY = 0;

   // SEND DATA TO ESP8266
   JsonObject& root = jbuffer.createObject();
//   root["voltage"] = loadvoltage;
//   root[ "current"] = current_mA;
//   root["power"] = power;
//   root["energy"] = energy;
   root["v"] = 12;
   root[ "c"] = 50;
   root["p"] = 22;
   root["e"] = 33;
   root.printTo(Serial);
   root.remove("v");
   root.remove("c");
   root.remove("p");
   root.remove("energy");
   Serial.println(); // clear buffer

    // ***************** VERIFICARE MONITOR SERIAL **************

//  Serial.print("LEFT TOP: ");
//  Serial.println(leftTop);
//
//  Serial.print("RIGHT TOP: ");
//  Serial.println(rightTop);
//
//  Serial.print("RIGHT BOT: ");
//  Serial.println(rightBot);
//
//  Serial.print("LEFT BOT: ");
//  Serial.println(leftBot);
//
//  Serial.println("************************************");

   // delay!
   delay(delayTime);
   
}

I'm sorry that I used romanian language for comments but its my its for my graduation paper and I should do

@sterretje

I also run the sketch like this:

#include <ArduinoJson.h> // pentru transmiterea datelor


// VARIABILE PENTRU TRANSFER DE DATA
DynamicJsonBuffer jbuffer;

// delayTime
int delayTime = 1000; // in milisec

void setup() {
  // put your setup code here, to run once:
  
  Serial.begin(9600);
}

void loop() {
     // SEND DATA TO ESP8266
   JsonObject& root = jbuffer.createObject();
//   root["voltage"] = loadvoltage;
//   root[ "current"] = current_mA;
//   root["power"] = power;
//   root["energy"] = energy;
   root["v"] = 12;
   root[ "c"] = 50;
   root["p"] = 22;
   root["e"] = 33;
   root.printTo(Serial);
   root.remove("v");
   root.remove("c");
   root.remove("p");
   root.remove("energy");
   Serial.println(); // clear buffer

     // delay!
   delay(delayTime);
}

and I have the same issue. :frowning:

I just use

   jbuffer.clear();

and it work ! <3 Thanks a lot!