0

i'm doing with nrf24l01 network. i want to storage value from sensor node ( like id, value temp, value humid) in JSON string like this one. my string:

{ 
    "1": {//this is a ID from node
            "Temp": "value",
            "Humid": "value"
        },
    "2": {
         "Temp": "value",
         "Humid": "value"
     }
}

i can create a object and add value from temp and humid, but i dont know how to create "1" /* ID /*

My code

void loop() 
{
  network.update();
  if(network.available())
  {
    RF24NetworkHeader header;  
    network.read(header, &pack0, sizeof(pack0));
    if (header.from_node == 1)//this is the ID , return 1, 2, ....
    {
      object["ID"] = header.from_node;
      value["Temperature"] = pack0.temperature;
      value["Humidity"] = pack0.humidity;
      value["Soil"] = pack0.soil;
      serializeJsonPretty(object, Serial);
      Serial.println("");    }
    else if (header.from_node == 2)
    { 
      object["ID"] = header.from_node;
      value["Temperature"] = pack0.temperature;
      value["Humidity"] = pack0.humidity;
      value["Soil"] = pack0.soil; 
      serializeJsonPretty(object, Serial);
      Serial.println("");
    }
  }
}
Quang Minh Lê
  • 169
  • 1
  • 2
  • 13

1 Answers1

1

You can use createnestedobject, also remember that you are creating nested json objects in a loop so you need to take care of the size of your Json document (256).

EDIT: For example when your nested object exceeds a fixed value, You can use clear() function to clear the JSON object.

#define MAX_ELEMENTS 10

int receive_count = 0;
StaticJsonDocument<256> doc;
JsonObject object = doc.to<JsonObject>();

void loop()
{
    network.update();
    if (network.available())
    {
        RF24NetworkHeader header;
        network.read(header, &pack0, sizeof(pack0));
        if (header.from_node)
        {
            JsonObject ID = object.createNestedObject(String(header.from_node));
            ID["Temperature"] = pack0.temperature;
            ID["Humidity"] = pack0.humidity;
            ID["Soil"] = pack0.soil;
            serializeJsonPretty(object, Serial);
            Serial.println("");
            receive_count++;
            //reset counter if reached and clear object
            if (receive_count >= MAX_ELEMENTS)
            {
                doc.clear();
                object = doc.to<JsonObject>();
                receive_count = 0;
            }
        }
    }
}
Masoud Rahimi
  • 5,785
  • 15
  • 39
  • 67
  • if i create a Nested outside of the loop(), it's ok. when i use your code and create a Nested inside a loop(), received 4 packages the 5th packages is error:
    20:18:31.218 -> { 20:18:31.218 -> "1": { 20:18:31.218 -> "Temperature": 28.5, 20:18:31.253 -> "Humidity": 39 20:18:31.288 -> } //Missing Soil value and the 6th packages is: 20:18:34.219 -> { 20:18:34.219 -> "1": {} 20:18:34.219 -> } Missing all of values. so i change from 256 to 512, i can receive 10 package, like out of space. in my web, i want it update value like every 10 mins
    – Quang Minh Lê Jun 18 '19 at 13:27
  • can i reset data the Json, like this. 1. read value;
    2. storage in Json; 3.send it to web (i'm using mosquitto and node red right now or firebase. subscribe ID then i can see the ID.temperature ,... ; 4. back to step 1; Sorry i dont know how cant i use formatting cmt
    – Quang Minh Lê Jun 18 '19 at 13:43
  • it's actually doc.clear(); but after clear(); needing to create a object again. `doc.clear(); object = doc.to(); receive_count = 0;` Thank you very much – Quang Minh Lê Jun 20 '19 at 04:16
  • Ah my fault, I fixed that. – Masoud Rahimi Jun 20 '19 at 06:33