2015-12-27 23:32:07 +01:00
# node Module
2016-01-05 00:58:23 +01:00
The node module provides access to system-level features such as sleep, restart and various info and IDs.
2016-01-05 01:17:20 +01:00
## node.bootreason()
Returns the boot reason code.
This is the raw code, not the new "reset info" code which was introduced in recent SDKs. Values are:
2016-01-08 20:49:56 +01:00
2016-01-08 21:17:16 +01:00
- 1, power-on
- 2, reset (software?)
- 3, hardware reset via reset pin
- 4, WDT reset (watchdog timeout)
2016-01-05 01:17:20 +01:00
####Parameters
2016-01-08 20:49:56 +01:00
none
2016-01-05 01:17:20 +01:00
####Returns
2016-01-08 20:49:56 +01:00
the boot reason code (number)
2016-01-05 01:17:20 +01:00
####Example
```lua
rsn = node.bootreason()
```
2015-12-27 23:32:07 +01:00
2016-01-08 20:49:56 +01:00
## node.chipid()
2015-12-27 23:32:07 +01:00
2016-01-08 20:49:56 +01:00
Returns the ESP chip ID.
2015-12-27 23:32:07 +01:00
####Parameters
2016-01-08 20:49:56 +01:00
none
####Returns
chip ID (number)
####Example
```lua
id = node.chipid();
```
## node.compile()
Compiles a Lua text file into Lua bytecode, and saves it as .lc file.
####Parameters
`filename` name of Lua text file
2015-12-27 23:32:07 +01:00
####Returns
`nil`
####Example
```lua
2016-01-08 20:49:56 +01:00
file.open("hello.lua","w+")
file.writeline([[print("hello nodemcu")]])
file.writeline([[print(node.heap())]])
file.close()
node.compile("hello.lua")
dofile("hello.lua")
dofile("hello.lc")
2015-12-27 23:32:07 +01:00
```
2016-01-08 20:49:56 +01:00
2015-12-27 23:32:07 +01:00
## node.dsleep()
2016-01-08 20:49:56 +01:00
Enters deep sleep mode, wakes up when timed out.
2015-12-27 23:32:07 +01:00
2016-01-05 02:26:41 +01:00
The maximum sleep time is 4294967295us, ~71 minutes. This is an SDK limitation.
Firmware from before 05 Jan 2016 have a maximum sleeptime of ~35 minutes.
2016-01-08 20:49:56 +01:00
!!! note "Note:"
2015-12-27 23:32:07 +01:00
2016-01-08 20:49:56 +01:00
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.
2015-12-27 23:32:07 +01:00
####Parameters
2016-01-08 20:49:56 +01:00
- `us` number (integer) or `nil` , sleep time in micro second. If `us == 0` , it will sleep forever. If `us == nil` , will not set sleep time.
2015-12-27 23:32:07 +01:00
2016-01-08 20:49:56 +01:00
- `option` number (integer) or `nil` . If `nil` , it will use last alive setting as default option.
- 0, init data byte 108 is valuable
- \> 0, init data byte 108 is valueless
- 0, RF_CAL or not after deep-sleep wake up, depends on init data byte 108
- 1, RF_CAL after deep-sleep wake up, there will belarge current
- 2, no RF_CAL after deep-sleep wake up, there will only be small current
- 4, disable RF after deep-sleep wake up, just like modem sleep, there will be the smallest current
2015-12-27 23:32:07 +01:00
####Returns
`nil`
####Example
```lua
--do nothing
node.dsleep()
--sleep μs
node.dsleep(1000000)
--set sleep option, then sleep μs
node.dsleep(1000000, 4)
--set sleep option only
node.dsleep(nil,4)
```
2016-01-05 00:58:23 +01:00
2016-01-08 20:49:56 +01:00
## node.flashid()
2016-01-05 00:58:23 +01:00
2016-01-08 20:49:56 +01:00
Returns the flash chip ID.
2016-01-05 00:58:23 +01:00
####Parameters
2016-01-08 20:49:56 +01:00
none
2016-01-05 00:58:23 +01:00
####Returns
2016-01-08 20:49:56 +01:00
flash ID (number)
2016-01-05 00:58:23 +01:00
####Example
```lua
2016-01-08 20:49:56 +01:00
flashid = node.flashid();
2016-01-05 00:58:23 +01:00
```
2016-01-08 20:49:56 +01:00
## node.heap()
2016-01-05 00:58:23 +01:00
2016-01-08 20:49:56 +01:00
Returns the current available heap size in bytes. Note that due to fragmentation, actual allocations of this size may not be possible.
2016-01-05 00:58:23 +01:00
####Parameters
2016-01-08 20:49:56 +01:00
none
2016-01-05 00:58:23 +01:00
####Returns
2016-01-08 20:49:56 +01:00
system heap size left in bytes (number)
2016-01-05 00:58:23 +01:00
####Example
```lua
2016-01-08 20:49:56 +01:00
heap_size = node.heap();
2016-01-05 00:58:23 +01:00
```
2016-01-08 20:49:56 +01:00
## node.info()
2016-01-05 00:58:23 +01:00
2016-01-08 20:49:56 +01:00
Returns NodeMCU version, chipid, flashid, flash size, flash mode, flash speed.
2016-01-05 00:58:23 +01:00
####Parameters
2016-01-08 20:49:56 +01:00
none
2016-01-05 00:58:23 +01:00
####Returns
2016-01-08 20:49:56 +01:00
- `majorVer` (number)
- `minorVer` (number)
- `devVer` (number)
- `chipid` (number)
- `flashid` (number)
- `flashsize` (number)
- `flashmode` (number)
- `flashspeed` (number)
2016-01-05 00:58:23 +01:00
####Example
```lua
2016-01-08 20:49:56 +01:00
majorVer, minorVer, devVer, chipid, flashid, flashsize, flashmode, flashspeed = node.info()
print("NodeMCU "..majorVer.."."..minorVer.."."..devVer)
2016-01-05 00:58:23 +01:00
```
2016-01-08 20:49:56 +01:00
## node.input()
2016-01-05 00:58:23 +01:00
2016-01-08 20:49:56 +01:00
Submits a string to the Lua interpreter. Similar to `pcall(loadstring(str))` , but without the single-line limitation.
2016-01-05 00:58:23 +01:00
2016-01-08 20:49:56 +01:00
!!! note "Note:"
This function only has an effect when invoked from a callback. Using it directly on the console **does not work** .
2016-01-05 00:58:23 +01:00
####Parameters
2016-01-08 20:49:56 +01:00
`str` Lua chunk
2016-01-05 00:58:23 +01:00
####Returns
`nil`
####Example
```lua
2016-01-08 20:49:56 +01:00
sk:on("receive", function(conn, payload) node.input(payload) end)
2016-01-05 00:58:23 +01:00
```
2016-01-08 20:49:56 +01:00
2016-01-05 00:58:23 +01:00
####See also
2016-01-08 20:49:56 +01:00
[`node.output()` ](#nodeoutput )
## node.key() --deprecated
2016-01-05 00:58:23 +01:00
2016-01-08 20:49:56 +01:00
Defines action to take on button press (on the old devkit 0.9), button connected to GPIO 16.
2016-01-05 00:58:23 +01:00
This function is only available if the firmware was compiled with DEVKIT_VERSION_0_9 defined.
####Parameters
2016-01-08 20:49:56 +01:00
- `type` : type is either string "long" or "short". long: press the key for 3 seconds, short: press shortly(less than 3 seconds)
- `function` : user defined function which is called when key is pressed. If nil, remove the user defined function. Default function: long: change LED blinking rate, short: reset chip
2016-01-05 00:58:23 +01:00
####Returns
`nil`
####Example
```lua
2016-01-08 20:49:56 +01:00
node.key("long", function() print('hello world') end)
2016-01-05 00:58:23 +01:00
```
####See also
2016-01-08 21:17:16 +01:00
[`node.led()` ](#nodeled-deprecated )
2016-01-05 00:58:23 +01:00
2016-01-08 20:49:56 +01:00
## node.led() --deprecated
2016-01-05 00:58:23 +01:00
2016-01-08 21:17:16 +01:00
Sets the on/off time for the LED (on the old devkit 0.9), with the LED connected to GPIO16, multiplexed with [`node.key()` ](#nodekey-deprecated ).
2016-01-05 00:58:23 +01:00
2016-01-08 20:49:56 +01:00
This function is only available if the firmware was compiled with DEVKIT_VERSION_0_9 defined.
2016-01-05 00:58:23 +01:00
####Parameters
2016-01-08 20:49:56 +01:00
- `low` LED off time, LED keeps on when low=0. Unit: milliseconds, time resolution: 80~100ms< br />
- `high` LED on time. Unit: milliseconds, time resolution: 80~100ms
2016-01-05 00:58:23 +01:00
####Returns
`nil`
####Example
```lua
2016-01-08 20:49:56 +01:00
-- turn led on forever.
node.led(0)
2016-01-05 00:58:23 +01:00
```
####See also
2016-01-08 21:17:16 +01:00
[`node.key()` ](#nodekey-deprecated )
2016-01-05 00:58:23 +01:00
## node.output()
Redirects the Lua interpreter output to a callback function. Optionally also prints it to the serial console.
!!! note "Note:"
2016-01-08 20:49:56 +01:00
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.
2016-01-05 00:58:23 +01:00
####Parameters
2016-01-08 20:49:56 +01:00
- `output_fn(str)` a function accept every output as str, and can send the output to a socket (or maybe a file).
- `serial_debug` 1 output also show in serial. 0: no serial output.
2016-01-05 00:58:23 +01:00
####Returns
`nil`
####Example
```lua
2016-01-08 20:49:56 +01:00
function tonet(str)
sk:send(str)
end
node.output(tonet, 1) -- serial also get the lua output.
2016-01-05 00:58:23 +01:00
```
```lua
2016-01-08 20:49:56 +01:00
-- a simple telnet server
s=net.createServer(net.TCP)
s:listen(2323,function(c)
con_std = c
function s_output(str)
if(con_std~=nil)
then con_std:send(str)
end
end
node.output(s_output, 0) -- re-direct output to function s_ouput.
c:on("receive",function(c,l)
node.input(l) -- works like pcall(loadstring(l)) but support multiple separate line
end)
c:on("disconnection",function(c)
con_std = nil
node.output(nil) -- un-regist the redirect output function, output goes to serial
end)
end)
2016-01-05 00:58:23 +01:00
```
####See also
2016-01-08 20:49:56 +01:00
[`node.input()` ](#nodeinput )
2016-01-05 00:58:23 +01:00
2016-01-08 20:49:56 +01:00
## node.readvdd33() --deprecated
Moved to [`adc.readvdd33()` ](adc/#adcreadvdd33 ).
2016-01-05 00:58:23 +01:00
2016-01-08 20:49:56 +01:00
## node.restart()
Restarts the chip.
2016-01-05 00:58:23 +01:00
####Parameters
2016-01-08 20:49:56 +01:00
none
2016-01-05 00:58:23 +01:00
####Returns
`nil`
####Example
```lua
2016-01-08 20:49:56 +01:00
node.restart();
2016-01-05 00:58:23 +01:00
```
2016-01-08 20:49:56 +01:00
## node.restore()
Restores system configuration to defaults. Erases all stored WiFi settings, and resets the "esp init data" to the defaults. This function is intended as a last-resort without having to reflash the ESP altogether.
2016-01-05 00:58:23 +01:00
2016-01-08 20:49:56 +01:00
This also uses the SDK function `system_restore()` , which doesn't document precisely what it erases/restores.
2016-01-05 00:58:23 +01:00
####Parameters
2016-01-08 20:49:56 +01:00
none
2016-01-05 00:58:23 +01:00
####Returns
2016-01-08 20:49:56 +01:00
`nil`
2016-01-05 00:58:23 +01:00
####Example
```lua
2016-01-08 20:49:56 +01:00
node.restore()
node.restart() -- ensure the restored settings take effect
2016-01-05 00:58:23 +01:00
```
2016-01-08 20:49:56 +01:00
## node.setcpufreq()
2016-01-05 00:58:23 +01:00
2016-01-08 20:49:56 +01:00
Change the working CPU Frequency.
2016-01-05 00:58:23 +01:00
####Parameters
2016-01-08 20:49:56 +01:00
`speed` constant 'node.CPU80MHZ' or 'node.CPU160MHZ'
2016-01-05 00:58:23 +01:00
####Returns
2016-01-08 20:49:56 +01:00
target CPU frequency (number)
2016-01-05 00:58:23 +01:00
####Example
```lua
2016-01-08 20:49:56 +01:00
node.setcpufreq(node.CPU80MHZ)
2016-01-05 00:58:23 +01:00
```
2016-01-08 20:49:56 +01:00
2016-01-05 01:17:20 +01:00
## node.stripdebug()
2016-01-08 20:49:56 +01:00
Controls the amount of debug information kept during `node.compile()` , and allows removal of debug information from already compiled Lua code.
2016-01-05 01:17:20 +01:00
Only recommended for advanced users, the NodeMCU defaults are fine for almost all use cases.
####Parameters
2016-01-08 20:49:56 +01:00
- `level`
2016-01-05 01:17:20 +01:00
- 1: don't discard debug info
- 2: discard Local and Upvalue debug info
- 3: discard Local, Upvalue and line-number debug info
2016-01-08 20:49:56 +01:00
- `function` a compiled function to be stripped per setfenv except 0 is not permitted.
2016-01-05 01:17:20 +01:00
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()` .
#### Returns
If invoked without arguments, returns the current level settings. Otherwise, `nil` is returned.
####Example
```lua
node.stripdebug(3)
node.compile('bigstuff.lua')
```
####See also
2016-01-08 20:49:56 +01:00
[`node.compile()` ](#nodecompile )