127
Change Log New GPIO Map Old GPIO Map node.restart() node.dsleep() node.info() node.chipid() node.flashid() node.heap() node.key() deprecated node.led() deprecated node.input() node.output() node.readvdd33() deprecated, moved to adc.readvdd33() node.compile() node.setcpufreq() file.remove() file.open() file.close() file.readline() file.writeline() NodeMCU API Instruction version 0.9.5 build 20150213 INDEX node module file module

NodeMCU API Instruction - Michael · PDF fileNodeMCU API Instruction version 0.9.5 build 2015­02­13 INDEX node module ... wifi.ap.config() wifi.ap.getip() wifi module wifi.sta sub­module

  • Upload
    lamcong

  • View
    285

  • Download
    5

Embed Size (px)

Citation preview

Page 1: NodeMCU API Instruction - Michael · PDF fileNodeMCU API Instruction version 0.9.5 build 2015­02­13 INDEX node module ... wifi.ap.config() wifi.ap.getip() wifi module wifi.sta sub­module

29.6.2015 data:text/html, <html contenteditable>

data:text/html, <html contenteditable> 1/127

Change LogNew GPIO MapOld GPIO Map

node.restart()node.dsleep()node.info()node.chipid()node.flashid()node.heap()node.key() ­­deprecatednode.led() ­­deprecatednode.input()node.output()node.readvdd33() ­­deprecated, moved to adc.readvdd33()node.compile()node.setcpufreq()

file.remove()file.open()file.close()file.readline()file.writeline()

NodeMCU API Instruction

version 0.9.5 build 2015­02­13

INDEX

node module

file module

Page 2: NodeMCU API Instruction - Michael · PDF fileNodeMCU API Instruction version 0.9.5 build 2015­02­13 INDEX node module ... wifi.ap.config() wifi.ap.getip() wifi module wifi.sta sub­module

29.6.2015 data:text/html, <html contenteditable>

data:text/html, <html contenteditable> 2/127

file.read()file.write()file.flush()file.seek()file.list()file.format()file.rename()file.fsinfo()

wifi.setmode()wifi.getmode()wifi.getchannel() ­­Added on 07/16/2015wifi.setphymode() ­­Added on 07/16/2015wifi.getphymode() ­­Added on 07/16/2015wifi.startsmart()wifi.stopsmart()wifi.sleeptype()

wifi.sta.getconfig() ­­Added on 07/16/2015wifi.sta.config() ­­Updated on 07/16/2015wifi.sta.connect()wifi.sta.disconnect()wifi.sta.autoconnect()wifi.sta.getip()wifi.sta.setip()wifi.sta.getmac() ­­Updated on 07/16/2015wifi.sta.setmac() ­­Updated on 07/16/2015wifi.sta.getap() ­­Updated on 07/16/2015wifi.sta.status()wifi.sta.getbroadcast()

wifi.ap.config()wifi.ap.getip()

wifi module

wifi.sta sub­module

wifi.ap sub­module

Page 3: NodeMCU API Instruction - Michael · PDF fileNodeMCU API Instruction version 0.9.5 build 2015­02­13 INDEX node module ... wifi.ap.config() wifi.ap.getip() wifi module wifi.sta sub­module

29.6.2015 data:text/html, <html contenteditable>

data:text/html, <html contenteditable> 3/127

wifi.ap.setip()wifi.ap.getmac()wifi.ap.setmac() ­­Updated on 07/16/2015wifi.ap.getbroadcast()

tmr.delay()tmr.now()tmr.alarm()tmr.stop()tmr.wdclr()tmr.time()

gpio.mode()gpio.read()gpio.write()gpio.trig()

pwm.setup()pwm.close()pwm.start()pwm.stop()pwm.setclock()pwm.getclock()pwm.setduty()pwm.getduty()

net.createServer()net.createConnection()

timer module

gpio module

pwm module

net module

net.server sub­module

Page 4: NodeMCU API Instruction - Michael · PDF fileNodeMCU API Instruction version 0.9.5 build 2015­02­13 INDEX node module ... wifi.ap.config() wifi.ap.getip() wifi module wifi.sta sub­module

29.6.2015 data:text/html, <html contenteditable>

data:text/html, <html contenteditable> 4/127

net.server:listen()net.server:close()

net.socket:connect()net.socket:send()net.socket:on()net.socket:close()net.socket:dns()

i2c.setup()i2c.start()i2c.stop()i2c.address()i2c.write()i2c.read()

adc.read()adc.readvdd33()

uart.setup()uart.on()uart.write()

ow.setup()ow.reset()ow.skip()ow.select()ow.write()ow.write_bytes()

net.socket sub­module

i2c module

adc module

uart module

1­wire module

Page 5: NodeMCU API Instruction - Michael · PDF fileNodeMCU API Instruction version 0.9.5 build 2015­02­13 INDEX node module ... wifi.ap.config() wifi.ap.getip() wifi module wifi.sta sub­module

29.6.2015 data:text/html, <html contenteditable>

data:text/html, <html contenteditable> 5/127

ow.read()ow.read_bytes()ow.depower()ow.reset_search()ow.target_search()ow.search()ow.crc8()ow.check_crc16()ow.crc16()

bit.bnot()bit.band()bit.bor()bit.bxor()bit.lshift()bit.rshift()bit.arshift()bit.bit()bit.set()bit.clear()bit.isset()bit.isclear()

spi.setup()spi.send()spi.recv()

mqtt.Client()

mqtt.client:lwt()mqtt.client:connect()

bit module

spi module

mqtt module

mqtt.client sub­module

Page 6: NodeMCU API Instruction - Michael · PDF fileNodeMCU API Instruction version 0.9.5 build 2015­02­13 INDEX node module ... wifi.ap.config() wifi.ap.getip() wifi module wifi.sta sub­module

29.6.2015 data:text/html, <html contenteditable>

data:text/html, <html contenteditable> 6/127

mqtt.client:close()mqtt.client:publish()mqtt.client:subscribe()mqtt.client:on()

ws2812.writergb()

cjson.encode()cjson.decode()

u8g.ssd1306_128x64_i2c()u8g.ssd1306_128x64_spi()u8g.pcd8544_84x48()

u8g.disp:begin()u8g.disp:drawBitmap()u8g.disp:drawBox()u8g.disp:drawCircle()u8g.disp:drawDisc()u8g.disp:drawEllipse()u8g.disp:drawFilledEllipse()u8g.disp:drawFrame()u8g.disp:drawHLine()u8g.disp:drawLine()u8g.disp:drawPixel()u8g.disp:drawRBox()u8g.disp:drawRFrame()u8g.disp:drawStr()u8g.disp:drawStr90()u8g.disp:drawStr180()u8g.disp:drawStr270()

WS2812 module

cjson module

u8g module

u8g.disp sub­module

Page 7: NodeMCU API Instruction - Michael · PDF fileNodeMCU API Instruction version 0.9.5 build 2015­02­13 INDEX node module ... wifi.ap.config() wifi.ap.getip() wifi module wifi.sta sub­module

29.6.2015 data:text/html, <html contenteditable>

data:text/html, <html contenteditable> 7/127

u8g.disp:drawTriangle()u8g.disp:drawVLine()u8g.disp:drawXBM()u8g.disp:firstPage()u8g.disp:getColorIndex()u8g.disp:getFontAscent()u8g.disp:getFontDescent()u8g.disp:getFontLineSpacing()u8g.disp:getHeight()u8g.disp:getMode()u8g.disp:getWidth()u8g.disp:getStrWidth()u8g.disp:nextPage()u8g.disp:setColorIndex()u8g.disp:setDefaultBackgroundColor()u8g.disp:setDefaultForegroundColor()u8g.disp:setFont()u8g.disp:setFontLineSpacingFactor()u8g.disp:setFontPosBaseline()u8g.disp:setFontPosBottom()u8g.disp:setFontPosCenter()u8g.disp:setFontPosTop()u8g.disp:setFontRefHeightAll()u8g.disp:setFontRefHeightExtendedText()u8g.disp:setFontRefHeightText()u8g.disp:setRot90()u8g.disp:setRot180()u8g.disp:setRot270()u8g.disp:setScale2x2()u8g.disp:sleepOn()u8g.disp:sleepOff()u8g.disp:undoRotation()u8g.disp:undoScale()

dht.read()dht.read11()dht.readxx()

dht module

Page 8: NodeMCU API Instruction - Michael · PDF fileNodeMCU API Instruction version 0.9.5 build 2015­02­13 INDEX node module ... wifi.ap.config() wifi.ap.getip() wifi module wifi.sta sub­module

29.6.2015 data:text/html, <html contenteditable>

data:text/html, <html contenteditable> 8/127

­Back to Index

Easy to access wireless routerBased on Lua 5.1.4, Developers are supposed to have experience with Lua Program language.Event­Drive programming modal.Build­in file, timer, pwm, i2c, net, gpio, wifi, uart, adc module.Serial Port BaudRate:9600Re­mapped GPIO pin, use the index to program gpio, i2c, pwm.GPIO Map Table:

IO index ESP8266 pin IO index

0 [*] GPIO16

1 GPIO5

2 GPIO4

3 GPIO0

4 GPIO2

5 GPIO14

6 GPIO12

** [] D0(GPIO16) can only be used as gpio read/write. no interrupt supported. no pwm/i2c/ow supported. *

Summary

GPIO NEW TABLE ( Build 20141219 and later)

new_gpio_map

Example

Page 9: NodeMCU API Instruction - Michael · PDF fileNodeMCU API Instruction version 0.9.5 build 2015­02­13 INDEX node module ... wifi.ap.config() wifi.ap.getip() wifi module wifi.sta sub­module

29.6.2015 data:text/html, <html contenteditable>

data:text/html, <html contenteditable> 9/127

gpio = [0]=3,[1]=10,[2]=4,[3]=9,[4]=1,[5]=2

pin = gpio[2] ‐‐ connect the signal wire to pin 4

­Back to Index

IO index ESP8266 pin IO index

0 GPIO12

1 GPIO13

2 GPIO14

3 GPIO15

4 GPIO3

5 GPIO1

­Back to Index

nodemcu_512k.bin: 0x00000See NodeMCU flash tool:nodemcu­flasher

GPIO OLD TABLE (Before build 20141212)

old_gpio_map

Burn/Flash Firmware

Address

node module

Page 10: NodeMCU API Instruction - Michael · PDF fileNodeMCU API Instruction version 0.9.5 build 2015­02­13 INDEX node module ... wifi.ap.config() wifi.ap.getip() wifi module wifi.sta sub­module

29.6.2015 data:text/html, <html contenteditable>

data:text/html, <html contenteditable> 10/127

restart the chip.

node.restart()

nil

nil

node.restart();

Back to Index

Enter deep sleep mode, wake up when timed out.

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

node.restart()

Description

Syntax

Parameters

Returns

Example

See also

node.dsleep()

Description

Syntax

Page 11: NodeMCU API Instruction - Michael · PDF fileNodeMCU API Instruction version 0.9.5 build 2015­02­13 INDEX node module ... wifi.ap.config() wifi.ap.getip() wifi module wifi.sta sub­module

29.6.2015 data:text/html, <html contenteditable>

data:text/html, <html contenteditable> 11/127

RST, the chip will wake up by a falling­edge on pin RST.option=0, init data byte 108 is valuable;option>0, init data byte 108 is valueless.More details as follows: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.

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.

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)

­ Back to Index

Parameters

Returns

Example

See also

Page 12: NodeMCU API Instruction - Michael · PDF fileNodeMCU API Instruction version 0.9.5 build 2015­02­13 INDEX node module ... wifi.ap.config() wifi.ap.getip() wifi module wifi.sta sub­module

29.6.2015 data:text/html, <html contenteditable>

data:text/html, <html contenteditable> 12/127

return NodeMCU version, chipid, flashid, flash size, flash mode, flash speed.

node.info()

nil

majorVer (number)minorVer (number)devVer (number)chipid (number)flashid (number)flashsize (number)flashmode (number)flashspeed (number)

