Write/Read Object to RTC Memory

Hi,

I'm trying to retain my MQTT connection through deep-sleeps to avoid re-authentication on every wake up.
I therefore attempt to write the PubSubClient object to the RTC memory...
I'm able to write/read to memory, but I have an issue with the object and the addressing of it...

I write my object like this:

typedef struct {
  uint32_t crc32;  // 4 bytes
  uint8_t mqtt[76];
} rtcMqttStruct;

rtcMqttStruct rtcMqtt;

WiFiClient espClient;
PubSubClient client(espClient);

memcpy( rtcMqtt.mqtt, &client, 76 ); // Copy 76 bytes of PubSubClient object
rtcMqtt.crc32 = calculateCRC32( ((uint8_t*)&rtcMqtt) + 4, sizeof( rtcMqtt ) - 4 );
ESP.rtcUserMemoryWrite( 16, (uint32_t*)&rtcMqtt, sizeof( rtcMqtt ) );

Afterwards I attempt to read with the following:

  if( ESP.rtcUserMemoryRead( 16, (uint32_t*)&rtcMqtt, sizeof( rtcMqtt ) ) ) {
    uint32_t crc = calculateCRC32( ((uint8_t*)&rtcMqtt) + 4, sizeof( rtcMqtt ) - 4 );
    if( crc == rtcMqtt.crc32 ) {
     Serial.println("Found valid MQTT connection in RTC");
     rtcMqttValid = true;
     memcpy( &client , rtcMqtt.mqtt, 76 ); // Copy 76 bytes of PubSubClient object
    }
  }

Using the value read from RTC memory, return a state of -3 (Connection lost) for the MQTT connection...
Any help is appreciated to get this fixed.

Which RTC do you have?

Has it got enough user RAM to store all of that data?

It's an ESP8266, so I have 512 bytes of RTC memory available.

The PubSubClient object is barely 76 bytes, so I have plenty of space to store it;

Note that the RTC is not a Real Time Clock as might have been inferred from the original post

I'm not using the clock function but the memory function. this can be clearly identified in the code snippets...

tstas:
I'm not using the clock function but the memory function. this can be clearly identified in the code snippets...

Code snippets or not, it would have been much better to explain in English what you were trying to do

Why such memory is referred to as "RTC memory" is beyond me and has caused confusion here before

1 Like

Are you sure that an object of the PubSubClient class is trivially copyable? Does it contain any pointers to objects in memory that are not maintained during deep sleep?

gfvalvo:
Are you sure that an object of the PubSubClient class is trivially copyable? Does it contain any pointers to objects in memory that are not maintained during deep sleep?

Good question, I honestly have no clue...
If the esp8266 is not using deep sleep, one single connect is sufficient to do lots of publish afterwards. So I guess there are states maintained in that PubSubClient object.
My goal is to store that within the memory so I can publish across deep sleep cycles.
(during deep sleep, the flash memory on zsp is turned off, therefore any state is lost. Only solution to keep state across deep-sleep cycles is the RTC memory)
BTW, "RTC Memory" is the terminology used in the ESP8266 documentation. No clue why they named it as such.

tstas:
Good question, I honestly have no clue...

I've never used PubSubClient nor deep sleep on ESP8266. But, my guess is that the answer lies in having a clue regarding that. Or, perhaps, the behavior of the WiFi subsystem and connections to servers during deep sleep.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.