简体   繁体   English

从ESP8266 SPIFFS中存储的变量设置全局对象

[英]Set global object from variable stored in SPIFFS in ESP8266

This is how I set this global object in the past. 这就是我过去设置此全局对象的方式。

MqttClient mqtt("192.168.1.8", 1883, msgRev);

I want to retrieve the IP address which is stored in spiffs and use it to declare this global object. 我想检索存储在spiffs中的IP地址,并用它声明此全局对象。

MqttClient mqtt(AppSettings.MQTTUWL, 1883, msgRev); 

AppSettings contain the structure of spiffs. AppSettings包含Spiff的结构。

Here is my init(), 这是我的init(),

void init()
{
    spiffs_mount(); // Mount file system, in order to work with files
    AppSettings.load();
    //...
}

The problem is during the declaration of the object code, AppSettings is not yet loaded. 问题是在声明目标代码期间,尚未加载AppSettings。 How should I declare the global object such that it is able to retrieve the value from AppSettings? 我应该如何声明全局对象,使其能够从AppSettings中检索值?

I am using SMING framework on ESP8266. 我在ESP8266上使用SMING框架。

You would have to either declare the MqttClient as a pointer, and initialise it after mounting SPIFFS, or put the SPIFFS mounting code inside the constructor of the client class (Which probably isn't what you want). 您可能必须声明MqttClient作为指针,并在安装SPIFFS之后对其进行初始化,或者将SPIFFS安装代码放入客户端类的构造函数中(这可能不是您想要的)。

To do the former, your code would look something like this: 为此,您的代码将如下所示:

MqttClient *mqtt;

void init()
{
   spiffs_mount(); // Mount file system, in order to work with files
   AppSettings.load();
   mqtt = new MqttClient(AppSettings.MQTTUWL, 1883, msgRev);
   //...
}

This will only create the client after mounting has completed. 这只会在安装完成后创建客户端。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM