ESP32 React环境 – 将变量存储到非易失性闪存内存

huangapple go评论55阅读模式
英文:

ESP32 React environment - storing variable to non vol flash memory

问题

我正在尝试将变量 chainCounter 存储到 ESP32 的非易失性内存中。我理解我需要使用 Preferences 库以及其中的一些相关命令。然而,我是一个新手,迄今为止的经验都是修改和构建来自其他示例的项目。在添加用于将变量存储到非易失性内存的额外部分之前,该项目构建和运行良好。

以下是 main.cpp 的摘录(为简洁起见删除了大量代码),相关部分如下。我的问题与以下内容有关:
Q: 这些语句应放在哪里?... 在 void setup() 之上还是之下?
Q: 我是否正确定义了用于预期用途的变量?(计数器是一个整数,可以是正数也可以是负数)

感激任何见解和帮助

#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;

// 从非易失性存储中检索上次的 ChainCounter 值
// 如果该键不存在,返回默认值 0。
// 注意:键名最多限制为 15 个字符。
// "counter" 是用于在 ESP32 闪存中存储 chainCounter 的键的名称(也称为 Non Vol)

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

// 设置函数执行一次性应用程序初始化。
void setup() {

    // 尚未工作 - 用于将 chainCounter 存储到非易失性闪存内存中

    preferences.begin("my_app", false);

    // 测试... 将计数器存储到 Preferences 中
    // 这应该放在哪里?

    preferences.putInt("counter", chainCounter);

    // 关闭 Preferences... 这应该放在哪里?
    preferences.end();

    // 其他部分已删除以简化代码

    // 启动网络、SK 服务器连接和其他 SensESP 内部操作
    sensesp_app->start();
}

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

我已经尝试在不同位置尝试了各种语句版本。当程序执行时,它总是初始化为 chainCounter = 0,这意味着它未找到在非易失性存储中保存的内容。

英文:

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 &quot;sensesp/sensors/digital_output.h&quot;
#include &quot;sensesp/controllers/smart_switch_controller.h&quot;
#include &quot;sensesp/sensors/digital_input.h&quot;
#include &quot;sensesp/sensors/sensor.h&quot;
#include &quot;sensesp/signalk/signalk_output.h&quot;
#include &quot;sensesp/system/lambda_consumer.h&quot;
#include &quot;sensesp/transforms/debounce.h&quot;
#include &quot;sensesp/transforms/linear.h&quot;
#include &quot;sensesp/transforms/lambda_transform.h&quot;
#include &quot;sensesp/transforms/moving_average.h&quot;
#include &quot;sensesp_app.h&quot;
#include &quot;sensesp_app_builder.h&quot;
#include &lt;Preferences.h&gt;

#include &lt;math.h&gt;

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.
// &quot;counter&quot;is the name of the key used to store chainCounter in ESP32 flash (aka Non Vol)

int chainCounter = preferences.getInt(&quot;counter&quot;, 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(&quot;my_app&quot;, false);

// test ...  Store the counter to the Preferences
///         where should this go?
          
preferences.putInt(&quot;counter&quot;, 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-&gt;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

得分: 1

Variable chainCounter 在 C++ 运行时执行时进行初始化 - 它实际上是发生的第一件事之一,而 Preferences 库尚未启动。因此,您始终从 chainCounter 设置为 0 开始。 Arduino 框架在很久以后执行 setup() 并启动 Preferences 库。只有在那之后,您才能尝试从 NVS 读取或写入值。

另外一点 - 您没有检查返回代码。任何 NVS 操作都可能失败(特别是 begin()),而您目前不知道是否失败或失败发生在何处。

英文:

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.

huangapple
  • 本文由 发表于 2023年2月24日 13:39:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/75552971.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定