Saving Yoctopuce device settings

Saving Yoctopuce device settings

We recently realized, during a support discussion with a customer, that the long-term preservation of settings on Yoctopuce devices was not detailed in the documentation and deserved some explanations. So here's a little addendum...




Saving settings in the flash memory

There are two types of settings in Yoctopuce devices. On the one hand, there are the current values, which form a kind of transient state of the device, such as the position of a relay or the output current of a 4-20mA transmitter, which do not need to be saved in the long term. On the other hand, there are configuration parameters, such as the logical name of a device or the unit of measurement of a sensor, for which it is desirable for the device to be able to retain a persistent value.

It is therefore possible to save configuration parameters in the device's flash memory, to be restored automatically when the device is switched on or restarted, without any external intervention.

There is, however, a small subtlety. When one of these configuration parameters is changed, the effect is immediate, but saving to flash memory is not automatic and instantaneous: it's only done when the user explicitly chooses to save the modifications. This corresponds to the behavior of the device configuration page in the VirtualHub's web interface: settings are applied directly, but only saved in the flash memory when the "Save" button is pressed. If the user changes their mind and presses the "Cancel" button, the opposite happens: the last settings saved in the flash memory are reloaded, and unsaved changes are cancelled.

Settings are saved only when Save is clicked
Settings are saved only when Save is clicked


If neither button is pressed, and the user just closes the browser, the settings will remain as they are, but will be lost the next time the module is restarted.

The programming library works on the same principle: configuration commands must be terminated by a call to the module.saveToFlash() method to save the settings in the device's flash memory. A single call to this method is all that's needed at the very end of the configuration. If you wish to implement a user interface similar to VirtualHub's with a Cancel function, you can use the module.revertFromFlash() method to implement cancellation.

The behavior of the command-line library, which you can use in your shell scripts, is identical. You can make a series of configuration changes and end with a call to saveToFlash:

YHumidity METEOMK1-2AE9F.humidity set_unit "g/m3"
YTemperature METEOMK1-2AE9F.temperature set_unit "°C"
YModule METEOMK1-2AE9F.module saveToFlash


There is, however, a useful shortcut for making a saveToFlash directly after applying a single setting, the -s option:

YHumidity -s METEOMK1-2AE9F.humidity set_unit "g/m3"



If you're in any doubt as to whether or not a setting is a configuration parameter saved in the flash memory, you can make sure of this thanks to the fact that the documentation for configuration functions systematically states: Remember to call the saveToFlash() method of the module if the modification must be kept.

A word of caution

If you need to make frequent changes to a configuration parameter that can be saved in the flash memory (e.g. the frequency in the Yocto-PWM-Tx signal generator), don't call saveToFlash unnecessarily for each change, so as not to wear out the flash memory in vain. This type of memory has a limited lifespan of a few tens of thousands of cycles. So only save parameters in the flash memory if you really want them to be preserved when the device is restarted.

Saving parameters to a file

The Yoctopuce library and the VirtualHub tool offer a different way of saving all settings, outside of the device, in the form of a JSON file, for later reloading. This makes it possible to backup the settings, or to transfer settings from one device to another. If the device has a file system, the files stored on the device are included in the JSON export, as hexadecimal strings.

In the VirtualHub's Web interface, the Export settings button lets you download this JSON file for a device, to save it on a disk on the machine running the browser.

Buttons for exporting and re-importing settings
Buttons for exporting and re-importing settings


The programming library offers an identical function that returns a memory buffer with the contents of the same JSON file: the module.get_allSettings() method.

If you're using the library from the command line, you can use the same function to obtain a corresponding JSON file. We have provided more detailed instructions on how to do this in a previous post.

JSON settings can later be reapplied to the same device or to a similar one, using the set_allSettingsAndFiles() method or the set_allSettings() method. Note that, as the JSON file contains not only the configuration parameters but also the device's current state values, these will also be restored by this method. For example, if a relay was active when the JSON file was created, it will be switched to the active state when the JSON file is restored.

Add a comment No comment yet Back to blog












Yoctopuce, get your stuff connected.