Reset reasons on ESP8266

#1

On ESP32 it is possible to get the reset reason by inserting the following C-code line:
esp_reset_reason_t resetReason = esp_reset_reason();
Is something similar possible on the ESP8266, too?

Thanks for help!
Rolf

#2

Sure. That’s what I’m using:

#include <user_interface.h>

#include "common/cs_dbg.h"

#if 0
/*
 * https://github.com/esp8266/Arduino/blob/899893294c50ae304148d3d1b7c589d34e44a87c/tools/sdk/include/user_interface.h#L51-L59
 */
enum rst_reason {
    REASON_DEFAULT_RST      = 0,    /* normal startup by power on */
    REASON_WDT_RST          = 1,    /* hardware watch dog reset */
    REASON_EXCEPTION_RST    = 2,    /* exception reset, GPIO status won’t change */
    REASON_SOFT_WDT_RST     = 3,    /* software watch dog reset, GPIO status won’t change */
    REASON_SOFT_RESTART     = 4,    /* software restart ,system_restart , GPIO status won’t change */
    REASON_DEEP_SLEEP_AWAKE = 5,    /* wake up from deep-sleep */
    REASON_EXT_SYS_RST      = 6     /* external system reset */
};

struct rst_info *rtc_info = system_get_rst_info();
#endif

struct reason_as_string {
  enum rst_reason reason;
  const char *string;
};

static struct reason_as_string reasons[] = {
    {REASON_DEFAULT_RST, "REASON_DEFAULT_RST"},
    {REASON_WDT_RST, "REASON_WDT_RST"},
    {REASON_EXCEPTION_RST, "REASON_EXCEPTION_RST"},
    {REASON_SOFT_WDT_RST, "REASON_SOFT_WDT_RST"},
    {REASON_SOFT_RESTART, "REASON_SOFT_RESTART"},
    {REASON_DEEP_SLEEP_AWAKE, "REASON_DEEP_SLEEP_AWAKE"},
    {REASON_EXT_SYS_RST, "REASON_EXT_SYS_RST"},
};

static const char *esp_reset_reason_stringify(enum rst_reason reset_reason) {
  const struct reason_as_string *p = reasons;
  for (size_t i = 0; i < sizeof(reasons) / sizeof(reasons[0]); ++i, ++p) {
    if (reset_reason == p->reason) {
      return p->string;
    }
  }
  return "N/A";
}

const char *mgos_get_reset_reason_string(void) {
  struct rst_info *rst_info = system_get_rst_info();
  return esp_reset_reason_stringify(rst_info->reason);
}
1 Like
#3

Hi nliviu,

Thanks for this information. But - unfortunately - I cannot link this code, because ‘system_get_rst_info’ results in an ‘undefined reference’.
What library I have to take?

Kind regards,
Rolf

#4

#include <user_interface.h> It’s in the snippet above.

#5

I already had this include in my code, but this did not help. I still get ‘… undefined reference to `system_get_rst_info()’…’.
It seems to be a linker problem and this include just helps for the compiler, but not for the linker.
We had this discussion lately: I have problems to find out, what libraries are taken by the build server. So do I in this case.

By the way: The build server is extremely slow since yesterday morning. One compiler run takes several minutes …

#6

No library needed. system_get_rst_info is used in the MongooseOS code too.

It looks like you are compiling C++ code. In this case, you need to add the __cplusplus guard:

#ifdef __cplusplus
extern "C" {
#endif
#include <user_interface.h>
#ifdef __cplusplus
}
#endif
#7

This solves the problem! Thanks a lot for your help!