c++esp8266sming

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.

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

AppSettings contain the structure of spiffs.

Here is my 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. How should I declare the global object such that it is able to retrieve the value from AppSettings?

I am using SMING framework on ESP8266.


Solution

  • 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).

    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.