Size of a table

I’m going to collect sensor data in offline mode and store them in the nv table between deepsleeps. I would like to check if the nv table can hold the new sensor data in the 4KB available before updating it. And of course enable the wifi, send the data to a database and clear the nv table if its reaching the limit. How can I determine the size of a table in device code? :slight_smile:

You can’t at the moment; right now they are BSON encoded. Note that the names of each entry in a table are stored there too, so name length (especially if you have an array of tuples) will make a significant difference - it’s more efficient, space-wise to have eg:

{ “timestamp”: [ time, time, time ], “reading”: [ reading, reading, reading ] }

than…

{ [ { “timestamp”: time, “reading”: reading }, { “timestamp”: time, “reading”: reading }, { “timestamp”: time, “reading”: reading } ] }

(I think I have the braces right!).

Thanks Hugo. Very neat. I’ll definitely implement that way of structuring readings, seems like a lot of space saved!
How would one deal with the issue of saving too much in the nv table then? Estimating with the length of the JSON encoded string and assuming every character takes up 1 byte? :slight_smile:

Unfortunately the JSON size of a table doesn’t even give a useful upper-bound on the BSON size of a table: the integer 0, for instance, takes up one byte in JSON and four in BSON. There’s no substitute for actually working out by experimentation how many readings the 4K can contain, and then embedding that number (minus a safety-factor) in your code as a constant.

Peter

Going with that approach then :slight_smile: Thanks for the replies!

FYI, I found a JSON to BSON conversion in C here: https://gist.github.com/algernon/1070514