目录
nodeMcu API 说明 ........................................................................................................................ 4
flash 错误 ....................................................................................................................... 4
概述 .................................................................................................................................................. 4
新 Gpio 索引 (20141219 及以后的版本采用) .................................................................... 4
固件烧写 .......................................................................................................................................... 5
地址................................................................................................................................... 5
node 模块 ........................................................................................................................................ 5
node.restart() ......................................................................................................................... 5
node.dsleep() ......................................................................................................................... 6
node.chipid().......................................................................................................................... 7
node.heap() ............................................................................................................................ 7
node.key() ............................................................................................................................... 8
node.led() ............................................................................................................................... 9
node.input().......................................................................................................................... 10
node.output() ....................................................................................................................... 11
file 模块 ......................................................................................................................................... 12
file.remove() ......................................................................................................................... 12
file.open() ............................................................................................................................. 12
file.close() ............................................................................................................................. 14
file.readline() ........................................................................................................................ 15
file.writeline() ....................................................................................................................... 15
file.write() .............................................................................................................................. 16
file.flush() ............................................................................................................................. 17
file.seek() .............................................................................................................................. 18
file.list() ................................................................................................................................. 20
wifi 模块 ......................................................................................................................................... 20
常量 ........................................................................................................................................ 21
wifi.setmode(mode) ........................................................................................................... 21
wifi.getmode(mode) ........................................................................................................... 21
wifi.startsmart() ................................................................................................................... 22
wifi.stopsmart() ................................................................................................................... 23
wifi.sta 子模块 ............................................................................................................................. 24
wifi.sta.config() ................................................................................................................... 24
wifi.sta.connect() ................................................................................................................ 24
wifi.sta.disconnect() .......................................................................................................... 25
wifi.sta.autoconnect() ........................................................................................................ 26
wifi.sta.getip() ...................................................................................................................... 27
wifi.sta.getmac() ................................................................................................................. 28
wifi.sta.getap() ..................................................................................................................... 28
wifi.sta.status().................................................................................................................... 29
wifi.ap 子模块 .............................................................................................................................. 30
wifi.ap.config() .................................................................................................................... 30
wifi.ap.getip() ....................................................................................................................... 31
wifi.ap.getmac() .................................................................................................................. 32
timer 模块 ..................................................................................................................................... 32
tmr.delay() ............................................................................................................................ 33
tmr.now() .............................................................................................................................. 33
tmr.alarm() ............................................................................................................................ 34
tmr.stop() .............................................................................................................................. 35
tmr.wdclr() ............................................................................................................................ 36
GPIO 模块..................................................................................................................................... 37
常量 ........................................................................................................................................ 37
gpio.mode() .......................................................................................................................... 37
gpio.read() ............................................................................................................................ 38
gpio.write() ........................................................................................................................... 38
gpio.trig() .............................................................................................................................. 39
PWM 模块 ...................................................................................................................................... 41
pwm.setup() ......................................................................................................................... 41
pwm.close() ......................................................................................................................... 41
pwm.start() ........................................................................................................................... 42
pwm.stop() ........................................................................................................................... 43
pwm.setclock() .................................................................................................................... 44
pwm.getclock().................................................................................................................... 44
pwm.setduty()...................................................................................................................... 45
pwm.getduty() ..................................................................................................................... 46
net 模块 ........................................................................................................................................ 47
常量 ........................................................................................................................................ 47
net.createServer() ............................................................................................................... 47
net.createConnection() ..................................................................................................... 48
net.server 子模块 ....................................................................................................................... 49
listen() ................................................................................................................................... 49
close() .................................................................................................................................... 50
net.socket 子模块 ....................................................................................................................... 51
connect() ............................................................................................................................... 51
send()..................................................................................................................................... 51
on() ......................................................................................................................................... 52
close() .................................................................................................................................... 53
dns() ....................................................................................................................................... 54
i2c 模块 .......................................................................................................................................... 54
常量 ........................................................................................................................................ 54
i2c.setup() ............................................................................................................................. 55
i2c.start() ............................................................................................................................... 55
i2c.stop() ............................................................................................................................... 56
i2c.address() ........................................................................................................................ 57
i2c.write() .............................................................................................................................. 57
i2c.read() ............................................................................................................................... 58
adc 模块 ........................................................................................................................................ 59
常量 ........................................................................................................................................ 60
adc.read() ............................................................................................................................. 60
uart 模块 ....................................................................................................................................... 60
常量 ........................................................................................................................................ 60
uart.setup() ........................................................................................................................... 60
uart.on() ................................................................................................................................ 61
uart.write() ............................................................................................................................ 62
onewire 模块 ............................................................................................................................... 63
常量 ........................................................................................................................................ 63
ow.setup() ............................................................................................................................. 63
ow.reset() .............................................................................................................................. 64
ow.skip() ............................................................................................................................... 64
ow.select() ............................................................................................................................ 65
ow.write() .............................................................................................................................. 67
ow.write_bytes() ................................................................................................................. 68
ow.read() ............................................................................................................................... 69
ow.read_bytes() .................................................................................................................. 69
ow.depower() ....................................................................................................................... 70
ow.reset_search() ............................................................................................................... 71
ow.target_search() ............................................................................................................. 71
ow.search() .......................................................................................................................... 72
ow.crc8() ............................................................................................................................... 73
ow.check_crc16() ............................................................................................................... 73
ow.crc16() ............................................................................................................................. 74
bit 模块 .......................................................................................................................................... 75
CONSTANT .......................................................................................................................... 75
bit.bnot() ............................................................................................................................... 75
bit.band() .............................................................................................................................. 76
bit.bor() ................................................................................................................................. 76
bit.bxor() ............................................................................................................................... 77
bit.lshift() .............................................................................................................................. 78
bit.rshift() .............................................................................................................................. 78
bit.arshift() ............................................................................................................................ 79
bit.bit() ................................................................................................................................... 80
bit.set() .................................................................................................................................. 80
bit.clear() ............................................................................................................................... 81
bit.isset() ............................................................................................................................... 82
bit.isclear() ........................................................................................................................... 82
nodeMcu API 说明
English Version148
flash 错误
注意:有些模块在烧写之后启动,串口输出 ERROR in flash_read: r=。。。
这是因为模块原来的 flash 内部没有擦除。
可使用 blank512k.bin,
内容为全 0xFF,从 0x00000 开始烧入。
烧入之后可以正常运行。
概述
• 快速、自动连接无线路由器
• 基于 Lua 5.1.4,使用者需了解最简单的 Lua 语法
• 采用事件驱动的编程模型
• 内置 file, timer, pwm, i2c, net, gpio, wifi, uart, adc 模块
• 串口波特率:9600-8N1
• 对模块的引脚进行编号;gpio,i2c,pwm 等模块需要使用引脚编号进行索引
• 目前的编号对应表格:
新 Gpio 索引 (20141219 及以后的版本采用)
index pin
0 [*] GPIO16
1 GPIO4
2 GPIO5
3 GPIO0
4 GPIO2
5 GPIO14
6 GPIO12
7 GPIO13
8 GPIO15
9 GPIO3
10 GPIO1
11 GPIO9
12 GPIO10
[*] D0(GPIO16) 只能用作 gpio 读写,不支持中断,i2c/pwm/ow
固件烧写
地址
nodemcu_512k.bin: 0x00000
参考 NodeMCU flash tool:
nodemcu-flasher59
node 模块
node.restart()
描述
重新启动
语法
node.restart()
参数
nil
返回值
nil
示例
node.restart();
参见
- []()
node.dsleep()
描述
进入睡眠模式,计时时间之后唤醒
语法
node.dsleep(us)
-注意: 如需使用此功能,需要将 esp8266 的 PIN32(RST)和 PIN8(XPD_DCDC)短接。
参数
us: 睡眠时间,单位:us
返回值
nil
示例
node.dsleep(us);
参见
- []()
node.chipid()
描述
返回芯片 ID
语法
node.chipid()
参数
nil
返回值
number:芯片 ID
示例
id = node.chipid();
参见
- []()
node.heap()
描述
返回当前系统剩余内存大小,单位:字节
语法
node.heap()
参数
nil
返回值
number: 系统剩余内存字节数
示例
heap_size = node.heap();
参见
- []()
node.key()
描述
定义按键的功能函数, 按键与 GPIO16 相连。
语法
node.key(type, function())
参数