CloudConfigRK
|
Store configuration settings locally with the ability to set from the cloud
Github repository: https://github.com/rickkas7/CloudConfigRK License: MIT
The storage methods provide a way to store the data locally so it's available immediately after restart, and to avoid having to get the data from the cloud as frequently. You can set an adjustable refresh period, if desired, or only fetch once.
Retained memory is preserved:
It is not preserved:
There is around 3K of retained memory on most devices.
To use retained memory you create a global retained variable to store the data. The <256> parameter is the maximum size of the JSON data in characters. This can be up to 622 for function or publish, the maximum size of the payload. And equal amount of RAM is reserved, so you don't want to make this excessively large.
From setup() initialize your updateMethod (function, subscription, webhook, etc.) and your storage method. In this case, retained memory.
Don't forget to call from loop() as well!
Emulated EEPROM is a good choice on Gen2 devices because it's preserved:
There is around 3K of emulated EEPROM on most devices.
On Gen 3 devices, it may make more sense to use the flash file system file option instead. On the Argon, Boron, B Series SoM, and Tracker SoM, emulated EEPROM is just a file on the flash file system, so there is no efficiency advantage to using EEPROM over files.
From setup() initialize your updateMethod (function, subscription, webhook, etc.) and your storage method. In this case, EEPROM.
The <256> parameter is the maximum size of the JSON data in characters. This can be up to 622 for function or publish, the maximum size of the payload. And equal amount of RAM is reserved, so you don't want to make this excessively large.
When using EEPROM you must specify the start offset (EEPROM_OFFSET, 0, in this example). Note that the total EEPROM required is sizeof(CloudConfigDataHeader) + SIZE bytes as there is a 20 byte header before the data. Thus for the <256> example, it will use 256 + 20 = 276 bytes of EEPROM so you can't use anything from offset 0 to 276. Of course you can change the offset to a different part of EEPROM if you are using offset 0 already.
On Gen 3 devices (Argon, Boron, B Series SoM, and Tracker SoM) running Device OS 2.0.0 or later, you can store the data in a file on the flash file system.
The file system is 2 MB, except on the Tracker SoM, where it's 4 MB.
Using a file is easy, just pass the pathname to the CloudConfigStorageFile constructor.
The <256> parameter is the maximum size of the JSON data in characters. This can be up to 622 for function or publish, the maximum size of the payload. And equal amount of RAM is reserved, so you don't want to make this excessively large.
This option does not allow for updating from the cloud, but does provide a way to store the configuration as a string constant in the program flash. This is convenient when you want to be able to use a similar code base with both hardcoded and cloud-based configuration settings.
The data update methods allow the data to be updated from the cloud.
The configuration data can be updated by making a Particle.function call to the device. Since it's a "push" method, there's no way for the device to request it be sent the configuration data, so it's best suited for EEPROM or file storage methods.
Function update is a good choice if:
Subscription is a good choice if:
Note that devices must be claimed to an account to use subscriptions; you cannot use subscriptions with unclaimed product devices.
Since it's a "push" method, there's no way for the device to request it be sent the configuration data, so it's best suited for EEPROM or file storage methods. The webhook method is based on subscription, but works as both "push" and "pull" so it can both request the current configuration and receive configuration updates spontaneously.
There are two examples of using a webhook: Device Notes and Google Sheets. You can easily base your own webhook-based system for getting configuration data from your own server using this method.
The Device Notes example allows you to store the configuration data for each device in the Device Notes field in the console. One of the major benefits is that you don't need any external services and you can edit the data in the Particle console, though you do need to edit the JSON data as text. See also the Google Sheets example, below.
https://api.particle.io/v1/devices/{{PARTICLE_DEVICE_ID}}
access_token
> your access token that you created above.Be sure to use triple curly brackets for this!
This is the firmware to test device notes. It stores the data in retained memory, but you could also use EEPROM or File.
This is a great option for storing per-device configuration for a set of devices in a Google Sheets spreadsheet. It makes adding and updating configuration easy, and you can see the values for your fleet at a glance.
The setup process is a bit involved, so it's in a separate page. However, once you've got it set up, updating configuration values is as easy as editing a Google spreadsheet. Also, it uses Apps Script, which is included in your G Suite subscription, so you don't need to purchase separate Google Cloud computing resources!
The second part of the tutorial adds the Update Selected button so you can send the configuration to selected devices (that are online) immediately!
This storage method doesn't use the cloud at all and instead has the configuration as a static string in code.
This is mainly so you can use the same code base for cloud or local storage, swappable at compile time. It's a bit of overkill for normal use.