In general, the extended reset cause supercedes the raw code. The raw code is kept for backwards compatibility only. For new applications it is highly recommended to use the extended reset cause instead.
In case of extended reset cause 3 (exception reset), additional values are returned containing the crash information. These are, in order, [EXCCAUSE](https://arduino-esp8266.readthedocs.io/en/latest/exception_causes.html), EPC1, EPC2, EPC3, EXCVADDR, and DEPC.
This function can only be used in the condition that esp8266 PIN32(RST) and PIN8(XPD_DCDC aka GPIO16) are connected together. Using sleep(0) will set no wake up timer, connect a GPIO to pin RST, the chip will wake up by a falling-edge on pin RST.
Do **not** attempt to `print()` or otherwise induce the Lua interpreter to produce output from within the callback function. Doing so results in infinite recursion, and leads to a watchdog-triggered restart.
Restores system configuration to defaults using the SDK function `system_restore()`, which is described in the documentation as:
> Reset default settings of following APIs: `wifi_station_set_auto_connect`, `wifi_set_phy_mode`, `wifi_softap_set_config` related, `wifi_station_set_config` related, `wifi_set_opmode`, and APs’ information recorded by `#define AP_CACHE`.
-`duration` Sleep duration in microseconds(μs). If a sleep duration of `0` is specified, suspension will be indefinite (Range: 0 or 50000 - 268435454 μs (0:4:28.000454))
-`wake_pin` 1-12, pin to attach wake interrupt to. Note that pin 0(GPIO 16) does not support interrupts.
- If sleep duration is indefinite, `wake_pin` must be specified
- Please refer to the [`GPIO module`](gpio.md) for more info on the pin map.
-`int_type` type of interrupt that you would like to wake on. (Optional, Default: `node.INT_LOW`)
- valid interrupt modes:
-`node.INT_UP` Rising edge
-`node.INT_DOWN` Falling edge
-`node.INT_BOTH` Both edges
-`node.INT_LOW` Low level
-`node.INT_HIGH` High level
-`resume_cb` Callback to execute when WiFi wakes from suspension. (Optional)
-`preserve_mode` preserve current WiFi mode through node sleep. (Optional, Default: true)
- If true, Station and StationAP modes will automatically reconnect to previously configured Access Point when NodeMCU resumes.
- If false, discard WiFi mode and leave NodeMCU in `wifi.NULL_MODE`. WiFi mode will be restored to original mode on restart.
#### Returns
-`nil`
#### Example
```lua
--Put NodeMCU in light sleep mode indefinitely with resume callback and wake interrupt
cfg={}
cfg.wake_pin=3
cfg.resume_cb=function() print("WiFi resume") end
node.sleep(cfg)
--Put NodeMCU in light sleep mode with interrupt, resume callback and discard WiFi mode
cfg={}
cfg.wake_pin=3 --GPIO0
cfg.resume_cb=function() print("WiFi resume") end
cfg.preserve_mode=false
node.sleep(cfg)
--Put NodeMCU in light sleep mode for 10 seconds with resume callback
Controls the amount of debug information kept during [`node.compile()`](#nodecompile), and allows removal of debug information from already compiled Lua code.
If no arguments are given then the current default setting is returned. If function is omitted, this is the default setting for future compiles. The function argument uses the same rules as for `setfenv()`.
This behaves like math.random except that it uses true random numbers derived from the ESP8266 hardware. It returns uniformly distributed
numbers in the required range. It also takes care to get large ranges correct.
It can be called in three ways. Without arguments in the floating point build of NodeMCU, it returns a random real number with uniform distribution in the interval [0,1).
When called with only one argument, an integer n, it returns an integer random number x such that 1 <= x <= n. For instance, you can simulate the result of a die with random(6).
Finally, random can be called with two integer arguments, l and u, to get a pseudo-random integer x such that l <= x <= u.
#### Syntax
`node.random()`
`node.random(n)`
`node.random(l, u)`
#### Parameters
-`n` the number of distinct integer values that can be returned -- in the (inclusive) range 1 .. `n`
-`l` the lower bound of the range
-`u` the upper bound of the range
#### Returns
The random number in the appropriate range. Note that the zero argument form will always return 0 in the integer build.
-`node.egc.NOT_ACTIVE` EGC inactive, no collection cycle will be forced in low memory situations
-`node.egc.ON_ALLOC_FAILURE` Try to allocate a new block of memory, and run the garbage collector if the allocation fails. If the allocation fails even after running the garbage collector, the allocator will return with error.
-`node.egc.ON_MEM_LIMIT` Run the garbage collector when the memory used by the Lua script goes beyond an upper `limit`. If the upper limit can't be satisfied even after running the garbage collector, the allocator will return with error.
-`node.egc.ALWAYS` Run the garbage collector before each memory allocation. If the allocation fails even after running the garbage collector, the allocator will return with error. This mode is very efficient with regards to memory savings, but it's also the slowest.
-`level` in the case of `node.egc.ON_MEM_LIMIT`, this specifies the memory limit.
Returns the function reference for a function in the LFS (Lua Flash Store).
#### Syntax
`node.flash.index()`
#### Parameters
None
#### Returns
- In the case where the LFS in not loaded, `node.flash.index` evaluates to `nil`
- If the LFS is loaded, this returns the index function within the LFS which indexes its contents. This index function can itself to called to interrogate or access the LFS contents:
- In that case where the function is called with the name of a valid module in the LFS, the function is returned in the same way the `load()` and the other Lua load functions do.
- Otherwise the function returns a list of module names in the LFS. Note that the first entry in the list is the Unix datetime of he build.
## node.flash.reload()
Reload the LFS (Lua Flash Store) with the flash image provided. Flash images are generated on the host machine using the `luac.cross`commnad.
#### Syntax
`node.flash.rebuild(imageName)`
#### Parameters
`imageName` The of name of a image file in the filesystem to be loaded into the LFS.
#### Returns
_Not applicable_. The ESP will load the LFS image and immediately reboot. Control is not returned to the calling application.