0

I am making a project involving the AutoConnect, ArduinoJSOn and LedControl libraries to fetch some values from an endpoint.

Basically what I do is connect to the internet, fetch the values from a JSON file in a given URL, store the values in variables and display the corresponding numbers in a 7segment module (using a max 7219 module).

Everything seems to work perfectly in the first iterations of the loop program, but after some iterations switching the values from one value to another, it starts to act strangely, as the input values changed on runtime:

[AC] WiFi.config(IP=0.0.0.0, Gateway=0.0.0.0, Subnetmask=0.0.0.0, 
DNS1=0.0.0.0, DNS2=0.0.0.0) [AC] DHCP client(STARTED)
[AC] WiFi.begin()
[AC] Connecting........[AC] established IP:192.168.1.139
[AC] http server started
Wifi connected: 192.168.1.139
Connected to endpoint
Daily:13.37
Monthly:1111
4
13.37
4
1111
4
13.37
4
1111
4
13.37
4
1111
4
13.37
4
1111
2
h
0

2
h
0

2
h

As you can see in the previous serial output, after the 8th iteration, everything starts malfunctioning. The output correlates with the number of digits that will be used in the 7-segment led module ( 4 ) and the number to display ( 13.37 and 1111 ). Then it starts getting the wrong values forever.

For getting the values in the JSON I use the following code:

void fetchJSONandPutValues(){

     HTTPClient http;  //Object of class HTTPClient
     http.begin(projectURL);
     int httpCode = http.GET();
     //Check the returning code                                                                  
     if (httpCode > 0) {
      //Parsing
      const size_t bufferSize = JSON_OBJECT_SIZE(2) + JSON_OBJECT_SIZE(3) + JSON_OBJECT_SIZE(5) + JSON_OBJECT_SIZE(8) + 370;
      DynamicJsonBuffer jsonBuffer(bufferSize);
      JsonObject& root = jsonBuffer.parseObject(http.getString());
      // Parameters
      daily = root["daily"]; 
      monthly = root["monthly"]; 
      // Output to serial monitor
      Serial.print("Daily:");
      Serial.println(daily);
      Serial.print("Monthly:");
      Serial.println(monthly);

      finalDaily = (char *) daily; 
      finalMonthly = (char *) monthly;
    }

    http.end();   //Close connection

   //setLCDValues(daily);
}

For displaying the numeric values in the 7-segment module I have made this function:

void setLCDValues(char * value){

  int charsInValue = strlen(value);
  boolean decimalPoint = false; 
  int numLEDElements = charsInValue; 
  char cleanedValue[8] ; 

  //look for point position
  int positionPoint = 999;

  for (int i = 0; i < charsInValue; i++){    
    if (value[i] == '.')
    {
      positionPoint = i;
      numLEDElements -= 1;       
      break;
    }
  }  

  int j = 0 ; 
  for (int i = 0; i < charsInValue; i++){    
    if (value[i] != '.')
    {
      cleanedValue[j] = value[i];
      j++;
    }
  }        

  Serial.println(numLEDElements);

  for (int i = 0; i < numLEDElements; i++){

    if ( i == positionPoint -1 ) 
    {
      ledDisplay.setChar(0, numLEDElements -1 - i, cleanedValue[i], true);
    }
    else
    {      
      ledDisplay.setChar(0, numLEDElements -1 - i, cleanedValue[i], false);
    }
  }
}

And in my loop() I have the calls to setLCDValues:

currentMillis = millis();  

if (currentMillis - previousMillis >= interval){

      previousMillis = currentMillis;

      if (showDaily)
      {
        setLCDValues(finalDaily);
        showDaily = false; 
        Serial.println(daily);          
      }
      else
      {
        setLCDValues(finalMonthly);
        showDaily = true; 
        Serial.println(monthly);                    
      }
}

I am guessing the problem I have here might be related to using pointers, although I haven't found yet the issue here. Could it be a problem with one of the libraries ? (ArduinoJSON?)

  • 1
    you fragment the memory heap with DynamicJsonBuffer and http.getString() – Juraj Feb 16 at 14:44
  • Thanks for pointing in the right direction, @Juraj - I will try to change the DynamicJsonBuffer to a StaticJsonBuffer (as stated in arduinojson.org/v5/doc/memory ) when I get back home. – PayToPwn Feb 16 at 15:08
  • @Juraj , you were totally right here, changing the DynamicJsonBuffer to a StaticJsonBuffer solved the memory leak. Although I don't understand the issue about using the http.getString(). How does this function fragments the memory and what would be a good alternative to it? – PayToPwn Feb 17 at 8:14

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.