英文:
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 "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
得分: 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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论