ESP32 React environment - storing variable to non vol flash memory

89 Views Asked by At

im trying to store a variable chainCounter into non vol memory on an ESP32. I understand i need to use the Preferences library and some of its associated commands. however im very much a noob ... and experience to date with modifiying and building projects from other examples. the project build and runs ok, (before adding the additional for storing variable to non vol)

below is an extract of the main.cpp (a lot of code deleted for brevity) the relevant parts are shown . my questions pertain to Q: where do the respective statements have to go? ... above or below void setup() { q: do i have the variable defined correctly for the intended use? (the counter is an integer, & can be + or -)

appreciate any insights and assistance //

#include "sensesp/sensors/digital_output.h"
#include "sensesp/controllers/smart_switch_controller.h"
#include "sensesp/sensors/digital_input.h"
#include "sensesp/sensors/sensor.h"
#include "sensesp/signalk/signalk_output.h"
#include "sensesp/system/lambda_consumer.h"
#include "sensesp/transforms/debounce.h"
#include "sensesp/transforms/linear.h"
#include "sensesp/transforms/lambda_transform.h"
#include "sensesp/transforms/moving_average.h"
#include "sensesp_app.h"
#include "sensesp_app_builder.h"
#include <Preferences.h>

#include <math.h>

using namespace sensesp;

reactesp::ReactESP app;

Preferences preferences;

// Retrieves last ChainCounter value from nonvolatile storage
//  if the key does not exist, return a default value of 0.
// Note: Key name is limited to 15 chars.
// "counter"is the name of the key used to store chainCounter in ESP32 flash (aka Non Vol)

int chainCounter = preferences.getInt("counter", 0);



// The setup function performs one-time application initialization.
void setup() {

//  ****  not as yet working - for storing chainCounter into non vol flash memeory *****************


preferences.begin("my_app", false);

// test ...  Store the counter to the Preferences
///         where should this go?
          
preferences.putInt("counter", chainCounter);

// Close the Preferences... where does this go? 
preferences.end();


// other parts deleted for brevity 

//  ********************

// Start networking, SK server connections and other SensESP internals
sensesp_app->start();
}

void loop() { app.tick(); }

i have tried various versions of the statements in different places .. when program executes - it always initialises with chainCounter = 0 which implies that its not finding anything saved in non vol.

1

There are 1 best solutions below

1
Tarmo On

Variable chainCounter is initialized when the C++ runtime executes - it's essentially one of the first things that happens and the Preferences library is not started yet. So you always start with chainCounter set to 0. Much later Arduino framework executes setup() and starts the Preferences library. Only after that can you attempt to read or write the value from NVS.

A side note - you're not checking your return codes. Any NVS operation can fail (especially begin()) and you don't currently know if or where that happens.