Skip to content

mhaberler/PicoSettings

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

PicoSettings

This is an example how to use PicoMQTT together with the ESP32 Preferences library for retrieving and changing configuration values backed by non-volatile storage.

Example

An instance of this class associates an MQTT server with a Preferences namespace, exporting variables in this namespace to topics like preferences/<namespace>/<variable>

// keep variables under the "testns" namespace:
PicoSettings settings(mqtt, "testns");

// this variable will appear under the topic preferences/testns/bar
PicoSettings::Setting<int> bar(settings, "bar", 42);

Setting a new value via assignment

To permanently change a Settings variable, just assign its value - the new value will be recorded in NVS:

bar = 4711;

On next reboot, bar will be set to 4711.

Setting a value via MQTT

To change the value of the bar setting, publish a new value to preferences/testns/bar.

The new value will be stored permanently in NVS, hence will retain the new value after a reboot.

Inspecting values via MQTT

For conveniently making all variables and their values visible via MQTT, periodically call settings.publish() .

Getting a value

A Setting can be used like a variable of the underlying type, for example:

Serial.printf("bar = %d\n", bar);

Resetting all variables to their compile-time defaults

To reset all variables in a namespace to their compile-time defaults, use the defaults() method:

setting.defaults();

After reboot, bar will again have the value 42.

Resetting all variables in a namespace via MQTT

To achieve the same effect via MQTT, publish any value to the special topic preferences/<namespace>/reset .

With this example, the reset topic would be preferences/testns/reset .

Value change notification

A value change callback may be associate with a setting, to cause some action in user code.

Example using a lambda:

PicoSettings::Setting<bool> flag(settings, "flag", true, [] {
    log_i("flag=%d", flag.get());
});

Example using a callback function:

void on_fparam_change(void) {
    log_i("fparam changed to %f, default value: %f",
          fparam.get(), fparam.get_default());
}

PicoSettings::Setting<float> fparam(settings, "fparam", 2.718, on_fparam_change);

Initialisation

To make the persisted values of PicoSettings available, call the begin() method:

PicoSettings settings(mqtt, "testns");
PicoSettings::Setting<int> bar(settings, "bar", 42);

void setup() {
    settings.begin();
    // print the persisted value of bar
    Serial.printf("bar = %d\n", bar.get());
}

This will fire the initial value change callback, subscribe settings for updates via the preferences/<namespace>/<name> topic, and register a preferences/<namespace>/reset topic.

Writing any value to this reset topic will wipe that namespace, and revert all values to compile-time defaults.

Publishing PicoSettings

To publish all variables in a namespace with their current values, call the publish() method.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages