Since | Origin / Contributor | Maintainer | Source |
---|---|---|---|
2014-12-22 | Zeroday | Zeroday | node.c |
The node module provides access to system-level features such as sleep, restart and various info and IDs.
Returns the boot reason and extended reset info.
The first value returned is the raw code, not the new "reset info" code which was introduced in recent SDKs. Values are:
- 1, power-on
- 2, reset (software?)
- 3, hardware reset via reset pin
- 4, WDT reset (watchdog timeout)
The second value returned is the extended reset cause. Values are:
- 0, power-on
- 1, hardware watchdog reset
- 2, exception reset
- 3, software watchdog reset
- 4, software restart
- 5, wake from deep sleep
- 6, external reset
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, EPC1, EPC2, EPC3, EXCVADDR, and DEPC.
node.bootreason()
none
rawcode, reason [, exccause, epc1, epc2, epc3, excvaddr, depc ]
_, reset_reason = node.bootreason()
if reset_reason == 0 then print("Power UP!") end
Returns the ESP chip ID.
node.chipid()
none
chip ID (number)
Compiles a Lua text file into Lua bytecode, and saves it as .lc file.
node.compile("file.lua")
filename
name of Lua text file
nil
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")
Enters deep sleep mode, wakes up when timed out.
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.
!!! caution
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.
node.dsleep(us, option)
-
us
number (integer) ornil
, sleep time in micro second. Ifus == 0
, it will sleep forever. Ifus == nil
, will not set sleep time. -
option
number (integer) ornil
. Ifnil
, 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
nil
--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)
wifi.suspend()
wifi.resume()
node.sleep()
Returns the flash chip ID.
node.flashid()
none
flash ID (number)
Returns the current available heap size in bytes. Note that due to fragmentation, actual allocations of this size may not be possible.
node.heap()
none
system heap size left in bytes (number)
Returns NodeMCU version, chipid, flashid, flash size, flash mode, flash speed.
node.info()
none
majorVer
(number)minorVer
(number)devVer
(number)chipid
(number)flashid
(number)flashsize
(number)flashmode
(number)flashspeed
(number)
majorVer, minorVer, devVer, chipid, flashid, flashsize, flashmode, flashspeed = node.info()
print("NodeMCU "..majorVer.."."..minorVer.."."..devVer)
Submits a string to the Lua interpreter. Similar to pcall(loadstring(str))
, but without the single-line limitation.
!!! attention
This function only has an effect when invoked from a callback. Using it directly on the console **does not work**.
node.input(str)
str
Lua chunk
nil
sk:on("receive", function(conn, payload) node.input(payload) end)
Defines action to take on button press (on the old devkit 0.9), button connected to GPIO 16.
This function is only available if the firmware was compiled with DEVKIT_VERSION_0_9 defined.
node.key(type, function())
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
nil
node.key("long", function() print('hello world') end)
Sets the on/off time for the LED (on the old devkit 0.9), with the LED connected to GPIO16, multiplexed with node.key()
.
This function is only available if the firmware was compiled with DEVKIT_VERSION_0_9 defined.
node.led(low, high)
low
LED off time, LED keeps on when low=0. Unit: milliseconds, time resolution: 80~100mshigh
LED on time. Unit: milliseconds, time resolution: 80~100ms
nil
-- turn led on forever.
node.led(0)
Redirects the Lua interpreter output to a callback function. Optionally also prints it to the serial console.
!!! caution
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.
node.output(function(str), serial_debug)
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.
nil
function tonet(str)
sk:send(str)
end
node.output(tonet, 1) -- serial also get the lua output.
-- 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)
Moved to adc.readvdd33()
.
Restarts the chip.
node.restart()
none
nil
Restores system configuration to defaults using the SDK function system_restore()
, which doesn't document precisely what it erases/restores.
node.restore()
none
nil
node.restore()
node.restart() -- ensure the restored settings take effect
Change the working CPU Frequency.
node.setcpufreq(speed)
speed
constant 'node.CPU80MHZ' or 'node.CPU160MHZ'
target CPU frequency (number)
node.setcpufreq(node.CPU80MHZ)
Put NodeMCU in light sleep mode to reduce current consumption. !!! note "Note:"
NodeMCU can not enter light sleep mode if wifi is suspended!
node.sleep({wake_gpio[,int_type, resume_cb, preserve_mode]})
wake_gpio
0-15, gpio to attach interrupt to. Note that gpio16 does not support interrupts.int_type
type of interrupt that you would like to wake on. (Optional, Default:node.INT_LOW
)- valid interrupt modes:
node.INT_UP
Rising edgenode.INT_DOWN
Falling edgenode.INT_BOTH
Both edgesnode.INT_LOW
Low levelnode.INT_HIGH
High levelresume_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.
nil
--Put NodeMCU in light sleep mode with resume callback
cfg={}
cfg.wake_gpio=0
cfg.resume_cb=function() print("WiFi resume") end
node.sleep(cfg)
--Put NodeMCU in light sleep mode with interrupt triggering on both edges, resume callback and discard WiFi mode
cfg={}
cfg.wake_gpio=0
cfg.int_type=node.INT_BOTH
cfg.resume_cb=function() print("WiFi resume") end
cfg.preserve_mode=false
node.sleep(cfg)
wifi.suspend()
wifi.resume()
node.dsleep()
Controls the amount of debug information kept during node.compile()
, and allows removal of debug information from already compiled Lua code.
Only recommended for advanced users, the NodeMCU defaults are fine for almost all use cases.
####Syntax
node.stripdebug([level[, function]])
level
- 1, don't discard debug info
- 2, discard Local and Upvalue debug info
- 3, discard Local, Upvalue and line-number debug info
function
a compiled function to be stripped per setfenv except 0 is not permitted.
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()
.
If invoked without arguments, returns the current level settings. Otherwise, nil
is returned.
node.stripdebug(3)
node.compile('bigstuff.lua')
Controls whether the debugging output from the Espressif SDK is printed. Note that this is only available if the firmware is build with DEVELOPMENT_TOOLS defined.
####Syntax
node.osprint(enabled)
enabled
This is eithertrue
to enable printing, orfalse
to disable it. The default isfalse
.
Nothing
node.osprint(true)
Sets the Emergency Garbage Collector mode. The EGC whitepaper provides more detailed information on the EGC.
####Syntax
node.egc.setmode(mode, [param])
mode
node.egc.NOT_ACTIVE
EGC inactive, no collection cycle will be forced in low memory situationsnode.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 upperlimit
. 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 ofnode.egc.ON_MEM_LIMIT
, this specifies the memory limit.
nil
node.egc.setmode(node.egc.ALWAYS, 4096) -- This is the default setting at startup.
node.egc.setmode(node.egc.ON_ALLOC_FAILURE) -- This is the fastest activeEGC mode.
Enable a Lua callback or task to post another task request. Note that as per the example multiple tasks can be posted in any task, but the highest priority is always delivered first.
If the task queue is full then a queue full error is raised.
####Syntax
node.task.post([task_priority], function)
task_priority
(optional)node.task.LOW_PRIORITY
= 0node.task.MEDIUM_PRIORITY
= 1node.task.HIGH_PRIORITY
= 2
function
a callback function to be executed when the task is run.
If the priority is omitted then this defaults to node.task.MEDIUM_PRIORITY
nil
for i = node.task.LOW_PRIORITY, node.task.HIGH_PRIORITY do
node.task.post(i,function(p2)
print("priority is "..p2)
end)
end
prints
priority is 2
priority is 1
priority is 0