majorVer, minorVer, devVer, chipid, flashid, flashsize, flashmode, flashspeed print("NodeMCU "..majorVer.."."..minorVer.."."

­ Back to Index

node.info()

Description

Syntax

Parameters

Returns

Example

See also

node.chipid()

Description

Page 13: NodeMCU API Instruction - Michael · PDF fileNodeMCU API Instruction version 0.9.5 build 2015­02­13 INDEX node module ... wifi.ap.config() wifi.ap.getip() wifi module wifi.sta sub­module

29.6.2015 data:text/html, <html contenteditable>

data:text/html, <html contenteditable> 13/127

return chip ID

node.chipid()

nil

number:chip ID

id = node.chipid();

­ Back to Index

return flashid ID

node.flashid()

nil

number:flash ID

Syntax

Parameters

Returns

Example

See also

node.flashid()

Description

Syntax

Parameters

Returns

Example

Page 14: NodeMCU API Instruction - Michael · PDF fileNodeMCU API Instruction version 0.9.5 build 2015­02­13 INDEX node module ... wifi.ap.config() wifi.ap.getip() wifi module wifi.sta sub­module

29.6.2015 data:text/html, <html contenteditable>

data:text/html, <html contenteditable> 14/127

flashid = node.flashid();

­ Back to Index

return the remain HEAP size in bytes

node.heap()

nil

number: system heap size left in bytes

heap_size = node.heap();

­ Back to Index

define button function, button is connected to GPIO16.

See also

node.heap()

Description

Syntax

Parameters

Returns

Example

See also

node.key()

Description

Page 15: NodeMCU API Instruction - Michael · PDF fileNodeMCU API Instruction version 0.9.5 build 2015­02­13 INDEX node module ... wifi.ap.config() wifi.ap.getip() wifi module wifi.sta sub­module

29.6.2015 data:text/html, <html contenteditable>

data:text/html, <html contenteditable> 15/127

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, cancling the user defined function.Default function: long: change LED blinking rate, short: reset chip

nil

node.key("long", function() print('hello world

­ node.led

­ Back to Index

setup the on/off time for led, which connected to GPIO16, multiplexing with node.key()

node.led(low, high)

Syntax

Parameters

Returns

Example

See also

node.led()

Description

Syntax

Page 16: NodeMCU API Instruction - Michael · PDF fileNodeMCU API Instruction version 0.9.5 build 2015­02­13 INDEX node module ... wifi.ap.config() wifi.ap.getip() wifi module wifi.sta sub­module

29.6.2015 data:text/html, <html contenteditable>

data:text/html, <html contenteditable> 16/127

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);

­ node.key

­ Back to Index

accept a string and put the string into Lua interpretor.same as pcall(loadstring(str)) but support multi seperated line.

node.input(str)

str: Lua chunk

nil

Parameters

Returns

Example

See also

node.input()

Description

Syntax

Parameters

Returns

Page 17: NodeMCU API Instruction - Michael · PDF fileNodeMCU API Instruction version 0.9.5 build 2015­02­13 INDEX node module ... wifi.ap.config() wifi.ap.getip() wifi module wifi.sta sub­module

29.6.2015 data:text/html, <html contenteditable>

data:text/html, <html contenteditable> 17/127

‐‐ never use node.input() in console. no effect. sk:on("receive", function(conn, payload) node.

­

­ Back to Index

direct output from lua interpretor to a call back function.

node.output(function(str), serial_debug)

function(str): a function accept every output as str, and can send the output to a socket.serial_debug: 1 output also show in serial. 0: no serial output.

nil

function tonet(str) sk:send(str) ‐‐ print(str) WRONG!!! never ever print something in this function ‐‐ because this will cause a recursive function call!!! end

Example

See also

node.output()

Description

Syntax

Parameters

Returns

Example

Page 18: NodeMCU API Instruction - Michael · PDF fileNodeMCU API Instruction version 0.9.5 build 2015­02­13 INDEX node module ... wifi.ap.config() wifi.ap.getip() wifi module wifi.sta sub­module

29.6.2015 data:text/html, <html contenteditable>

data:text/html, <html contenteditable> 18/127

node.ouput(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)

­

­ Back to Index

Reading vdd33 pin voltage

node.readvdd33()

no parameters

See also

node.readvdd33()

Description

Syntax

Parameters

Page 19: NodeMCU API Instruction - Michael · PDF fileNodeMCU API Instruction version 0.9.5 build 2015­02­13 INDEX node module ... wifi.ap.config() wifi.ap.getip() wifi module wifi.sta sub­module

29.6.2015 data:text/html, <html contenteditable>

data:text/html, <html contenteditable> 19/127

mV

print(node.readvdd33())

output

3345

v = node.readvdd33() / 1000 print(v) v=nil

output

3.315

­

­ Back to Index

compile lua text file into lua bytecode file, and save it as .lc file.

node.compile("file.lua")

lua text file end with ".lua"

Returns

Example

See also

node.compile()

Description

Syntax

Parameters

Page 20: NodeMCU API Instruction - Michael · PDF fileNodeMCU API Instruction version 0.9.5 build 2015­02­13 INDEX node module ... wifi.ap.config() wifi.ap.getip() wifi module wifi.sta sub­module

29.6.2015 data:text/html, <html contenteditable>

data:text/html, <html contenteditable> 20/127

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")

­

­ Back to Index

Change the working CPU Frequency

node.setcpufreq(speed)

speed: node.CPU80MHZ or node.CPU160MHZ

return targe CPU Frequency

Returns

Example

See also

node.setcpufreq()

Description

Syntax

Parameters

Returns

Example

Page 21: NodeMCU API Instruction - Michael · PDF fileNodeMCU API Instruction version 0.9.5 build 2015­02­13 INDEX node module ... wifi.ap.config() wifi.ap.getip() wifi module wifi.sta sub­module

29.6.2015 data:text/html, <html contenteditable>

data:text/html, <html contenteditable> 21/127

node.setcpufreq(node.CPU80MHZ)

­ Back to Index

remove file from file system.

file.remove(filename)

filename: file to remove

nil

‐‐ remove "foo.lua" from file system. file.remove("foo.lua")

­ file.open()­ file.close()

­ Back to Index

See also

file module

file.remove()

Description

Syntax

Parameters

Returns

Example

See also

Page 22: NodeMCU API Instruction - Michael · PDF fileNodeMCU API Instruction version 0.9.5 build 2015­02­13 INDEX node module ... wifi.ap.config() wifi.ap.getip() wifi module wifi.sta sub­module

29.6.2015 data:text/html, <html contenteditable>

data:text/html, <html contenteditable> 22/127

open file.

file.open(filename, mode)

filename: file to be opened, directories are not supportedmode:

"r": read mode (the default)"w": write mode"a": append mode"r+": update mode, all previous data is preserved"w+": update mode, all previous data is erased"a+": append update mode, previous data is preserved, writing is only allowed at the end of file

nil: file not opened, or not exists. true: file opened ok.

‐‐ open 'init.lua', print the first line. file.open("init.lua", "r") print(file.readline()) file.close()

­ file.close()

file.open()

Description

Syntax

Parameters

Returns

Example

See also

Page 23: NodeMCU API Instruction - Michael · PDF fileNodeMCU API Instruction version 0.9.5 build 2015­02­13 INDEX node module ... wifi.ap.config() wifi.ap.getip() wifi module wifi.sta sub­module

29.6.2015 data:text/html, <html contenteditable>

data:text/html, <html contenteditable> 23/127

­ file.readline()

­ Back to Index

close the file.

file.close()

nil

nil

‐‐ open 'init.lua', print the first line. file.open("init.lua", "r") print(file.readline()) file.close()

­ file.open()­ file.readline()

­ Back to Index

file.close()

Description

Syntax

Parameters

Returns

Example

See also

file.readline()

Description

Page 24: NodeMCU API Instruction - Michael · PDF fileNodeMCU API Instruction version 0.9.5 build 2015­02­13 INDEX node module ... wifi.ap.config() wifi.ap.getip() wifi module wifi.sta sub­module

29.6.2015 data:text/html, <html contenteditable>

data:text/html, <html contenteditable> 24/127

read one line of file which is opened before.

file.readline()

nil

file content in string, line by line, include EOL('\n')return nil when EOF.

‐‐ print the first line of 'init.lua' file.open("init.lua", "r") print(file.readline()) file.close()

­ file.open()­ file.close()

­ Back to Index

write string to file and add a '\n' at the end.

file.writeline(string)

Syntax

Parameters

Returns

Example

See also

file.writeline()

Description

Syntax

Parameters

Page 25: NodeMCU API Instruction - Michael · PDF fileNodeMCU API Instruction version 0.9.5 build 2015­02­13 INDEX node module ... wifi.ap.config() wifi.ap.getip() wifi module wifi.sta sub­module

29.6.2015 data:text/html, <html contenteditable>

data:text/html, <html contenteditable> 25/127

string: content to be write to file

true: write ok. nil: there is error

‐‐ open 'init.lua' in 'a+' mode file.open("init.lua", "a+") ‐‐ write 'foo bar' to the end of the file file.writeline('foo bar') file.close()

­ file.open()­ file.write()

­ Back to Index

read content of file which is opened before.

file.read()

if nothing passed in, read all byte in file. if pass a number n, then read n byte from file, or EOF is reached. if pass a string "q", then read until 'q' or EOF is reached.

file content in string

Returns

Example

See also

file.read()

Description

Syntax

Parameters

Returns

Page 26: NodeMCU API Instruction - Michael · PDF fileNodeMCU API Instruction version 0.9.5 build 2015­02­13 INDEX node module ... wifi.ap.config() wifi.ap.getip() wifi module wifi.sta sub­module

29.6.2015 data:text/html, <html contenteditable>

data:text/html, <html contenteditable> 26/127

return nil when EOF.

‐‐ print the first line of 'init.lua' file.open("init.lua", "r") print(file.read('\r')) file.close()

‐‐ print the first 5 byte of 'init.lua' file.open("init.lua", "r") print(file.read(5)) file.close()

­ file.open()­ file.close()

­ Back to Index

write string to file.

file.write(string)

string: content to be write to file.

true: write ok. nil: there is error

Example

See also

file.write()

Description

Syntax

Parameters

Returns

Example

Page 27: NodeMCU API Instruction - Michael · PDF fileNodeMCU API Instruction version 0.9.5 build 2015­02­13 INDEX node module ... wifi.ap.config() wifi.ap.getip() wifi module wifi.sta sub­module

29.6.2015 data:text/html, <html contenteditable>

data:text/html, <html contenteditable> 27/127

‐‐ open 'init.lua' in 'a+' mode file.open("init.lua", "a+") ‐‐ write 'foo bar' to the end of the file file.write('foo bar') file.close()

­ file.open()­ file.writeline()

­ Back to Index

flush to file.

file.flush()

nil

nil

‐‐ open 'init.lua' in 'a+' mode file.open("init.lua", "a+") ‐‐ write 'foo bar' to the end of the file file.write('foo bar') file.flush() file.close()

See also

file.flush()

Description

Syntax

Parameters

Returns

Example

See also

Page 28: NodeMCU API Instruction - Michael · PDF fileNodeMCU API Instruction version 0.9.5 build 2015­02­13 INDEX node module ... wifi.ap.config() wifi.ap.getip() wifi module wifi.sta sub­module

29.6.2015 data:text/html, <html contenteditable>

data:text/html, <html contenteditable> 28/127

­ file.open()­ file.writeline()

­ Back to Index

Sets and gets the file position, measured from the beginning of the file, to the position given by offset plus a base specified by the string whence.

file.seek(whence, offset)

whence:"set": base is position 0 (beginning of the file);"cur": base is current position;(default value)"end": base is end of file;offset: default 0

success: returns the final file positionfail: returns nil

‐‐ open 'init.lua' in 'a+' mode file.open("init.lua", "a+") ‐‐ write 'foo bar' to the end of the file file.write('foo bar') file.flush() file.seek("set") print(file.readline()) file.close()

file.seek()

Description

Syntax

Parameters

Returns

Example

Page 29: NodeMCU API Instruction - Michael · PDF fileNodeMCU API Instruction version 0.9.5 build 2015­02­13 INDEX node module ... wifi.ap.config() wifi.ap.getip() wifi module wifi.sta sub­module

29.6.2015 data:text/html, <html contenteditable>

data:text/html, <html contenteditable> 29/127

­ file.open()­ file.writeline()

­ Back to Index

list all files.

file.list()

nil

a lua table which contains the file name: file size pairs

l = file.list(); for k,v in pairs(l) do print("name:"..k..", size:"..v) end

­ file.remove()

­ Back to Index

See also

file.list()

Description

Syntax

Parameters

Returns

Example

See also

file.format()

Page 30: NodeMCU API Instruction - Michael · PDF fileNodeMCU API Instruction version 0.9.5 build 2015­02­13 INDEX node module ... wifi.ap.config() wifi.ap.getip() wifi module wifi.sta sub­module

29.6.2015 data:text/html, <html contenteditable>

data:text/html, <html contenteditable> 30/127

format file system.

file.format()

nil

nil

file.format()

­ file.remove()

­ Back to Index

rename a file. NOTE: the current opened file will be closed.

file.rename(oldname, newname)

oldname: old file name, directories are not supportednewname: new file name, directories are not

Description

Syntax

Parameters

Returns

Example

See also

file.rename()

Description

Syntax

Parameters

Page 31: NodeMCU API Instruction - Michael · PDF fileNodeMCU API Instruction version 0.9.5 build 2015­02­13 INDEX node module ... wifi.ap.config() wifi.ap.getip() wifi module wifi.sta sub­module

29.6.2015 data:text/html, <html contenteditable>

data:text/html, <html contenteditable> 31/127

supported

false: rename failed. true: rename ok.

‐‐ rename file 'temp.lua' to 'init.lua'. file.rename("temp.lua","init.lua")

­ file.close()

­ Back to Index

Get file system info

file.fsinfo()

nil

remaining (number)used (number)total (number)

‐‐ get file system info remaining, used, total=file.fsinfo()

Returns

Example

See also

file.fsinfo()

Description

Syntax

Parameters

Returns

Example

Page 32: NodeMCU API Instruction - Michael · PDF fileNodeMCU API Instruction version 0.9.5 build 2015­02­13 INDEX node module ... wifi.ap.config() wifi.ap.getip() wifi module wifi.sta sub­module

29.6.2015 data:text/html, <html contenteditable>

data:text/html, <html contenteditable> 32/127

print("\nFile system info:\nTotal : "..total..

­ Back to Index

wifi.STATION, wifi.SOFTAP, wifi.STATIONAP

setup wifi operation mode.

wifi.STATION is when the device is connected to another wifi router. This is often done to give the device access to the internet.wifi.SOFTAP is when the device is acting as ONLY an access point. This mode will allow you to see the device in the list of wifi networks. In this mode your computer can connect to the device creating a local area network. Unless you change the value, the ESP8266 device will be given a local IP address of 192.168.4.1 and assign your computer the next available IP, such as: 192.168.4.2.wifi.STATIONAP is a combination of wifi.STATION and wifi.SOFTAP. It allows you to create a local wifi connection AND connect to another wifi router.

See also

wifi module

CONSTANT

wifi.setmode()

Description

Syntax

Page 33: NodeMCU API Instruction - Michael · PDF fileNodeMCU API Instruction version 0.9.5 build 2015­02­13 INDEX node module ... wifi.ap.config() wifi.ap.getip() wifi module wifi.sta sub­module

29.6.2015 data:text/html, <html contenteditable>

data:text/html, <html contenteditable> 33/127

wifi.setmode(mode)

mode: value should be:

wifi.STATIONwifi.SOFTAPwifi.STATIONAP

current mode after setup

wifi.setmode(wifi.STATION)

­ wifi.getmode()

­ Back to Index

get wifi operation mode.

wifi.getmode()

nil

wifi operation mode

Parameters

Returns

Example

See also

wifi.getmode()

Description

Syntax

Parameters

Returns

Page 34: NodeMCU API Instruction - Michael · PDF fileNodeMCU API Instruction version 0.9.5 build 2015­02­13 INDEX node module ... wifi.ap.config() wifi.ap.getip() wifi module wifi.sta sub­module

29.6.2015 data:text/html, <html contenteditable>

data:text/html, <html contenteditable> 34/127

print(wifi.getmode())

­ wifi.setmode()

­ Back to Index

get current wifi channel.

wifi.getchannel()

nil

current wifi channel

print(wifi.getchannel())

­ Back to Index

Example

See also

wifi.getchannel()

Description

Syntax

Parameters

Returns

Example

See also

wifi.setphymode()

Description

Page 35: NodeMCU API Instruction - Michael · PDF fileNodeMCU API Instruction version 0.9.5 build 2015­02­13 INDEX node module ... wifi.ap.config() wifi.ap.getip() wifi module wifi.sta sub­module

29.6.2015 data:text/html, <html contenteditable>

data:text/html, <html contenteditable> 35/127

Setup wifi physical mode.

wifi.PHYMODE_B 802.11b, More range, Low Transfer rate, More current drawwifi.PHYMODE_G 802.11g, Medium range, Medium transfer rate, Medium current drawwifi.PHYMODE_N 802.11n, Least range, Fast transfer rate, Least current draw (STATION ONLY) Information from the Espressif datasheet v4.3

Parameters

Tx 802.11b, CCK 11Mbps, P OUT=+17dBm

Tx 802.11g, OFDM 54Mbps, P OUT =+15dBm

Tx 802.11n, MCS7 65Mbps, P OUT =+13dBm

Rx 802.11b, 1024 bytes packet length, ­80dBm

Rx 802.11g, 1024 bytes packet length, ­70dBm

Rx 802.11n, 1024 bytes packet length, ­65dBm

wifi.setphymode(mode)

mode: value should be:

wifi.PHYMODE_Bwifi.PHYMODE_Gwifi.PHYMODE_N

Current physical mode after setup

‐‐STATION wifi.setphymode()

Syntax

Parameters

Returns

Example

Page 36: NodeMCU API Instruction - Michael · PDF fileNodeMCU API Instruction version 0.9.5 build 2015­02­13 INDEX node module ... wifi.ap.config() wifi.ap.getip() wifi module wifi.sta sub­module

29.6.2015 data:text/html, <html contenteditable>

data:text/html, <html contenteditable> 36/127

­ wifi.getphymode()

­ Back to Index

get wifi physical mode.

wifi.getmode()

nil

wifi physical mode

1: wifi.PHYMODE_B2: wifi.PHYMODE_G3: wifi.PHYMODE_N

print(wifi.getphymode())

­ wifi.setphymode()

­ Back to Index

See also

wifi.getphymode()

Description

Syntax

Parameters

Returns

Example

See also

wifi.startsmart()

Page 37: NodeMCU API Instruction - Michael · PDF fileNodeMCU API Instruction version 0.9.5 build 2015­02­13 INDEX node module ... wifi.ap.config() wifi.ap.getip() wifi module wifi.sta sub­module

29.6.2015 data:text/html, <html contenteditable>

data:text/html, <html contenteditable> 37/127

starts to auto configuration, if success set up ssid and pwd automatically .

wifi.startsmart(channel, function succeed_callback())

channel: 1~13, startup channel for searching, if nil, default to 6. 20 seconds for each channel.succeed_callback: callback function called after configuration, which is called when got password and connected to AP.

nil

wifi.startsmart(6, function() end)

­ wifi.stopsmart()

­ Back to Index

stop the configuring process.

wifi.stopsmart()

Description

Syntax

Parameters

Returns

Example

See also

wifi.stopsmart()

Description

Syntax

Page 38: NodeMCU API Instruction - Michael · PDF fileNodeMCU API Instruction version 0.9.5 build 2015­02­13 INDEX node module ... wifi.ap.config() wifi.ap.getip() wifi module wifi.sta sub­module

29.6.2015 data:text/html, <html contenteditable>

data:text/html, <html contenteditable> 38/127

nil

nil

wifi.stopsmart()

­ wifi.startsmart()

­ Back to Index

config the sleep type for wifi modem.

type_actual = wifi.sleeptype(type_need)

type_need:wifi.NONE_SLEEP, wifi.LIGHT_SLEEP, wifi.MODEM_SLEEP

type_actual:wifi.NONE_SLEEP, wifi.LIGHT_SLEEP, wifi.MODEM_SLEEP

Parameters

Returns

Example

See also

wifi.sleeptype()

Description

Syntax

Parameters

Returns

Example

Page 39: NodeMCU API Instruction - Michael · PDF fileNodeMCU API Instruction version 0.9.5 build 2015­02­13 INDEX node module ... wifi.ap.config() wifi.ap.getip() wifi module wifi.sta sub­module

29.6.2015 data:text/html, <html contenteditable>

data:text/html, <html contenteditable> 39/127

realtype = wifi.sleeptype(wifi.MODEM_SLEEP)

­ node.dsleep()

­ Back to Index

Get wifi station configuration.Note: If bssid_set is equal to 0 then bssid is irrelevant

ssid, password, bssid_set, bssid=wifi.sta.getconfig()

nil

ssid, password, bssid_set, bssid

‐‐Get current Station configuration ssid, password, bssid_set, bssid=wifi.sta.getconfig print("\nCurrent Station configuration:\nSSID : .."\nPassword : "..password .."\nBSSID_set : "..bssid_set .."\nBSSID: "..bssid.."\n") ssid, password, bssid_set, bssid=nil, nil, nil,

See also

wifi.sta.getconfig()

Description

Syntax

Parameters

Returns

Example

See also

Page 40: NodeMCU API Instruction - Michael · PDF fileNodeMCU API Instruction version 0.9.5 build 2015­02­13 INDEX node module ... wifi.ap.config() wifi.ap.getip() wifi module wifi.sta sub­module

29.6.2015 data:text/html, <html contenteditable>

data:text/html, <html contenteditable> 40/127

­ wifi.sta.connect()­ wifi.sta.disconnect()

­ Back to Index

Set wifi station configuration

wifi.sta.config(ssid, password)wifi.sta.config(ssid, password, auto)wifi.sta.config(ssid, password, bssid)wifi.sta.config(ssid, password, auto, bssid)

ssid: string which is less than 32 bytes.password: string which is less than 64 bytes.auto: value of 0 or 1 (Default is 1)

0: Disable auto connect and remain disconnected from Access Point 1: Enable auto connect and connect to Access Point.

bssid: String that contains the MAC address of the Access Point, (optional).

You can set bssid if you have multiple Access Points with the same ssid.Note: if you set bssid for a specific SSID and would like to configure station to connect to the same ssid only without the bssid requirement, you MUST first configure to station to a different ssid first, then connect to the desired ssidThe following formats are valid:

"DE­C1­A5­51­F1­ED""AC­1D­1C­B1­0B­22"

wifi.sta.config()

Description

Syntax

Parameters

Page 41: NodeMCU API Instruction - Michael · PDF fileNodeMCU API Instruction version 0.9.5 build 2015­02­13 INDEX node module ... wifi.ap.config() wifi.ap.getip() wifi module wifi.sta sub­module

29.6.2015 data:text/html, <html contenteditable>

data:text/html, <html contenteditable> 41/127

"DE AD BE EF 7A C0"

nil

‐‐Connect to Access Point automatically when in range wifi.sta.config("myssid", "password")

‐‐Connect to Access Point, User decides when to connect/disconnect to/from AP wifi.sta.config("myssid", "mypassword", 0) wifi.sta.connect() ‐‐do some wifi stuff wifi.sta.disconnect()

‐‐Connect to specific Access Point automatically when in range wifi.sta.config("myssid", "mypassword", "12:34:56:78:90:12

‐‐Connect to specific Access Point, User decides when to connect/disconnect to/from AP wifi.sta.config("myssid", "mypassword", 0, "12:34:56:78:90:12 wifi.sta.connect() ‐‐do some wifi stuff wifi.sta.disconnect()

­ wifi.sta.connect()­ wifi.sta.disconnect()

­ Back to Index

connect to AP in station mode.

wifi.sta.connect()

Returns

Example

See also

wifi.sta.connect()

Description

Syntax

Page 42: NodeMCU API Instruction - Michael · PDF fileNodeMCU API Instruction version 0.9.5 build 2015­02­13 INDEX node module ... wifi.ap.config() wifi.ap.getip() wifi module wifi.sta sub­module

29.6.2015 data:text/html, <html contenteditable>

data:text/html, <html contenteditable> 42/127

nil

nil

wifi.sta.connect()

­ wifi.sta.disconnect()­ wifi.sta.config()

­ Back to Index

disconnect from AP in station mode.

wifi.sta.disconnect()

nil

nil

wifi.sta.disconnect()

Parameters

Returns

Example

See also

wifi.sta.disconnect()

Description

Syntax

Parameters

Returns

Example

Page 43: NodeMCU API Instruction - Michael · PDF fileNodeMCU API Instruction version 0.9.5 build 2015­02­13 INDEX node module ... wifi.ap.config() wifi.ap.getip() wifi module wifi.sta sub­module

29.6.2015 data:text/html, <html contenteditable>

data:text/html, <html contenteditable> 43/127

­ wifi.sta.config()­ wifi.sta.connect()

­ Back to Index

auto connect to AP in station mode.

wifi.sta.autoconnect(auto)

auto: 0 to disable auto connecting. 1 to enable auto connecting

nil

wifi.sta.autoconnect()

­ wifi.sta.config()­ wifi.sta.connect()­ wifi.sta.disconnect()

­ Back to Index

See also

wifi.sta.autoconnect()

Description

Syntax

Parameters

Returns

Example

See also

wifi.sta.getip()

Page 44: NodeMCU API Instruction - Michael · PDF fileNodeMCU API Instruction version 0.9.5 build 2015­02­13 INDEX node module ... wifi.ap.config() wifi.ap.getip() wifi module wifi.sta sub­module

29.6.2015 data:text/html, <html contenteditable>

data:text/html, <html contenteditable> 44/127

get ip, netmask, gateway address in station mode.

wifi.sta.getip()

nil

ip, netmask, gateway address in string, for example:"192.168.0.111"return nil if ip = "0.0.0.0".

‐‐ print current ip, netmask, gateway print(wifi.sta.getip()) ‐‐ 192.168.0.111 255.255.255.0 192.168.0.1 ip = wifi.sta.getip() print(ip) ‐‐ 192.168.0.111 ip, nm = wifi.sta.getip() print(nm) ‐‐ 255.255.255.0

­ wifi.sta.getmac()

­ Back to Index

set ip, netmask, gateway address in station mode.

Description

Syntax

Parameters

Returns

Example

See also

wifi.sta.setip()

Description

Page 45: NodeMCU API Instruction - Michael · PDF fileNodeMCU API Instruction version 0.9.5 build 2015­02­13 INDEX node module ... wifi.ap.config() wifi.ap.getip() wifi module wifi.sta sub­module

29.6.2015 data:text/html, <html contenteditable>

data:text/html, <html contenteditable> 45/127

wifi.sta.setip(cfg)

cfg: table contain ip, netmask, and gateway

ip="192.168.0.111", netmask="255.255.255.0", gateway="192.168.0.1"

true if success, false if fail.

cfg = ip="192.168.0.111", netmask="255.255.255.0", gateway="192.168.0.1" wifi.sta.setip(cfg)

­ wifi.sta.setmac()

­ Back to Index

get mac address in station mode.

Syntax

Parameters

Returns

Example

See also

wifi.sta.getmac()

Description

Syntax

Page 46: NodeMCU API Instruction - Michael · PDF fileNodeMCU API Instruction version 0.9.5 build 2015­02­13 INDEX node module ... wifi.ap.config() wifi.ap.getip() wifi module wifi.sta sub­module

29.6.2015 data:text/html, <html contenteditable>

data:text/html, <html contenteditable> 46/127

wifi.sta.getmac()

nil

mac address in string, for example:"18­33­44­FE­55­BB"

‐‐ print current mac address print(wifi.sta.getmac())

­ wifi.sta.getip()

­ Back to Index

set mac address in station mode.

wifi.sta.setmac(mac)

mac address in string, for example:"DE:AD:BE:EF:7A:C0"

true if success, false if fail.

Parameters

Returns

Example

See also

wifi.sta.setmac()

Description

Syntax

Parameters

Returns

Page 47: NodeMCU API Instruction - Michael · PDF fileNodeMCU API Instruction version 0.9.5 build 2015­02­13 INDEX node module ... wifi.ap.config() wifi.ap.getip() wifi module wifi.sta sub­module

29.6.2015 data:text/html, <html contenteditable>

data:text/html, <html contenteditable> 47/127

print(wifi.sta.setmac("DE:AD:BE:EF:7A:C0"))

­ wifi.sta.setip()

­ Back to Index

scan and get ap list as a lua table into callback function.

wifi.sta.getap(function(table)) wifi.sta.getap(cfg, function(table)) wifi.sta.getap(format, function(table)) wifi.sta.getap(cfg, format, function(table))

cfg: table that contains scan configurationssid: ssid == nil, don't filter ssid.bssid: bssid == nil, don't filter bssid.channel: channel == 0, scan all channels, otherwise scan set channel.(Default is 0)show_hidden: show_hidden == 1, get info for router with hidden ssid.(Default is 0)

format: Select output table format, 0 or 1 is valid.(0 is Default)

0: Old format (SSID : Authmode, RSSI, BSSID, Channel)

NOTE: When using old format for table output, any duplicate SSIDs will be

Example

See also

wifi.sta.getap()

Description

Syntax

Parameters

Page 48: NodeMCU API Instruction - Michael · PDF fileNodeMCU API Instruction version 0.9.5 build 2015­02­13 INDEX node module ... wifi.ap.config() wifi.ap.getip() wifi module wifi.sta sub­module

29.6.2015 data:text/html, <html contenteditable>

data:text/html, <html contenteditable> 48/127

discarded.1: New format (BSSID : SSID, RSSI, Authmode, Channel)

function(table): a callback function to receive ap table when scan is done

This function receives a table, the key is the ssid, value is other info in format: authmode,rssi,bssid,channelIf you are using the new output format, the key is the bssid, value is other info in format: ssid,rssi,authmode,channel

nil

‐‐ print ap list function listap(t) for k,v in pairs(t) do print(k.." : "..v) end end wifi.sta.getap(listap)

‐‐ Print AP list that is easier to read function listap(t) (SSID : Authmode, RSSI, BSSID, Channel) print("\n\t\t\tSSID\t\t\t\t\tBSSID\t\t\t RSSI for ssid,v in pairs(t) do local authmode, rssi, bssid, channel = string.match print(string.format("%32.s",ssid).."\t"..bssid end end wifi.sta.getap(listap)

‐‐NOTE: The rest of the examples use the new style output format

‐‐ print ap list function listap(t) for k,v in pairs(t) do print(k.." : "..v) end end

Returns

Example

Page 49: NodeMCU API Instruction - Michael · PDF fileNodeMCU API Instruction version 0.9.5 build 2015­02­13 INDEX node module ... wifi.ap.config() wifi.ap.getip() wifi module wifi.sta sub­module

29.6.2015 data:text/html, <html contenteditable>

data:text/html, <html contenteditable> 49/127

wifi.sta.getap(1, listap)

‐‐ Print AP list that is easier to read function listap(t) (SSID : Authmode, RSSI, BSSID, Channel) print("\n\t\t\tSSID\t\t\t\t\tBSSID\t\t\t RSSI for bssid,v in pairs(t) do local ssid, rssi, authmode, channel = string.match print(string.format("%32.s",ssid).."\t"..bssid end end wifi.sta.getap(1, listap)

‐‐check for specific AP function listap(t) print("\n\t\t\tSSID\t\t\t\t\tBSSID\t\t\t RSSI for bssid,v in pairs(t) do local ssid, rssi, authmode, channel = string.match print(string.format("%32.s",ssid).."\t"..bssid end end scan_cfg= scan_cfg.ssid="myssid" scan_cfg.bssid="AA:AA:AA:AA:AA:AA" scan_cfg.channel=0 scan_cfg.show_hidden=1 wifi.sta.getap(scan_cfg, 1, listap)

‐‐get RSSI for currently configured AP function listap(t) for bssid,v in pairs(t) do local ssid, rssi, authmode, channel = string.match print("CURRENT RSSI IS: "..rssi) end end ssid, tmp, bssid_set, bssid=wifi.sta.getconfig() scan_cfg= scan_cfg.ssid=ssid if bssid_set==1 then scan_cfg.bssid=bssid else scan_cfg. scan_cfg.channel=wifi.getchannel() scan_cfg.show_hidden=0 ssid, tmp, bssid_set, bssid=nil, nil, nil, nil wifi.sta.getap(scan_cfg, 1, listap)

­ wifi.sta.getip()

See also

Page 50: NodeMCU API Instruction - Michael · PDF fileNodeMCU API Instruction version 0.9.5 build 2015­02­13 INDEX node module ... wifi.ap.config() wifi.ap.getip() wifi module wifi.sta sub­module

29.6.2015 data:text/html, <html contenteditable>

data:text/html, <html contenteditable> 50/127

­ Back to Index

get current status in station mode.

wifi.sta.status()

nil

number: 0~5

0: STATION_IDLE,1: STATION_CONNECTING,2: STATION_WRONG_PASSWORD,3: STATION_NO_AP_FOUND,4: STATION_CONNECT_FAIL,5: STATION_GOT_IP.

­

­ Back to Index

get getbroadcast address in station mode.

wifi.sta.status()

Description

Syntax

Parameters

Returns

See also

wifi.sta.getbroadcast()

Description

Syntax

Page 51: NodeMCU API Instruction - Michael · PDF fileNodeMCU API Instruction version 0.9.5 build 2015­02­13 INDEX node module ... wifi.ap.config() wifi.ap.getip() wifi module wifi.sta sub­module

29.6.2015 data:text/html, <html contenteditable>

data:text/html, <html contenteditable> 51/127

wifi.sta.getbroadcast()

nil

getbroadcast address in string, for example:"192.168.0.255"return nil if ip = "0.0.0.0".

bc = wifi.sta.getbroadcast() print(bc) ‐‐ 192.168.0.255

­ wifi.sta.getip()

­ Back to Index

set ssid and pwd in ap mode. Be sure to make the pwd value at least 8 characters! If you don't make the pwd value 8 characters, it will default to no password and not set the value for ssid. It will still work as an access point, but you will see a name in your wifi list like: ESP_9997C3

Parameters

Returns

Example

See also

wifi.ap module

wifi.ap.config()

Description

Syntax

Page 52: NodeMCU API Instruction - Michael · PDF fileNodeMCU API Instruction version 0.9.5 build 2015­02­13 INDEX node module ... wifi.ap.config() wifi.ap.getip() wifi module wifi.sta sub­module

29.6.2015 data:text/html, <html contenteditable>

data:text/html, <html contenteditable> 52/127

wifi.ap.config(cfg)

cfg: lua table to setup ap.

cfg= cfg.ssid="myssid" cfg.pwd="mypassword" wifi.ap.config(cfg)

nil

­

­ Back to Index

get ip, netmask, gateway in ap mode.

wifi.ap.getip()

nil

ip, netmask, gateway address in string, for example:"192.168.0.111"

Parameters

Example:

Returns

See also

wifi.ap.getip()

Description

Syntax

Parameters

Returns

Page 53: NodeMCU API Instruction - Michael · PDF fileNodeMCU API Instruction version 0.9.5 build 2015­02­13 INDEX node module ... wifi.ap.config() wifi.ap.getip() wifi module wifi.sta sub­module

29.6.2015 data:text/html, <html contenteditable>

data:text/html, <html contenteditable> 53/127

return nil if ip = "0.0.0.0".

‐‐ print current ip, netmask, gateway print(wifi.ap.getip()) ‐‐ 192.168.4.1 255.255.255.0 192.168.4.1 ip = wifi.ap.getip() print(ip) ‐‐ 192.168.4.1 ip, nm = wifi.ap.getip() print(nm) ‐‐ 255.255.255.0 ip, nm, gw = wifi.ap.getip() print(gw) ‐‐ 192.168.4.1

­ wifi.ap.getmac()

­ Back to Index

set ip, netmask, gateway address in ap mode.

wifi.ap.setip(cfg)

cfg: table contain ip, netmask, and gateway

ip="192.168.1.1", netmask="255.255.255.0", gateway="192.168.1.1"

Example

See also

wifi.ap.setip()

Description

Syntax

Parameters

Page 54: NodeMCU API Instruction - Michael · PDF fileNodeMCU API Instruction version 0.9.5 build 2015­02­13 INDEX node module ... wifi.ap.config() wifi.ap.getip() wifi module wifi.sta sub­module

29.6.2015 data:text/html, <html contenteditable>

data:text/html, <html contenteditable> 54/127

true if success, false if fail.

cfg = ip="192.168.1.1", netmask="255.255.255.0", gateway="192.168.1.1" wifi.ap.setip(cfg)

­ wifi.ap.setmac()

­ Back to Index

get mac address in ap mode.

wifi.ap.getmac()

nil

mac address in string, for example:"1A­33­44­FE­55­BB"

Returns

Example

See also

wifi.ap.getmac()

Description

Syntax

Parameters

Returns

Example

Page 55: NodeMCU API Instruction - Michael · PDF fileNodeMCU API Instruction version 0.9.5 build 2015­02­13 INDEX node module ... wifi.ap.config() wifi.ap.getip() wifi module wifi.sta sub­module

29.6.2015 data:text/html, <html contenteditable>

data:text/html, <html contenteditable> 55/127

wifi.ap.getmac()

­ wifi.ap.getip()

­ Back to Index

set mac address in ap mode.

wifi.ap.setmac(mac)

mac address in byte string, for example:"AC­1D­1C­B1­0B­22"

true if success, false if fail.

print(wifi.ap.setmac("AC‐1D‐1C‐B1‐0B‐22"))

­ wifi.ap.setip()

­ Back to Index

See also

wifi.ap.setmac()

Description

Syntax

Parameters

Returns

Example

See also

wifi.ap.getbroadcast()

Page 56: NodeMCU API Instruction - Michael · PDF fileNodeMCU API Instruction version 0.9.5 build 2015­02­13 INDEX node module ... wifi.ap.config() wifi.ap.getip() wifi module wifi.sta sub­module

29.6.2015 data:text/html, <html contenteditable>

data:text/html, <html contenteditable> 56/127

get getbroadcast address in ap mode.

wifi.ap.getbroadcast()

nil

getbroadcast address in string, for example:"192.168.0.255"return nil if ip = "0.0.0.0".

bc = wifi.ap.getbroadcast() print(bc) ‐‐ 192.168.0.255

­ wifi.ap.getip()

­ Back to Index

delay us micro seconds.

Description

Syntax

Parameters

Returns

Example

See also

timer module

tmr.delay()

Description

Syntax

Page 57: NodeMCU API Instruction - Michael · PDF fileNodeMCU API Instruction version 0.9.5 build 2015­02­13 INDEX node module ... wifi.ap.config() wifi.ap.getip() wifi module wifi.sta sub­module

29.6.2015 data:text/html, <html contenteditable>

data:text/html, <html contenteditable> 57/127

tmr.delay(us)

us: delay time in micro second

nil

‐‐ delay 100us tmr.delay(100)

­ tmr.now()

­ Back to Index

return the current value of system counter: uint31, us.

tmr.now()

nil

uint31: value of counter

Parameters

Returns

Example

See also

tmr.now()

Description

Syntax

Parameters

Returns

Example

Page 58: NodeMCU API Instruction - Michael · PDF fileNodeMCU API Instruction version 0.9.5 build 2015­02­13 INDEX node module ... wifi.ap.config() wifi.ap.getip() wifi module wifi.sta sub­module

29.6.2015 data:text/html, <html contenteditable>

data:text/html, <html contenteditable> 58/127

‐‐ print current value of counter print(tmr.now())

­ tmr.delay()

­ Back to Index

alarm time.

tmr.alarm(id, interval, repeat, function do())

id: 0~6, alarmer id. Interval: alarm time, unit: millisecondrepeat: 0 ­ one time alarm, 1 ­ repeatfunction do(): callback function for alarm timed out

nil

‐‐ print "hello world" every 1000ms tmr.alarm(0, 1000, 1, function() print("hello world

­ tmr.now()

See also

tmr.alarm()

Description

Syntax

Parameters

Returns

Example

See also

Page 59: NodeMCU API Instruction - Michael · PDF fileNodeMCU API Instruction version 0.9.5 build 2015­02­13 INDEX node module ... wifi.ap.config() wifi.ap.getip() wifi module wifi.sta sub­module

29.6.2015 data:text/html, <html contenteditable>

data:text/html, <html contenteditable> 59/127

­ Back to Index

stop alarm.

tmr.stop(id)

id: 0~6, alarmer id.

nil

‐‐ print "hello world" every 1000ms tmr.alarm(1, 1000, 1, function() print("hello world

‐‐ something else

‐‐ stop alarm tmr.stop(1)

­ tmr.now()

­ Back to Index

tmr.stop()

Description

Syntax

Parameters

Returns

Example

See also

tmr.wdclr()

Description

Page 60: NodeMCU API Instruction - Michael · PDF fileNodeMCU API Instruction version 0.9.5 build 2015­02­13 INDEX node module ... wifi.ap.config() wifi.ap.getip() wifi module wifi.sta sub­module

29.6.2015 data:text/html, <html contenteditable>

data:text/html, <html contenteditable> 60/127

clear system watchdog counter.

tmr.wdclr()

nil.

nil

for i=1,10000 do print(i) tmr.wdclr() ‐‐ should call tmr.wdclr() in a long loop to avoid hardware reset caused by watchdog. end

­ tmr.delay()

­ Back to Index

return rtc time since start up in second, uint31 form.

tmr.time()

nil.

Syntax

Parameters

Returns

Example

See also

tmr.time()

Description

Syntax

Parameters

Page 61: NodeMCU API Instruction - Michael · PDF fileNodeMCU API Instruction version 0.9.5 build 2015­02­13 INDEX node module ... wifi.ap.config() wifi.ap.getip() wifi module wifi.sta sub­module

29.6.2015 data:text/html, <html contenteditable>

data:text/html, <html contenteditable> 61/127

number

­ tmr.now()

­ Back to Index

gpio.OUTPUT, gpio.INPUT, gpio.INT, gpio.HIGH, gpio.LOW

initialize pin to GPIO mode, set the pin in/out mode, internal pullup.

gpio.mode(pin, mode, pullup)

pin: 0~12, IO indexmode: gpio.OUTPUT or gpio.INPUT, or gpio.INT(interrupt mode) pullup: gpio.PULLUP or gpio.FLOAT, default: gpio.FLOAT.

Returns

Example

See also

GPIO module

CONSTANT

gpio.mode()

Description

Syntax

Parameters

Returns

Page 62: NodeMCU API Instruction - Michael · PDF fileNodeMCU API Instruction version 0.9.5 build 2015­02­13 INDEX node module ... wifi.ap.config() wifi.ap.getip() wifi module wifi.sta sub­module

29.6.2015 data:text/html, <html contenteditable>

data:text/html, <html contenteditable> 62/127

nil

‐‐ set gpio 0 as output. gpio.mode(0, gpio.OUTPUT)

­ gpio.read()

­ Back to Index

read pin value.

gpio.read(pin)

pin: 0~12, IO index

number:0 ­ low, 1 ­ high

‐‐ read value of gpio 0. gpio.read(0)

­ gpio.mode()

Example

See also

gpio.read()

Description

Syntax

Parameters

Returns

Example

See also

Page 63: NodeMCU API Instruction - Michael · PDF fileNodeMCU API Instruction version 0.9.5 build 2015­02­13 INDEX node module ... wifi.ap.config() wifi.ap.getip() wifi module wifi.sta sub­module

29.6.2015 data:text/html, <html contenteditable>

data:text/html, <html contenteditable> 63/127

­ Back to Index

set pin value.

gpio.write(pin)

pin: 0~12, IO indexlevel: gpio.HIGH or gpio.LOW

nil

‐‐ set pin index 1 to GPIO mode, and set the pin to high. pin=1 gpio.mode(pin, gpio.OUTPUT) gpio.write(pin, gpio.HIGH)

­ gpio.mode()­ gpio.read()

­ Back to Index

gpio.write()

Description

Syntax

Parameters

Returns

Example

See also

gpio.trig()

Description

Page 64: NodeMCU API Instruction - Michael · PDF fileNodeMCU API Instruction version 0.9.5 build 2015­02­13 INDEX node module ... wifi.ap.config() wifi.ap.getip() wifi module wifi.sta sub­module

29.6.2015 data:text/html, <html contenteditable>

data:text/html, <html contenteditable> 64/127

set the interrupt callback function for pin.

gpio.trig(pin, type, function(level))

pin: 1~12, IO index, pin D0 does not support Interrupt.type: "up", "down", "both", "low", "high", which represent rising edge, falling edge, both edge, low level, high level trig mode separately.function(level): callback function when triggered. The gpio level is the param. Use previous callback function if undefined here.

nil

‐‐ use pin 0 as the input pulse width counter pulse1 = 0 du = 0 gpio.mode(1,gpio.INT) function pin1cb(level) du = tmr.now() – pulse1 print(du) pulse1 = tmr.now() if level == 1 then gpio.trig(1, "down") else gpio. end gpio.trig(1, "down",pin1cb)

­ gpio.mode()­ gpio.write()

­ Back to Index

Syntax

Parameters

Returns

Example

See also

Page 65: NodeMCU API Instruction - Michael · PDF fileNodeMCU API Instruction version 0.9.5 build 2015­02­13 INDEX node module ... wifi.ap.config() wifi.ap.getip() wifi module wifi.sta sub­module

29.6.2015 data:text/html, <html contenteditable>

data:text/html, <html contenteditable> 65/127

set pin to PWM mode. Only 3 pins can be set to PWM mode at the most.

pwm.setup(pin, clock, duty)

pin: 1~12, IO indexclock: 1~1000, pwm frequencyduty: 0~1023, pwm duty cycle, max 1023(10bit)

nil

‐‐ set pin index 1 as pwm output, frequency is 100Hz, duty cycle is half. pwm.setup(1, 100, 512)

­ pwm.start()

­ Back to Index

PWM module

pwm.setup()

Description

Syntax

Parameters

Returns

Example

See also

pwm.close()

Description

Page 66: NodeMCU API Instruction - Michael · PDF fileNodeMCU API Instruction version 0.9.5 build 2015­02­13 INDEX node module ... wifi.ap.config() wifi.ap.getip() wifi module wifi.sta sub­module

29.6.2015 data:text/html, <html contenteditable>

data:text/html, <html contenteditable> 66/127

quit PWM mode for specified pin.

pwm.close(pin)

pin: 1~12, IO index

nil

pwm.close(1)

­ pwm.start()

­ Back to Index

pwm starts, you can detect the waveform on the gpio.

pwm.start(pin)

pin: 1~12, IO index

Syntax

Parameters

Returns

Example

See also

pwm.start()

Description

Syntax

Parameters

Returns

Page 67: NodeMCU API Instruction - Michael · PDF fileNodeMCU API Instruction version 0.9.5 build 2015­02­13 INDEX node module ... wifi.ap.config() wifi.ap.getip() wifi module wifi.sta sub­module

29.6.2015 data:text/html, <html contenteditable>

data:text/html, <html contenteditable> 67/127

nil

pwm.start(1)

­ pwm.stop()

­ Back to Index

pause the output of PWM waveform.

pwm.stop(pin)

pin: 1~12, IO index

nil

pwm.stop(1)

­ pwm.start()

­ Back to Index

Example

See also

pwm.stop()

Description

Syntax

Parameters

Returns

Example

See also

Page 68: NodeMCU API Instruction - Michael · PDF fileNodeMCU API Instruction version 0.9.5 build 2015­02­13 INDEX node module ... wifi.ap.config() wifi.ap.getip() wifi module wifi.sta sub­module

29.6.2015 data:text/html, <html contenteditable>

data:text/html, <html contenteditable> 68/127

set pwm frequency for pin.­Note: setup pwm frequency will synchronously change others if there are any. Only one PWM frequency can be allowed for the system.

pwm.setclock(pin, clock)

pin: 1~12, IO index.clock: 1~1000, pwm frequency.

nil

pwm.setclock(1, 100)

­ pwm.getclock()

­ Back to Index

get pwm frequency of pin.

pwm.setclock()

Description

Syntax

Parameters

Returns

Example

See also

pwm.getclock()

Description

Syntax

Page 69: NodeMCU API Instruction - Michael · PDF fileNodeMCU API Instruction version 0.9.5 build 2015­02­13 INDEX node module ... wifi.ap.config() wifi.ap.getip() wifi module wifi.sta sub­module

29.6.2015 data:text/html, <html contenteditable>

data:text/html, <html contenteditable> 69/127

pwm.getclock(pin)

pin: 1~12, IO index.

number:pwm frequency of pin

print(pwm.getclock(1))

­ pwm.setclock()

­ Back to Index

set duty clycle for pin.

pwm.setduty(pin, duty)

pin: 1~12, IO indexduty: 0~1023, pwm duty cycle, max 1023(10bit).

nil

Parameters

Returns

Example

See also

pwm.setduty()

Description

Syntax

Parameters

Returns

Example

Page 70: NodeMCU API Instruction - Michael · PDF fileNodeMCU API Instruction version 0.9.5 build 2015­02­13 INDEX node module ... wifi.ap.config() wifi.ap.getip() wifi module wifi.sta sub­module

29.6.2015 data:text/html, <html contenteditable>

data:text/html, <html contenteditable> 70/127

pwm.setduty(1, 512)

­ pwm.getduty()

­ Back to Index

get duty clycle for pin.

pwm.getduty(pin)

pin: 1~12, IO index

number: duty cycle, max 1023.

‐‐ D1 is connected to green led ‐‐ D2 is connected to blue led ‐‐ D3 is connected to red led pwm.setup(1,500,512) pwm.setup(2,500,512) pwm.setup(3,500,512) pwm.start(1) pwm.start(2) pwm.start(3) function led(r,g,b) pwm.setduty(1,g) pwm.setduty(2,b) pwm.setduty(3,r) end led(512,0,0) ‐‐ set led to red

See also

pwm.getduty()

Description

Syntax

Parameters

Returns

Example

Page 71: NodeMCU API Instruction - Michael · PDF fileNodeMCU API Instruction version 0.9.5 build 2015­02­13 INDEX node module ... wifi.ap.config() wifi.ap.getip() wifi module wifi.sta sub­module

29.6.2015 data:text/html, <html contenteditable>

data:text/html, <html contenteditable> 71/127

led(0,0,512) ‐‐ set led to blue.

­ pwm.setduty()

­ Back to Index

net.TCP, net.UDP

create a server.

net.createServer(type, timeout)

type: net.TCP or net.UDPtimeout: for a TCP server, timeout is 1~28800 seconds, for a inactive client to disconnected.

net.server sub module

net.createServer(net.TCP, 30) ‐‐ 30s timeout

See also

net module

CONSTANT

net.createServer()

Description

Syntax

Parameters

Returns

Example

Page 72: NodeMCU API Instruction - Michael · PDF fileNodeMCU API Instruction version 0.9.5 build 2015­02­13 INDEX node module ... wifi.ap.config() wifi.ap.getip() wifi module wifi.sta sub­module

29.6.2015 data:text/html, <html contenteditable>

data:text/html, <html contenteditable> 72/127

­ net.createConnection()

­ Back to Index

Create a client.

net.createConnection(type, secure)

type: net.TCP or net.UDPsecure: 1 or 0, 1 for ssl link, 0 for normal link

net.socket sub module

net.createConnection(net.UDP, 0)

­ net.createServer()

­ Back to Index

See also

net.createConnection()

Description

Syntax

Parameters

Returns

Example

See also

net.server module

net.server:listen()

Page 73: NodeMCU API Instruction - Michael · PDF fileNodeMCU API Instruction version 0.9.5 build 2015­02­13 INDEX node module ... wifi.ap.config() wifi.ap.getip() wifi module wifi.sta sub­module

29.6.2015 data:text/html, <html contenteditable>

data:text/html, <html contenteditable> 73/127

listen on port from [ip] address.

net.server.listen(port,[ip],function(net.socket))

port: port numberip:ip address string, can be omittedfunction(net.socket): callback function, pass to Caller function as param if a connection is created successfully

nil

‐‐ create a server sv=net.createServer(net.TCP, 30) ‐‐ 30s time out for a inactive client ‐‐ server listen on 80, if data received, print data to console, and send "hello world" to remote. sv:listen(80,function(c) c:on("receive", function(c, pl) print(pl) end c:send("hello world") end)

­ net.createServer()

­ Back to Index

close server.

Description

Syntax

Parameters

Returns

Example

See also

net.server:close()

Description

Page 74: NodeMCU API Instruction - Michael · PDF fileNodeMCU API Instruction version 0.9.5 build 2015­02­13 INDEX node module ... wifi.ap.config() wifi.ap.getip() wifi module wifi.sta sub­module

29.6.2015 data:text/html, <html contenteditable>

data:text/html, <html contenteditable> 74/127

net.server.close()

nil

nil

‐‐ create a server sv=net.createServer(net.TCP, 30) ‐‐ close server sv:close()

­ net.createServer()

­ Back to Index

connect to remote.

connect(port, ip/domain)

Syntax

Parameters

Returns

Example

See also

net.socket module

net.socket:connect()

Description

Syntax

Parameters

Page 75: NodeMCU API Instruction - Michael · PDF fileNodeMCU API Instruction version 0.9.5 build 2015­02­13 INDEX node module ... wifi.ap.config() wifi.ap.getip() wifi module wifi.sta sub­module

29.6.2015 data:text/html, <html contenteditable>

data:text/html, <html contenteditable> 75/127

port: port numberip: ip address or domain name in string

nil

­ net.socket:on() ­ Back to Index

send data to remote via connection.

send(string, function(sent))

string: data in string which will be sent to remotefunction(sent): callback function for sending string

nil

­ net.socket:on()

­ Back to Index

register callback function for event.

Returns

See also

net.socket:send()

Description

Syntax

Parameters

Returns

See also

net.socket:on()

Description

Page 76: NodeMCU API Instruction - Michael · PDF fileNodeMCU API Instruction version 0.9.5 build 2015­02­13 INDEX node module ... wifi.ap.config() wifi.ap.getip() wifi module wifi.sta sub­module

29.6.2015 data:text/html, <html contenteditable>

data:text/html, <html contenteditable> 76/127

on(event, function cb())

event: string, which can be: "connection", "reconnection", "disconnection", "receive", "sent"function cb(net.socket, [string]): callback function. The first param is the socket.If event is "receive", the second param is received data in string.

nil

sk=net.createConnection(net.TCP, 0) sk:on("receive", function(sck, c) print(c) end sk:connect(80,"192.168.0.66") sk:send("GET / HTTP/1.1\r\nHost: 192.168.0.66\r\n

­ net.createServer()

­ Back to Index

close socket.

close()

Syntax

Parameters

Returns

Example

See also

net.socket:close()

Description

Syntax

Page 77: NodeMCU API Instruction - Michael · PDF fileNodeMCU API Instruction version 0.9.5 build 2015­02­13 INDEX node module ... wifi.ap.config() wifi.ap.getip() wifi module wifi.sta sub­module

29.6.2015 data:text/html, <html contenteditable>

data:text/html, <html contenteditable> 77/127

nil

nil

­ net.createServer()

­ Back to Index

get domain ip

dns(domain, function(net.socket, ip))

domain: domain name.function (net.socket, ip): callback function. The first param is the socket, the second param is the ip address in string.

nil

sk=net.createConnection(net.TCP, 0) sk:dns("www.nodemcu.com",function(conn,ip) print sk = nil

Parameters

Returns

See also

net.socket:dns()

Description

Syntax

Parameters

Returns

Example

Page 78: NodeMCU API Instruction - Michael · PDF fileNodeMCU API Instruction version 0.9.5 build 2015­02­13 INDEX node module ... wifi.ap.config() wifi.ap.getip() wifi module wifi.sta sub­module

29.6.2015 data:text/html, <html contenteditable>

data:text/html, <html contenteditable> 78/127

­ net.createServer()

­ Back to Index

i2c.SLOW, i2c.TRANSMITTER, i2c. RECEIVER. FAST(400k)is not supported for now.

initialize i2c.

i2c.setup(id, pinSDA, pinSCL, speed)

id = 0pinSDA: 1~12, IO indexpinSCL: 1~12, IO indexspeed: i2c.SLOW

speed: the seted speed.

­ i2c.read()

­ Back to Index

See also

i2c module

CONSTANT

i2c.setup()

Description

Syntax

Parameters

Returns

See also

Page 79: NodeMCU API Instruction - Michael · PDF fileNodeMCU API Instruction version 0.9.5 build 2015­02­13 INDEX node module ... wifi.ap.config() wifi.ap.getip() wifi module wifi.sta sub­module

29.6.2015 data:text/html, <html contenteditable>

data:text/html, <html contenteditable> 79/127

start i2c transporting.

i2c.start(id)

id = 0

nil

­ i2c.read()

­ Back to Index

stop i2c transporting.

i2c.stop(id)

id = 0

nil

i2c.start()

Description

Syntax

Parameters

Returns

See also

i2c.stop()

Description

Syntax

Parameters

Returns

Page 80: NodeMCU API Instruction - Michael · PDF fileNodeMCU API Instruction version 0.9.5 build 2015­02­13 INDEX node module ... wifi.ap.config() wifi.ap.getip() wifi module wifi.sta sub­module

29.6.2015 data:text/html, <html contenteditable>

data:text/html, <html contenteditable> 80/127

­ i2c.read()

­ Back to Index

setup i2c address and read/write mode.

i2c.address(id, device_addr, direction)

id=0device_addr: device address.direction: i2c.TRANSMITTER for writing mode , i2c. RECEIVER for reading mode

true: get ack false: no ack get

­ i2c.read()

­ Back to Index

write data to i2c, data can be multi numbers, string or lua table.

See also

i2c.address()

Description

Syntax

Parameters

Returns

See also

i2c.write()

Description

Syntax

Page 81: NodeMCU API Instruction - Michael · PDF fileNodeMCU API Instruction version 0.9.5 build 2015­02­13 INDEX node module ... wifi.ap.config() wifi.ap.getip() wifi module wifi.sta sub­module

29.6.2015 data:text/html, <html contenteditable>

data:text/html, <html contenteditable> 81/127

i2c.write(id, data1, data2,...)

id=0data: data can be numbers, string or lua table.

number: number of bytes wrote.

i2c.write(0, "hello", "world")

­ i2c.read()

­ Back to Index

read data for len bytes.

i2c.read(id, len)

id=0len: data length

string:data received.

Parameters

Returns

Example

See also

i2c.read()

Description

Syntax

Parameters

Returns

Example

Page 82: NodeMCU API Instruction - Michael · PDF fileNodeMCU API Instruction version 0.9.5 build 2015­02­13 INDEX node module ... wifi.ap.config() wifi.ap.getip() wifi module wifi.sta sub­module

29.6.2015 data:text/html, <html contenteditable>

data:text/html, <html contenteditable> 82/127

id=0 sda=1 scl=2

‐‐ initialize i2c, set pin1 as sda, set pin2 as scl i2c.setup(id,sda,scl,i2c.SLOW)

‐‐ user defined function: read from reg_addr content of dev_addr function read_reg(dev_addr, reg_addr) i2c.start(id) i2c.address(id, dev_addr ,i2c.TRANSMITTER) i2c.write(id,reg_addr) i2c.stop(id) i2c.start(id) i2c.address(id, dev_addr,i2c.RECEIVER) c=i2c.read(id,1) i2c.stop(id) return c end

‐‐ get content of register 0xAA of device 0x77 reg = read_reg(0x77, 0xAA) print(string.byte(reg))

­ i2c.write()

­ Back to Index

none

See also

adc module

CONSTANT

adc.read()

Description

Page 83: NodeMCU API Instruction - Michael · PDF fileNodeMCU API Instruction version 0.9.5 build 2015­02­13 INDEX node module ... wifi.ap.config() wifi.ap.getip() wifi module wifi.sta sub­module

29.6.2015 data:text/html, <html contenteditable>

data:text/html, <html contenteditable> 83/127

read adc value of id, esp8266 has only one 10bit adc, id=0, pin TOUT

adc.read(id)

id = 0

adc value

­

­ Back to Index

Reading vdd33 pin voltage

adc.readvdd33()

no parameters

mV

print(adc.readvdd33())

Syntax

Parameters

Returns

See also

adc.readvdd33()

Description

Syntax

Parameters

Returns

Example

Page 84: NodeMCU API Instruction - Michael · PDF fileNodeMCU API Instruction version 0.9.5 build 2015­02­13 INDEX node module ... wifi.ap.config() wifi.ap.getip() wifi module wifi.sta sub­module

29.6.2015 data:text/html, <html contenteditable>

data:text/html, <html contenteditable> 84/127

output

3345

v = adc.readvdd33() / 1000 print(v) v=nil

output

3.315

­

­ Back to Index

none

setup uart's baud, databits, parity, stopbits, echo.

uart.setup( id, baud, databits, parity, stopbits, echo )

id = 0, only 1 uart supported.baud = 300, 600, 1200, 2400, 4800, 9600, 19200,

See also

uart module

CONSTANT

uart.setup()

Description

Syntax

Parameters

Page 85: NodeMCU API Instruction - Michael · PDF fileNodeMCU API Instruction version 0.9.5 build 2015­02­13 INDEX node module ... wifi.ap.config() wifi.ap.getip() wifi module wifi.sta sub­module

29.6.2015 data:text/html, <html contenteditable>

data:text/html, <html contenteditable> 85/127

38400, 57600, 74880, 115200, 230400, 460800, 921600, 1843200, 2686400. databits = 5, 6, 7, 8. parity = 0(none).stopbits = 1(1 stopbit), 2(2 stopbit).echo = 0(close echo back).

baud.

­

­ Back to Index

set the callback function to the uart event,"data" event supported, means there is data input from uart.

uart.on(method, [number/end_char], [function], [run_input])

method = "data", there is data input from uart.number/end_char: if pass in a number n if n=0, will receive every char in buffer.if pass in a one char string "c", the callback will called when "c" is encounterd, or max n=255 received.function: callback function, event "data" has a callback like this: function(data) endrun_input: 0 or 1, 0: input from uart will not go into

Returns

See also

uart.on()

Description

Syntax

Parameters

Page 86: NodeMCU API Instruction - Michael · PDF fileNodeMCU API Instruction version 0.9.5 build 2015­02­13 INDEX node module ... wifi.ap.config() wifi.ap.getip() wifi module wifi.sta sub­module

29.6.2015 data:text/html, <html contenteditable>

data:text/html, <html contenteditable> 86/127

lua interpreter, can accept binary data.1: input from uart will go into lua interpreter, and run.

nil

‐‐ when 4 chars is received. uart.on("data", 4, function(data) print("receive from uart:", data) if data=="quit" then uart.on("data") end end, 0) ‐‐ when '\r' is received. uart.on("data", "\r", function(data) print("receive from uart:", data) if data=="quit\r" then uart.on("data") end end, 0)

­

­ Back to Index

write string to uart.

uart.write( id, string1, string2... )

Returns

Example

See also

uart.write()

Description

Syntax

Page 87: NodeMCU API Instruction - Michael · PDF fileNodeMCU API Instruction version 0.9.5 build 2015­02­13 INDEX node module ... wifi.ap.config() wifi.ap.getip() wifi module wifi.sta sub­module

29.6.2015 data:text/html, <html contenteditable>

data:text/html, <html contenteditable> 87/127

id = 0, only 1 uart supported.string1..n: string write to uart.

nil

­

­ Back to Index

none

set a pin in onewire mode.

ow.setup(pin)

pin: 1~12, IO index

nil

Parameters

Returns

See also

onewire module

CONSTANT

ow.setup()

Description

Syntax

Parameters

Returns

Page 88: NodeMCU API Instruction - Michael · PDF fileNodeMCU API Instruction version 0.9.5 build 2015­02­13 INDEX node module ... wifi.ap.config() wifi.ap.getip() wifi module wifi.sta sub­module

29.6.2015 data:text/html, <html contenteditable>

data:text/html, <html contenteditable> 88/127

­

­ Back to Index

Perform a 1­Wire reset cycle.

ow.reset(pin)

pin: 1~12, IO index

number: Returns 1 if a device responds with a presence pulse. Returns 0 if there is no device or the bus is shorted or otherwise held low for more than 250uS

­

­ Back to Index

Issue a 1­Wire rom skip command, to address all on bus.

See also

ow.reset()

Description

Syntax

Parameters

Returns

See also

ow.skip()

Description

Syntax

Page 89: NodeMCU API Instruction - Michael · PDF fileNodeMCU API Instruction version 0.9.5 build 2015­02­13 INDEX node module ... wifi.ap.config() wifi.ap.getip() wifi module wifi.sta sub­module

29.6.2015 data:text/html, <html contenteditable>

data:text/html, <html contenteditable> 89/127

ow.skip(pin)

pin: 1~12, IO index

nil

­

­ Back to Index

Issue a 1­Wire rom select command, make sure you do the ow.reset(pin) first.

ow.select(pin, rom)

pin: 1~12, IO indexrom: string value, len 8, rom code of the salve device

nil

‐‐ 18b20 Examplepin = 9ow.setup(pin)count = 0

Parameters

Returns

See also

ow.select()

Description

Syntax

Parameters

Returns

Example

Page 90: NodeMCU API Instruction - Michael · PDF fileNodeMCU API Instruction version 0.9.5 build 2015­02­13 INDEX node module ... wifi.ap.config() wifi.ap.getip() wifi module wifi.sta sub­module

29.6.2015 data:text/html, <html contenteditable>

data:text/html, <html contenteditable> 90/127

repeat count = count + 1 addr = ow.reset_search(pin) addr = ow.search(pin) tmr.wdclr()until((addr ~= nil) or (count > 100))if (addr == nil) then print("No more addresses.")else print(addr:byte(1,8)) crc = ow.crc8(string.sub(addr,1,7)) if (crc == addr:byte(8)) then if ((addr:byte(1) == 0x10) or (addr:byte(1) == print("Device is a DS18S20 family device.") repeat ow.reset(pin) ow.select(pin, addr) ow.write(pin, 0x44, 1) tmr.delay(1000000) present = ow.reset(pin) ow.select(pin, addr) ow.write(pin,0xBE,1) print("P="..present) data = nil data = string.char(ow.read(pin)) for i = 1, 8 do data = data .. string.char(ow.read(pin)) end print(data:byte(1,9)) crc = ow.crc8(string.sub(data,1,8)) print("CRC="..crc) if (crc == data:byte(9)) then t = (data:byte(1) + data:byte(2) * 256 t1 = t / 10000 t2 = t % 10000 print("Temperature="..t1.."."..t2.."Centigrade end tmr.wdclr() until false else print("Device family is not recognized.") end else print("CRC is not valid!") endend

Page 91: NodeMCU API Instruction - Michael · PDF fileNodeMCU API Instruction version 0.9.5 build 2015­02­13 INDEX node module ... wifi.ap.config() wifi.ap.getip() wifi module wifi.sta sub­module

29.6.2015 data:text/html, <html contenteditable>

data:text/html, <html contenteditable> 91/127

­

­ Back to Index

Write a byte. If 'power' is 1 then the wire is held high at the end for parasitically powered devices. You are responsible for eventually depowering it by calling depower() or doing another read or write.

ow.write(pin, v, power)

pin: 1~12, IO index v: byte to be written to salve device power: 1 for wire being held high for parasitically powered devices.

nil

­

­ Back to Index

See also

ow.write()

Description

Syntax

Parameters

Returns

Example

See also

ow.write_bytes()

Page 92: NodeMCU API Instruction - Michael · PDF fileNodeMCU API Instruction version 0.9.5 build 2015­02­13 INDEX node module ... wifi.ap.config() wifi.ap.getip() wifi module wifi.sta sub­module

29.6.2015 data:text/html, <html contenteditable>

data:text/html, <html contenteditable> 92/127

Write multi bytes. If 'power' is 1 then the wire is held high at the end for parasitically powered devices. You are responsible for eventually depowering it by calling depower() or doing another read or write.

ow.write_bytes(pin, buf, power)

pin: 1~12, IO index buf: string to be written to salve device power: 1 for wire being held high for parasitically powered devices.

nil

­

­ Back to Index

read a byte.

ow.read(pin)

Description

Syntax

Parameters

Returns

Example

See also

ow.read()

Description

Syntax

Parameters

Page 93: NodeMCU API Instruction - Michael · PDF fileNodeMCU API Instruction version 0.9.5 build 2015­02­13 INDEX node module ... wifi.ap.config() wifi.ap.getip() wifi module wifi.sta sub­module

29.6.2015 data:text/html, <html contenteditable>

data:text/html, <html contenteditable> 93/127

pin: 1~12, IO index

byte read from slave device.

­

­ Back to Index

read multi bytes.

ow.read_bytes(pin, size)

pin: 1~12, IO index size: number of bytes to be read from slave device.

string: bytes read from slave device.

­

­ Back to Index

Returns

Example

See also

ow.read_bytes()

Description

Syntax

Parameters

Returns

Example

See also

Page 94: NodeMCU API Instruction - Michael · PDF fileNodeMCU API Instruction version 0.9.5 build 2015­02­13 INDEX node module ... wifi.ap.config() wifi.ap.getip() wifi module wifi.sta sub­module

29.6.2015 data:text/html, <html contenteditable>

data:text/html, <html contenteditable> 94/127

Stop forcing power onto the bus. You only need to do this if you used the 'power' flag to ow.write() or used a ow.write_bytes() and aren't about to do another read or write.

ow.depower(pin)

pin: 1~12, IO index

nil

­

­ Back to Index

Clear the search state so that it will start from the beginning again.

ow.reset_search(pin)

ow.depower()

Description

Syntax

Parameters

Example

Returns

See also

ow.reset_search()

Description

Syntax

Parameters

Page 95: NodeMCU API Instruction - Michael · PDF fileNodeMCU API Instruction version 0.9.5 build 2015­02­13 INDEX node module ... wifi.ap.config() wifi.ap.getip() wifi module wifi.sta sub­module

29.6.2015 data:text/html, <html contenteditable>

data:text/html, <html contenteditable> 95/127

pin: 1~12, IO index

nil

­

­ Back to Index

Setup the search to find the device type 'family_code' on the next call to ow.search() if it is present.

ow.target_search(pin, family_code)

pin: 1~12, IO index family_code: byte for family code.

nil

­

­ Back to Index

Returns

Example

See also

ow.target_search()

Description

Syntax

Parameters

Returns

Example

See also

Page 96: NodeMCU API Instruction - Michael · PDF fileNodeMCU API Instruction version 0.9.5 build 2015­02­13 INDEX node module ... wifi.ap.config() wifi.ap.getip() wifi module wifi.sta sub­module

29.6.2015 data:text/html, <html contenteditable>

data:text/html, <html contenteditable> 96/127

Look for the next device.

ow.search(pin)

pin: 1~12, IO index

if succeed return a string length of 8, which contain the rom code of slave device. if failed in searching next device return nil.

­

­ Back to Index

Compute a Dallas Semiconductor 8 bit CRC, these are used in the ROM and scratchpad registers.

ow.crc8(buf)

ow.search()

Description

Syntax

Parameters

Returns

Example

See also

ow.crc8()

Description

Syntax

Parameters

Page 97: NodeMCU API Instruction - Michael · PDF fileNodeMCU API Instruction version 0.9.5 build 2015­02­13 INDEX node module ... wifi.ap.config() wifi.ap.getip() wifi module wifi.sta sub­module

29.6.2015 data:text/html, <html contenteditable>

data:text/html, <html contenteditable> 97/127

buf: string value, data to be calculated check sum in string.

crc result in byte.

­

­ Back to Index

Compute the 1­Wire CRC16 and compare it against the received CRC.

ow.check_crc16(buf, inverted_crc0, inverted_crc1, crc)

buf: string value, data to be calculated check sum in string. inverted_crc0: LSB of received CRC. inverted_crc1: MSB of received CRC. crc: crc starting value (optional)

bool: true, if the CRC matches; false for dismatches.

Returns

Example

See also

ow.check_crc16()

Description

Syntax

Parameters

Returns

Example

Page 98: NodeMCU API Instruction - Michael · PDF fileNodeMCU API Instruction version 0.9.5 build 2015­02­13 INDEX node module ... wifi.ap.config() wifi.ap.getip() wifi module wifi.sta sub­module

29.6.2015 data:text/html, <html contenteditable>

data:text/html, <html contenteditable> 98/127

­

­ Back to Index

Compute a Dallas Semiconductor 16 bit CRC. This is required to check the integrity of data received from many 1­Wire devices. Note that the CRC computed here is not what you'll get from the 1­Wire network, for two reasons:1) The CRC is transmitted bitwise inverted.2) Depending on the endian­ness of your processor, the binary representation of the two­byte return value may have a different byte order than the two bytes you get from 1­Wire.

ow.crc16(buf, crc)

buf: string value, data to be calculated check sum in string. crc: crc starting value (optional)

return The CRC16, as defined by Dallas Semiconductor.

­

See also

ow.crc16()

Description

Syntax

Parameters

Returns

Example

See also

Page 99: NodeMCU API Instruction - Michael · PDF fileNodeMCU API Instruction version 0.9.5 build 2015­02­13 INDEX node module ... wifi.ap.config() wifi.ap.getip() wifi module wifi.sta sub­module

29.6.2015 data:text/html, <html contenteditable>

data:text/html, <html contenteditable> 99/127

­ Back to Index

none

Bitwise negation, equivalent to ~value in C.

bit.bnot(value)

value: the number to negate.

number: the bitwise negated value of the number.

­

­ Back to Index

bit module

CONSTANT

bit.bnot()

Description

Syntax

Parameters

Returns

Example

See also

bit.band()

Description

Page 100: NodeMCU API Instruction - Michael · PDF fileNodeMCU API Instruction version 0.9.5 build 2015­02­13 INDEX node module ... wifi.ap.config() wifi.ap.getip() wifi module wifi.sta sub­module

29.6.2015 data:text/html, <html contenteditable>

data:text/html, <html contenteditable> 100/127

Bitwise AND, equivalent to val1 & val2 & ... & valn in C.

bit.band(val1, val2, ... valn)

val1: first AND argument.val2: second AND argument.valn: nth AND argument.

number: the bitwise AND of all the arguments.

­

­ Back to Index

Bitwise OR, equivalent to val1 | val2 | ... | valn in C.

bit.bor(val1, val2, ... valn)

val1: first OR argument.val2: second OR argument.valn: nth OR argument.

Syntax

Parameters

Returns

Example

See also

bit.bor()

Description

Syntax

Parameters

Page 101: NodeMCU API Instruction - Michael · PDF fileNodeMCU API Instruction version 0.9.5 build 2015­02­13 INDEX node module ... wifi.ap.config() wifi.ap.getip() wifi module wifi.sta sub­module

29.6.2015 data:text/html, <html contenteditable>

data:text/html, <html contenteditable> 101/127

number: the bitwise OR of all the arguments.

­

­ Back to Index

Bitwise XOR, equivalent to val1 ^ val2 ^ ... ^ valn in C.

bit.bxor(val1, val2, ... valn)

val1: first XOR argument.val2: second XOR argument.valn: nth XOR argument.

number: the bitwise XOR of all the arguments.

­

­ Back to Index

Returns

Example

See also

bit.bxor()

Description

Syntax

Parameters

Returns

Example

See also

Page 102: NodeMCU API Instruction - Michael · PDF fileNodeMCU API Instruction version 0.9.5 build 2015­02­13 INDEX node module ... wifi.ap.config() wifi.ap.getip() wifi module wifi.sta sub­module

29.6.2015 data:text/html, <html contenteditable>

data:text/html, <html contenteditable> 102/127

Left­shift a number, equivalent to value << shift in C.

bit.lshift(value, shift)

value: the value to shift.shift: positions to shift.

number: the number shifted left

­

­ Back to Index

Logical right shift a number, equivalent to ( unsigned )value >> shift in C.

bit.rshift(value, shift)

value: the value to shift.

bit.lshift()

Description

Syntax

Parameters

Returns

Example

See also

bit.rshift()

Description

Syntax

Parameters

Page 103: NodeMCU API Instruction - Michael · PDF fileNodeMCU API Instruction version 0.9.5 build 2015­02­13 INDEX node module ... wifi.ap.config() wifi.ap.getip() wifi module wifi.sta sub­module

29.6.2015 data:text/html, <html contenteditable>

data:text/html, <html contenteditable> 103/127

shift: positions to shift.

number: the number shifted right (logically).

­

­ Back to Index

Arithmetic right shift a number equivalent to value >> shift in C.

bit.arshift(value, shift)

value: the value to shift.shift: positions to shift.

number: the number shifted right (arithmetically).

­

­ Back to Index

Returns

Example

See also

bit.arshift()

Description

Syntax

Parameters

Returns

Example

See also

Page 104: NodeMCU API Instruction - Michael · PDF fileNodeMCU API Instruction version 0.9.5 build 2015­02­13 INDEX node module ... wifi.ap.config() wifi.ap.getip() wifi module wifi.sta sub­module

29.6.2015 data:text/html, <html contenteditable>

data:text/html, <html contenteditable> 104/127

Generate a number with a 1 bit (used for mask generation). Equivalent to 1 << position in C.

bit.bit(position)

position: position of the bit that will be set to 1.

number: a number with only one 1 bit at position (the rest are set to 0).

­

­ Back to Index

Set bits in a number.

bit.set(value, pos1, pos2, ..., posn)

value: the base number.

bit.bit()

Description

Syntax

Parameters

Returns

Example

See also

bit.set()

Description

Syntax

Parameters

Page 105: NodeMCU API Instruction - Michael · PDF fileNodeMCU API Instruction version 0.9.5 build 2015­02­13 INDEX node module ... wifi.ap.config() wifi.ap.getip() wifi module wifi.sta sub­module

29.6.2015 data:text/html, <html contenteditable>

data:text/html, <html contenteditable> 105/127

pos1: position of the first bit to set.pos2: position of the second bit to set.posn: position of the nth bit to set.

number: the number with the bit(s) set in the given position(s).

­

­ Back to Index

Clear bits in a number.

bit.clear(value, pos1, pos2, ..., posn)

value: the base number.pos1: position of the first bit to clear.pos2: position of the second bit to clear.posn: position of thet nth bit to clear.

number: the number with the bit(s) cleared in the given position(s).

Returns

Example

See also

bit.clear()

Description

Syntax

Parameters

Returns

Example

Page 106: NodeMCU API Instruction - Michael · PDF fileNodeMCU API Instruction version 0.9.5 build 2015­02­13 INDEX node module ... wifi.ap.config() wifi.ap.getip() wifi module wifi.sta sub­module

29.6.2015 data:text/html, <html contenteditable>

data:text/html, <html contenteditable> 106/127

­

­ Back to Index

Test if a given bit is set.

bit.isset(value, position)

value: the value to test.position: bit position to test.

boolean: true if the bit at the given position is 1, false otherwise.

­

­ Back to Index

Test if a given bit is cleared.

See also

bit.isset()

Description

Syntax

Parameters

Returns

Example

See also

bit.isclear()

Description

Syntax

Page 107: NodeMCU API Instruction - Michael · PDF fileNodeMCU API Instruction version 0.9.5 build 2015­02­13 INDEX node module ... wifi.ap.config() wifi.ap.getip() wifi module wifi.sta sub­module

29.6.2015 data:text/html, <html contenteditable>

data:text/html, <html contenteditable> 107/127

bit.isclear(value, position)

value: the value to test.position: bit position to test.

boolean: true if the bit at the given position is 0, false othewise.

­

­ Back to Index

MASTER, SLAVE, CPHA_LOW, CPHA_HIGH, CPOL_LOW, CPOL_HIGH, DATABITS_8, DATABITS_16

setup spi configuration.

spi.setup( id, mode, cpol, cpha, databits, clock )

Parameters

Returns

Example

See also

spi module

CONSTANT

spi.setup()

Description

Syntax

Page 108: NodeMCU API Instruction - Michael · PDF fileNodeMCU API Instruction version 0.9.5 build 2015­02­13 INDEX node module ... wifi.ap.config() wifi.ap.getip() wifi module wifi.sta sub­module

29.6.2015 data:text/html, <html contenteditable>

data:text/html, <html contenteditable> 108/127

id: spi id number.mode: MASTER or SLAVE(not supported yet).cpol: CPOL_LOW or CPOL_HIGH, clock polarity.cpha: CPHA_HIGH or CPHA_LOW, clock phase.databits: DATABITS_8 or DATABITS_16.clock: spi clock (not supported yet).

number: 1.

­

­ Back to Index

send data to spi.

wrote = spi.send( id, data1, [data2], ..., [datan] )

id: spi id number.data: data can be either a string, a table or an 8­bit number

number: bytes writen count.

Parameters

Returns

Example

See also

spi.send()

Description

Syntax

Parameters

Returns

Page 109: NodeMCU API Instruction - Michael · PDF fileNodeMCU API Instruction version 0.9.5 build 2015­02­13 INDEX node module ... wifi.ap.config() wifi.ap.getip() wifi module wifi.sta sub­module

29.6.2015 data:text/html, <html contenteditable>

data:text/html, <html contenteditable> 109/127

­

­ Back to Index

recv data from spi.

read = spi.recv( id, size )

id: spi id number.size: data size want to read.

string: string bytes read from spi.

­

­ Back to Index

Example

See also

spi.recv()

Description

Syntax

Parameters

Returns

Example

See also

mqtt module

CONSTANT

Page 110: NodeMCU API Instruction - Michael · PDF fileNodeMCU API Instruction version 0.9.5 build 2015­02­13 INDEX node module ... wifi.ap.config() wifi.ap.getip() wifi module wifi.sta sub­module

29.6.2015 data:text/html, <html contenteditable>

data:text/html, <html contenteditable> 110/127

Create a MQTT client. The client adheres to version 3.1.1 of the MQTT protocol, make sure that your broker supports and is correctly configured for version 3.1.1 of the MQTT protocol. The client is incompatible with brokers running version 3.1 of the MQTT protocol.

mqtt.Client(clientid, keepalive, user, pass)

clientid: the client id.keepalive: keepalive second, a number.user: user name, a string.pass: user password, a string.

mqtt client.

‐‐ init mqtt client with keepalive timer 120secm = mqtt.Client("clientid", 120, "user", "password

‐‐ setup Last Will and Testament (optional)‐‐ Broker will publish a message with qos = 0, retain = 0, data = "offline" ‐‐ to topic "/lwt" if client don't send keepalive packetm:lwt("/lwt", "offline", 0, 0)

m:on("connect", function(con) print ("connected") m:on("offline", function(con) print ("offline") end

‐‐ on publish message receive eventm:on("message", function(conn, topic, data) print(topic .. ":" ) if data ~= nil then

mqtt.Client()

Description

Syntax

Parameters

Returns

Example

Page 111: NodeMCU API Instruction - Michael · PDF fileNodeMCU API Instruction version 0.9.5 build 2015­02­13 INDEX node module ... wifi.ap.config() wifi.ap.getip() wifi module wifi.sta sub­module

29.6.2015 data:text/html, <html contenteditable>

data:text/html, <html contenteditable> 111/127

print(data) endend)

‐‐ for secure: m:connect("192.168.11.118", 1880, 1)m:connect("192.168.11.118", 1880, 0, function(conn

‐‐ subscribe topic with qos = 0m:subscribe("/topic",0, function(conn) print("subscribe success

‐‐ publish a message with data = hello, QoS = 0, retain = 0m:publish("/topic","hello",0,0, function(conn) print

m:close();‐‐ you can call m:connect again

­

­ Back to Index

setup Last Will and Testament (optional)Broker will publish a message with qos = 0, retain = 0, data = "offline" to topic "/lwt" if client don't send keepalive packet.

mqtt:lwt(topic, message, qos, retain)

topic: the topic to publish to, String.

See also

mqtt client module

mqtt.client:lwt()

Description

Syntax

Parameters

Page 112: NodeMCU API Instruction - Michael · PDF fileNodeMCU API Instruction version 0.9.5 build 2015­02­13 INDEX node module ... wifi.ap.config() wifi.ap.getip() wifi module wifi.sta sub­module

29.6.2015 data:text/html, <html contenteditable>

data:text/html, <html contenteditable> 112/127

message: the message to publish, Buffer or String.qos: qos level, default 0.retain: retain flag, default 0.

nil.

­

­ Back to Index

Connects to the broker specified by the given host, port, and secure options

mqtt:connect( host, port, secure, function(client) )

host: host domain or ip, string.port: number, broker port.secure: 0 or 1, default 0.function(client): when connected, call this function.

nil.

Returns

Example

See also

mqtt.client:connect()

Description

Syntax

Parameters

Returns

Example

See also

Page 113: NodeMCU API Instruction - Michael · PDF fileNodeMCU API Instruction version 0.9.5 build 2015­02­13 INDEX node module ... wifi.ap.config() wifi.ap.getip() wifi module wifi.sta sub­module

29.6.2015 data:text/html, <html contenteditable>

data:text/html, <html contenteditable> 113/127

­

­ Back to Index

close connection to the broker.

mqtt:close()

nil

nil.

­

­ Back to Index

Publish a message

mqtt:publish( topic, payload, qos, retain, function(client) )

mqtt.client:close()

Description

Syntax

Parameters

Returns

Example

See also

mqtt.client:publish()

Description

Syntax

Page 114: NodeMCU API Instruction - Michael · PDF fileNodeMCU API Instruction version 0.9.5 build 2015­02­13 INDEX node module ... wifi.ap.config() wifi.ap.getip() wifi module wifi.sta sub­module

29.6.2015 data:text/html, <html contenteditable>

data:text/html, <html contenteditable> 114/127

topic: the topic to publish to, stringmessage: the message to publish, stringqos: qos level, default 0retain: retain flag, default 0function(client): callback fired when PUBACK received.

nil.

­

­ Back to Index

Subscribe to a topic or topics

mqtt:subscribe(topic, qos, function(client, topic, message))

topic: a string topic to subscribe toqos: qos subscription level, default 0function(client, topic, message): callback fired when message received.

Parameters

Returns

Example

See also

mqtt.client:subscribe()

Description

Syntax

Parameters

Returns

Page 115: NodeMCU API Instruction - Michael · PDF fileNodeMCU API Instruction version 0.9.5 build 2015­02­13 INDEX node module ... wifi.ap.config() wifi.ap.getip() wifi module wifi.sta sub­module

29.6.2015 data:text/html, <html contenteditable>

data:text/html, <html contenteditable> 115/127

nil.

­

­ Back to Index

register callback function to event.

mqtt:on(event, function(client, [topic], [message]))

event: string, which can be: "connect", "message", "offline"function cb(client, [topic], [message]): callback function. The first param is the client.If event is "message", the 2nd and 3rd param are received topic and message in string.

nil.

­

Example

See also

mqtt.client:on()

Description

Syntax

Parameters

Returns

Example

See also

WS2812 Module

Page 116: NodeMCU API Instruction - Michael · PDF fileNodeMCU API Instruction version 0.9.5 build 2015­02­13 INDEX node module ... wifi.ap.config() wifi.ap.getip() wifi module wifi.sta sub­module

29.6.2015 data:text/html, <html contenteditable>

data:text/html, <html contenteditable> 116/127

Send the RGB Data in 8bits to WS2812

ws2812.writergb(pin, string.char(R1,G1,B1(,R2,G2,B2...)) )

pin = Supported all the PINs(0,1,2...)R1 = The first WS2812 though the line's Red Channel's Parameters(0­255)G1 = The first WS2812 though the line's Green Channel's Parameters(0­255) B1 = The first WS2812 though the line's Blue Channel's Parameters(0­255) ... You can connect a lot of WS2812...R2,G2,B2 is the next WS2812's Red, Green and Blue Channel's Parameters

nil

Encode table to json string

cjson.encode(table)

ws2812.writergb()

Description

Syntax

Parameters

Return

cjson.encode()

Description

Syntax

Parameters

Page 117: NodeMCU API Instruction - Michael · PDF fileNodeMCU API Instruction version 0.9.5 build 2015­02­13 INDEX node module ... wifi.ap.config() wifi.ap.getip() wifi module wifi.sta sub­module

29.6.2015 data:text/html, <html contenteditable>

data:text/html, <html contenteditable> 117/127

table = data to encode

json string

print(cjson.encode(key="value"))

Decode json string to table

cjson.decode(s)

s = string to decode

Lua table

t= cjson.decode("\"key\":\"value\"")for k,v in pairs(t) do print(k,v) end

­ Back to Index

Return

Example

cjson.decode()

Description

Syntax

Parameters

Return

Example

u8g module

Page 118: NodeMCU API Instruction - Michael · PDF fileNodeMCU API Instruction version 0.9.5 build 2015­02­13 INDEX node module ... wifi.ap.config() wifi.ap.getip() wifi module wifi.sta sub­module

29.6.2015 data:text/html, <html contenteditable>

data:text/html, <html contenteditable> 118/127

u8g.DRAW_UPPER_RIGHT, u8g.DRAW_UPPER_LEFT, u8g.DRAW_LOWER_RIGHT, u8g.DRAW_LOWER_LEFT, u8g.DRAW_ALL, u8g.MODE_BW, u8g.MODE_GRAY2BIT

u8g.font_6x10, ...

Initialize an SSD1306 128x64 display via I2C.

u8g.ssd1306_128x64_i2c(sla)

sla: I2C slave address.

u8g display.

sda = 5scl = 6i2c.setup(0, sda, scl, i2c.SLOW)

sla = 0x3cdisp = u8g.ssd1306_128x64_i2c(sla)

­ Back to Index

CONSTANT

u8g.ssd1306_128x64_i2c()

Description

Syntax

Parameters

Returns

Example

See also

Page 119: NodeMCU API Instruction - Michael · PDF fileNodeMCU API Instruction version 0.9.5 build 2015­02­13 INDEX node module ... wifi.ap.config() wifi.ap.getip() wifi module wifi.sta sub­module

29.6.2015 data:text/html, <html contenteditable>

data:text/html, <html contenteditable> 119/127

Initialize an SSD1306 128x64 display via SPI.

u8g.ssd1306_128x64_spi(cs, dc, res)

cs: GPIO pin for /CS.dc: GPIO pin for DC.res: GPIO pin for /RES.

u8g display.

spi.setup(1, spi.MASTER, spi.CPOL_LOW, spi.CPHA_LOW

cs = 8 ‐‐ GPIO15, pull‐down 10k to GNDdc = 4 ‐‐ GPIO2res = 0 ‐‐ GPIO16, RES is optional YMMVdisp = u8g.ssd1306_128x64_spi(cs, dc, res)

­ Back to Index

The Lua bindings for this library closely follow u8glib's object oriented C++ API. Visit theu8glib

u8g.ssd1306_128x64_spi()

Description

Syntax

Parameters

Returns

Example

See also

u8g display sub­module

Page 120: NodeMCU API Instruction - Michael · PDF fileNodeMCU API Instruction version 0.9.5 build 2015­02­13 INDEX node module ... wifi.ap.config() wifi.ap.getip() wifi module wifi.sta sub­module

29.6.2015 data:text/html, <html contenteditable>

data:text/html, <html contenteditable> 120/127

homepage for technical details.

Draw a bitmap at the specified x/y position (upper left corner of the bitmap). Parts of the bitmap may be outside the display boundaries. The bitmap is specified by the array bitmap. A cleared bit means: Do not draw a pixel. A set bit inside the array means: Write pixel with the current color index. For a monochrome display, the color index 0 will usually clear a pixel and the color index 1 will set a pixel.

disp.drawBitmap(x, y, cnt, h, bitmap)

x: X­position (left position of the bitmap).y: Y­position (upper position of the bitmap).cnt: Number of bytes of the bitmap in horizontal direction. The width of the bitmap is cnt*8.h: Height of the bitmap.bitmap: Bitmap data supplied as string.

nil

lua_examples/u8glib/u8g_bitmaps.lua

u8glib drawBitmap()

­ Back to Index

u8g.disp:drawBitmap()

Description

Syntax

Parameters

Returns

Example

See also

Page 121: NodeMCU API Instruction - Michael · PDF fileNodeMCU API Instruction version 0.9.5 build 2015­02­13 INDEX node module ... wifi.ap.config() wifi.ap.getip() wifi module wifi.sta sub­module

29.6.2015 data:text/html, <html contenteditable>

data:text/html, <html contenteditable> 121/127

Draw a XBM Bitmap. Position (x,y) is the upper left corner of the bitmap. XBM contains monochrome, 1­bit bitmaps. This procedure only draws pixel values 1. The current color index is used for drawing (see setColorIndex). Pixel with value 0 are not drawn (transparent).

Bitmaps and XBMs are supplied as strings to drawBitmap() and drawXBM(). This off­loads all data handling from the u8g module to generic methods for binary files. In contrast to the source code based inclusion of XBMs into u8glib, it's required to provide precompiled binary files. This can be performed online with Online­Utility's Image Converter: Convert from XBM to MONO format and upload the binary result with nodemcu­uploader.py.

disp.drawXBM(x, y, w, h, bitmap)

x: X­position (left position of the bitmap).y: Y­position (upper position of the bitmap).w: Width of the bitmap.h: Height of the bitmap.bitmap: XBM data supplied as string.

nil

lua_examples/u8glib/u8g_bitmaps.lua

u8g.disp:drawXBM()

Description

Syntax

Parameters

Returns

Example

Page 122: NodeMCU API Instruction - Michael · PDF fileNodeMCU API Instruction version 0.9.5 build 2015­02­13 INDEX node module ... wifi.ap.config() wifi.ap.getip() wifi module wifi.sta sub­module

29.6.2015 data:text/html, <html contenteditable>

data:text/html, <html contenteditable> 122/127

u8glib drawXBM()

­ Back to Index

u8glib comes with a wide range of fonts for small displays. Since they need to be compiled into the firmware image, you'd need to include them in app/include/u8g_config.h and recompile. Simply add the desired fonts to the font table:

#define U8G_FONT_TABLE \ U8G_FONT_TABLE_ENTRY(font_6x10) \ U8G_FONT_TABLE_ENTRY(font_chikita)

They'll be available as u8g.<font_name> in Lua.

disp.setFont(font)

font: Constant to indentify pre­compiled font.

nil

disp:setFont(u8g.font_6x10)

See also

u8g.disp:setFont()

Description

Syntax

Parameters

Returns

Example

See also

Page 123: NodeMCU API Instruction - Michael · PDF fileNodeMCU API Instruction version 0.9.5 build 2015­02­13 INDEX node module ... wifi.ap.config() wifi.ap.getip() wifi module wifi.sta sub­module

29.6.2015 data:text/html, <html contenteditable>

data:text/html, <html contenteditable> 123/127

u8glib setFont()

­ Back to Index

dht.OK, dht.ERROR_CHECKSUM, dht.ERROR_TIMEOUT

dht.OK is 0, dht.ERROR_CHECKSUM is 1, dht.ERROR_TIMEOUT is 2

Read all kinds of dht sensors, including dht11, 21, 22, 33, 44 humidity temperature combo sensor.

dht.read(pin)

pin: pin number of dht sensor (can't be 0), type is number

integer of status, number of temperature, humidity, decimial of temperature, decimial of humidity.status is integer, temperature, humidity, decimial of temperature, decimial of humidity is number. *Note: If using float firmware, the temperature, humidity already with decimial.

dht module

CONSTANT

dht.read()

Description

Syntax

Parameters

Return

Page 124: NodeMCU API Instruction - Michael · PDF fileNodeMCU API Instruction version 0.9.5 build 2015­02­13 INDEX node module ... wifi.ap.config() wifi.ap.getip() wifi module wifi.sta sub­module

29.6.2015 data:text/html, <html contenteditable>

data:text/html, <html contenteditable> 124/127

pin = 5status,temp,humi,temp_decimial,humi_decimial = dht.if( status == dht.OK ) then ‐‐ Integer firmware using this example print( string.format( "DHT Temperature:%d.%03d;Humidity:%d.%03d\r\n math.floor(temp), temp_decimial, math.floor(humi), humi_decimial ) ) ‐‐ Float firmware using this example print("DHT Temperature:"..temp..";".."Humidity:"elseif( status == dht.ERROR_CHECKSUM ) then print( "DHT Checksum error." );elseif( status == dht.ERROR_TIMEOUT ) then print( "DHT Time out." );end

Read dht11 humidity temperature combo sensor.

dht.read11(pin)

pin: pin number of dht sensor (can't be 0), type is number

integer of status, number of temperature, humidity, decimial of temperature, decimial of humidity.

Example

dht.read11()

Description

Syntax

Parameters

Return

Page 125: NodeMCU API Instruction - Michael · PDF fileNodeMCU API Instruction version 0.9.5 build 2015­02­13 INDEX node module ... wifi.ap.config() wifi.ap.getip() wifi module wifi.sta sub­module

29.6.2015 data:text/html, <html contenteditable>

data:text/html, <html contenteditable> 125/127

status is integer, temperature, humidity, decimial of temperature, decimial of humidity is number. *Note: If using float firmware, the temperature, humidity already with decimial.

pin = 5status,temp,humi,temp_decimial,humi_decimial = dht.if( status == dht.OK ) then ‐‐ Integer firmware using this example print( string.format( "DHT Temperature:%d.%03d;Humidity:%d.%03d\r\n math.floor(temp), temp_decimial, math.floor(humi), humi_decimial ) ) ‐‐ Float firmware using this example print("DHT Temperature:"..temp..";".."Humidity:"elseif( status == dht.ERROR_CHECKSUM ) then print( "DHT Checksum error." );elseif( status == dht.ERROR_TIMEOUT ) then print( "DHT Time out." );end

Read all kinds of dht sensors, except dht11.

dht.readxx(pin)

pin: pin number of dht sensor (can't be 0), type is number

Example

dht.readxx()

Description

Syntax

Parameters

Page 126: NodeMCU API Instruction - Michael · PDF fileNodeMCU API Instruction version 0.9.5 build 2015­02­13 INDEX node module ... wifi.ap.config() wifi.ap.getip() wifi module wifi.sta sub­module

29.6.2015 data:text/html, <html contenteditable>

data:text/html, <html contenteditable> 126/127

integer of status, number of temperature, humidity, decimial of temperature, decimial of humidity.status is integer, temperature, humidity, decimial of temperature, decimial of humidity is number. *Note: If using float firmware, the temperature, humidity already with decimial.

pin = 5status,temp,humi,temp_decimial,humi_decimial = dht.if( status == dht.OK ) then ‐‐ Integer firmware using this example print( string.format( "DHT Temperature:%d.%03d;Humidity:%d.%03d\r\n math.floor(temp), temp_decimial, math.floor(humi), humi_decimial ) ) ‐‐ Float firmware using this example print("DHT Temperature:"..temp..";".."Humidity:"elseif( status == dht.ERROR_CHECKSUM ) then print( "DHT Checksum error." );elseif( status == dht.ERROR_TIMEOUT ) then print( "DHT Time out." );end

Return

Example

The MIT License (MIT)

Copyright (c) 2014 zeroday nodemcu.com

Permission is hereby granted, free of charge, to any person obtaining a copyof this software and associated documentation files (the "Software"), to dealin the Software without restriction, including without limitation the rightsto use, copy, modify, merge, publish, distribute, sublicense, and/or sellcopies of the Software, and to permit persons to whom the Software isfurnished to do so, subject to the following conditions:

Page 127: NodeMCU API Instruction - Michael · PDF fileNodeMCU API Instruction version 0.9.5 build 2015­02­13 INDEX node module ... wifi.ap.config() wifi.ap.getip() wifi module wifi.sta sub­module

29.6.2015 data:text/html, <html contenteditable>

data:text/html, <html contenteditable> 127/127

The above copyright notice and this permission notice shall be included in allcopies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS ORIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THEAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHERLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THESOFTWARE.