The ADC module provides access to the in-built ADC.
The ADC module provides access to the in-built ADC.
On the ESP8266 there is only a single-channel, which is multiplexed with the
On the ESP8266 there is only a single-channel, which is multiplexed with the battery voltage. Depending on the setting in the "esp init data" (byte 107) one can either use the ADC to read an external voltage, or to read the system voltage, but not both.
battery voltage. Depending on the setting in the "esp init data" (byte 107)
one can either use the ADC to read an external voltage, or to read the
system voltage, but not both.
The default setting in the NodeMCU firmware can be controlled via user_config.h at compile time, by defining one of ESP_INIT_DATA_ENABLE_READVDD33, ESP_INIT_DATA_ENABLE_READADC or ESP_INIT_DATA_FIXED_VDD33_VALUE. To change the setting
The default setting in the NodeMCU firmware can be controlled via user_config.h at compile time, by defining one of ESP_INIT_DATA_ENABLE_READVDD33, ESP_INIT_DATA_ENABLE_READADC or ESP_INIT_DATA_FIXED_VDD33_VALUE. To change the setting at a later date, use Espressif's flash download tool to create a new init data block.
at a later date, use Espressif's flash download tool to create a new init data block.
## adc.read()
## adc.read()
Samples the ADC.
Samples the ADC.
####Syntax
`adc.read(channel)`
####Parameters
####Parameters
-`channel`: Always zero on the ESP8266
`channel` always zero on the ESP8266
####Returns
####Returns
number:the sampled value
the sampled value (number)
####Example
####Example
```lua
```lua
val=adc.read(0)
val=adc.read(0)
```
```
___
## adc.readvdd33()
## adc.readvdd33()
Reads the system voltage.
Reads the system voltage.
####Syntax
`adc.readvdd33()`
####Parameters
####Parameters
`nil`
none
####Returns
####Returns
The system voltage, in millivolts.
system voltage in millivolts (number)
If the ESP8266 has been configured to use the ADC for sampling the external pin, this function will always return 65535. This is a hardware and/or SDK limitation.
If the ESP8266 has been configured to use the ADC for sampling the external pin, this function will always return 65535. This is a hardware and/or SDK limitation.
...
@@ -46,4 +36,3 @@ If the ESP8266 has been configured to use the ADC for sampling the external pin,
...
@@ -46,4 +36,3 @@ If the ESP8266 has been configured to use the ADC for sampling the external pin,
@@ -6,73 +6,86 @@ The node module provides access to system-level features such as sleep, restart
...
@@ -6,73 +6,86 @@ The node module provides access to system-level features such as sleep, restart
Returns the boot reason code.
Returns the boot reason code.
This is the raw code, not the new "reset info" code which was introduced in recent SDKs. Values are:
This is the raw code, not the new "reset info" code which was introduced in recent SDKs. Values are:
- 1: power-on
- 1: power-on
- 2: reset (software?)
- 2: reset (software?)
- 3: hardware reset via reset pin
- 3: hardware reset via reset pin
- 4: WDT reset (watchdog timeout)
- 4: WDT reset (watchdog timeout)
####Syntax
`node.bootreason()`
####Parameters
####Parameters
`nil`
none
####Returns
####Returns
number:the boot reason code
the boot reason code (number)
####Example
####Example
```lua
```lua
rsn=node.bootreason()
rsn=node.bootreason()
```
```
___
## node.restart()
Restarts the chip.
## node.chipid()
####Syntax
Returns the ESP chip ID.
`node.restart()`
####Parameters
####Parameters
`nil`
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
####Returns
####Returns
`nil`
`nil`
####Example
####Example
```lua
```lua
node.restart();
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")
```
```
___
## node.dsleep()
## node.dsleep()
Enter deep sleep mode, wake up when timed out.
Enters deep sleep mode, wakes up when timed out.
The maximum sleep time is 4294967295us, ~71 minutes. This is an SDK limitation.
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.
Firmware from before 05 Jan 2016 have a maximum sleeptime of ~35 minutes.
####Syntax
!!! note "Note:"
`node.dsleep(us, option)`
**Note:** 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.<br/>
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.
option=0, init data byte 108 is valuable;<br/>
option>0, init data byte 108 is valueless.<br/>
More details as follows:<br/>
0, RF_CAL or not after deep-sleep wake up, depends on init data byte 108.<br/>
1, RF_CAL after deep-sleep wake up, there will belarge current.<br/>
2, no RF_CAL after deep-sleep wake up, there will only be small current.<br/>
4, disable RF after deep-sleep wake up, just like modem sleep, there will be the smallest current.
####Parameters
####Parameters
-`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.
-`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.
-`option`: number(Integer) or nil. If option = nil, it will use last alive setting as default option.
-`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
Define action to take on button press (on the old devkit 0.9), button connected to GPIO 16.
## node.input()
This function is only available if the firmware was compiled with DEVKIT_VERSION_0_9 defined.
Submits a string to the Lua interpreter. Similar to `pcall(loadstring(str))`, but without the single-line limitation.
!!! note "Note:"
This function only has an effect when invoked from a callback. Using it directly on the console **does not work**.
####Syntax
`node.key(type, function)`
####Parameters
####Parameters
-`type`: type is either string "long" or "short". long: press the key for 3 seconds, short: press shortly(less than 3 seconds)
`str` Lua chunk
-`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
Set the on/off time for the LED (on the old devkit 0.9), with the LED connected to GPIO16, multiplexed with `node.key()`.
Defines action to take on button press (on the old devkit 0.9), button connected to GPIO16.
This function is only available if the firmware was compiled with DEVKIT_VERSION_0_9 defined.
This function is only available if the firmware was compiled with DEVKIT_VERSION_0_9 defined.
####Syntax
`node.led(low, high)`
####Parameters
####Parameters
-`low`: LED off time, LED keeps on when low=0. Unit: milliseconds, time resolution: 80~100ms<br/>
-`type`: type is either string "long" or "short". long: press the key for 3 seconds, short: press shortly(less than 3 seconds)
-`high`: LED on time. Unit: milliseconds, time resolution: 80~100ms
-`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
Redirects the Lua interpreter output to a callback function. Optionally also prints it to the serial console.
Redirects the Lua interpreter output to a callback function. Optionally also prints it to the serial console.
!!! note "Note:"
!!! note "Note:"
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.
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.
####Syntax
`node.output(output_fn, serial_debug)`
####Parameters
####Parameters
-`output_fn(str)`: a function accept every output as str, and can send the output to a socket (or maybe a file).
-`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.
-`serial_debug` 1 output also show in serial. 0: no serial output.
####Returns
####Returns
`nil`
`nil`
####Example
####Example
```lua
```lua
functiontonet(str)
functiontonet(str)
sk:send(str)
sk:send(str)
end
end
node.output(tonet,1)-- serial also get the lua output.
node.output(tonet,1)-- serial also get the lua output.
```
```
```lua
```lua
-- a simple telnet server
-- a simple telnet server
s=net.createServer(net.TCP)
s=net.createServer(net.TCP)
s:listen(2323,function(c)
s:listen(2323,function(c)
con_std=c
con_std=c
functions_output(str)
functions_output(str)
if(con_std~=nil)
if(con_std~=nil)
thencon_std:send(str)
thencon_std:send(str)
end
end
end
end
node.output(s_output,0)-- re-direct output to function s_ouput.
node.output(s_output,0)-- re-direct output to function s_ouput.
c:on("receive",function(c,l)
c:on("receive",function(c,l)
node.input(l)-- works like pcall(loadstring(l)) but support multiple separate line
node.input(l)-- works like pcall(loadstring(l)) but support multiple separate line
end)
end)
c:on("disconnection",function(c)
c:on("disconnection",function(c)
con_std=nil
con_std=nil
node.output(nil)-- un-regist the redirect output function, output goes to serial
node.output(nil)-- un-regist the redirect output function, output goes to serial
end)
end)
end)
end)
```
```
####See also
####See also
-`node.input()`
[`node.input()`](#nodeinput)
___
## node.readvdd33() --deprecated, moved to adc.readvdd33()
####See also
-`adc.readvdd33()`
___
## node.compile()
Compile a Lua text file into Lua bytecode, and save it as .lc file.
## node.readvdd33() --deprecated
Moved to [`adc.readvdd33()`](adc/#adcreadvdd33).
####Syntax
## node.restart()
`node.compile(filename)`
Restarts the chip.
####Parameters
####Parameters
-`filename`: name of Lua text file
none
####Returns
####Returns
`nil`
`nil`
####Example
####Example
```lua
```lua
file.open("hello.lua","w+")
node.restart();
file.writeline([[print("hello nodemcu")]])
file.writeline([[print(node.heap())]])
file.close()
node.compile("hello.lua")
dofile("hello.lua")
dofile("hello.lc")
```
```
___
## node.setcpufreq()
Change the working CPU Frequency.
## 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.
####Syntax
This also uses the SDK function `system_restore()`, which doesn't document precisely what it erases/restores.
`node.setcpufreq(speed)`
####Parameters
####Parameters
-`speed`: `node.CPU80MHZ` or `node.CPU160MHZ`
none
####Returns
####Returns
number:target CPU Frequency
`nil`
####Example
####Example
```lua
```lua
node.setcpufreq(node.CPU80MHZ)
node.restore()
node.restart()-- ensure the restored settings take effect
```
```
___
## node.restore()
Restore 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.
## node.setcpufreq()
This also uses the SDK function `system_restore()`, which doesn't document precisely what it erases/restores.
####Syntax
Change the working CPU Frequency.
`node.restore()`
####Parameters
####Parameters
`nil`
`speed` constant 'node.CPU80MHZ' or 'node.CPU160MHZ'
####Returns
####Returns
`nil`
target CPU frequency (number)
####Example
####Example
```lua
```lua
node.restore()
node.setcpufreq(node.CPU80MHZ)
node.restart()-- ensure the restored settings take effect
```
```
___
## node.stripdebug()
## node.stripdebug()
Controls the amount of debug information kept during `node.compile()`, and
Controls the amount of debug information kept during `node.compile()`, and allows removal of debug information from already compiled Lua code.
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.
Only recommended for advanced users, the NodeMCU defaults are fine for almost all use cases.
####Syntax
`node.stripdebug([level[, function]])``
####Parameters
####Parameters
- `level`:
-`level`
- 1: don't discard debug info
- 1: don't discard debug info
- 2: discard Local and Upvalue debug info
- 2: discard Local and Upvalue debug info
- 3: discard Local, Upvalue and line-number 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.
-`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 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
#### Returns
If invoked without arguments, returns the current level settings. Otherwise, `nil` is returned.
If invoked without arguments, returns the current level settings. Otherwise, `nil` is returned.