How to handle PSON what has array of objects?

Hi

I had made IOT setup where is different kind of devices connected on different use case.

Device management is planned to handle by Thinger Property.

So if you have changes on devices, you just modify property from Thinger.

I am now looking help from community to found out how to read on PSON of Thinger property what has:

  • array of Objects

  • content of object

Attached picture shows what kind of structure can be.

In real life one device has lots of parameters so it is simplified for illustrative purposes.

All examples what i had found is only demonstrating how to read integers or something similar from property.

So is this correct aproach and how i should continue ?

  pson data;
  bool requestOK = thing.get_property("DeviceList", data);
  if (requestOK) {
  pson_array my_array = (pson_array)data["devices"] ;
 }

I found out that get_property not work if trying to use it setup()
So i moved it to loop()

And here is my example code how i was able to iterate objects of array.

int pin=0;
pson data;
if (thing.get_property("DeviceList", data)) {
    if (data.is_object()) {
        pson_array my_array = (pson_array)data["devices"];
        if (my_array.size() > 0) {
            pson_container<pson>::iterator iterator = my_array.begin();
            bool pson_from_Iterator = iterator.valid();
            while (pson_from_Iterator) {
                data = iterator.item();
                if (data.is_object()) {
                    pin= (int)data["pin"];
                }
                else {
                    Serial.println("Array item wasn't object");
                }
                if (iterator.has_next()) {
                    pson_from_Iterator = iterator.next();
                }
                else {
                    Serial.println("All objects iterated");
                    pson_from_Iterator = false;
                }
            }
        }
        else Serial.println("Array was empty");
    }
    else {
        Serial.println("Device list is not object");
    }
}
else {
    Serial.print("No response for:");
    Serial.println("DeviceList");
}

Maybe this is clear for all others, but i was surprised when my heap poisoning problems was disappeared by changing memory allocation define on thingger.h file
So these 2 lines make to difference:
#define THINGER_USE_STATIC__MEMORY
#define THINGER_STATIC_MEMORY_SIZE 2048

First i tried to use 1024 define, but my properties file was too big for reading it. There is no any error happen on that situation , so you just need just test it.

1 Like