Search code examples
c++jsonarduinonodemcuarduino-esp8266

How to store JSON Array in arduino program?


I was working on IOT project for which I needed JSON parsing in NodeMCU. I saw this sample code for arduino for parsing JSON with the help of ArduinoJson library which worked well and I was able to get and parse the data from url(say url_1) successfully. Now I want to store this data in an array so that when I get data from the other ur2(say url_2) I can compare them with each other and trigger an event repective to the result. Data in url_1 and url_2 is of form,
["1","1","1","1","0","0","0","0"]
and assume that url_1 had same values as specified.

What I did was I declared an array Data[] in which I stored the parsed JSON values so that I could use them later on in if else statements in the code. As you can see that the data I am retrieving in url have 1's and 0's only, so what I want to do is that "If get 1 do this", "else do that", which you can see in the code. But the problem is that once I end the connection to the url the Data[] array gives only garbage values which I checked by printing them on serial monitor as shown in the code.
What I believe is that "const char* Data[20];" stores the position of JSON data and when I end the connection, the data at those position is also lost that's why I'm getting the garbage values. Now, I could be wrong since I'm new to this stuff. That's why I wanted to know how to solve this problem which is that if what I said was right, then how could I store the parsed json data in an array so that it is not lost even if the connection to the url has been ended.
(I'm new to this platform so If I did something wrong and wish that you guys can guide me for the future and I also apologize for my broken english).

#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <ArduinoJson.h>  

const char* Data[20];
const char* ssid = "SSID";
const char* password = "Password";

//Connecting to WiFi
void setup() {
  WiFi.mode(WIFI_OFF);
  delay(1000);
  WiFi.mode(WIFI_STA);// Hides the viewing of ESP as wifi Hotspot 
  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.println("Connecting...");
  }
  Serial.println("Connected to WiFi Successfully");
}

void loop()
{
  if(WiFi.status()== WL_CONNECTED)
  {
    HTTPClient http;
    //Starting connection  to url_1
    http.begin("url_1");
    int httpCode = http.GET();
    if(httpCode > 0)
    {
      String data = http.getString(); 
      const size_t bufferSize = JSON_ARRAY_SIZE(8) + 20;
      DynamicJsonBuffer jsonBuffer(bufferSize);
      JsonArray& root = jsonBuffer.parseArray(data);
      for(int i=0;i<8;i++){
        Data[i] = root[i];
        Serial.println("Printing whole Data");
        Serial.println(Data[i]);
      }
    }
    http.end();//ending the connetion


    for(int i=0;i<8;i++)
    {
      if(strcmp(Data[i],"1")==0){
        Serial.print("if satement, Data = ");
        Serial.println(Data[i]);  
      }
      else
      {
        Serial.print("else satement, Data = ");
        Serial.println(Data[i]);
      }
    }
  }
}

Solution

  • Data[] contains pointers to dynamically allocated strings. When you leave the code block with the JSON parser, its destructor is called and therefore the allocated memory can and has been overwritten by something else.

    I would suggest to use instead

    bool Data[...];
    ...
       // true for "1", false for "0"
       Data[i] = strcmp(root[i], "1") == 0;
    ...
    

    EDIT if you need to store more "complicated" data, e.g. actual strings, you will need to make a copy of the string pointed to by root[i].