Persistent JSON object


Hey guys,
I’m currently trying to create a simple running hour meter with an ESP32 and Mongoose OS. Currently there’s a timer which runs every 5 seconds and increments a counter var if GPIO26 is high. This counter var is sent to my server via MQTT every few hours.
If there’s a reboot, obviously the value of my counter var is gone, so after every boot I call my server to get the last value sent and count from this one on.

This works fine, except that in the event of an internet outage, this is exactly what might stop working, which is why I wanted to take a look at local storage.
At the moment that’s what I tried (looping every hour):

json_fprintf("hours.json", "{ "hours": %d}", hours);

And then

struct hour_config { int hours; } c = { .hours = 0 };
char *content = json_fread("hours.json");
json_scanf(content, strlen(content), "{hours: %d}", &c.hours);

Is this the way to go or are there other possibilities to do this?

Cheers, Jonas


You can add in mos.yml (config_schema section)

  - ["app.count", "i", 0, {}]

and in your application use

mgos_sys_config_save(&mgos_sys_config, false, NULL);

when you want to save that variable and

  count = mgos_sys_config_get_app_count();

to initialize it upon startup.


Great tip, thank you! :slight_smile: