2015-03-06 09:34:24 +01:00
# **NodeMCU** #
2015-01-05 14:56:55 +01:00
version 0.9.5
2015-03-05 02:22:02 +01:00
2015-03-15 08:01:07 +01:00
[![Join the chat at https://gitter.im/nodemcu/nodemcu-firmware ](https://badges.gitter.im/Join%20Chat.svg )](https://gitter.im/nodemcu/nodemcu-firmware?utm_source=badge& utm_medium=badge& utm_campaign=pr-badge& utm_content=badge)
2015-03-10 16:19:30 +01:00
[![Build Status ](https://travis-ci.org/nodemcu/nodemcu-firmware.svg )](https://travis-ci.org/nodemcu/nodemcu-firmware) [![Download ](https://img.shields.io/badge/download-~400k-orange.svg )](https://github.com/nodemcu/nodemcu-firmware/releases/latest)
2015-03-05 02:22:02 +01:00
2014-11-12 17:17:08 +01:00
###A lua based firmware for wifi-soc esp8266
2015-01-24 16:36:04 +01:00
Build on [ESP8266 sdk 0.9.5 ](http://bbs.espressif.com/viewtopic.php?f=5&t=154 )< br />
2014-12-22 12:37:49 +01:00
Lua core based on [eLua project ](http://www.eluaproject.net/ )< br />
File system based on [spiffs ](https://github.com/pellepl/spiffs )< br />
Open source development kit for NodeMCU [nodemcu-devkit ](https://github.com/nodemcu/nodemcu-devkit )< br />
Flash tool for NodeMCU [nodemcu-flasher ](https://github.com/nodemcu/nodemcu-flasher )< br />
2014-12-22 12:35:05 +01:00
2015-03-06 09:34:24 +01:00
wiki: [NodeMCU wiki ](https://github.com/nodemcu/nodemcu-firmware/wiki )< br />
api: [NodeMCU api ](https://github.com/nodemcu/nodemcu-firmware/wiki/nodemcu_api_en )< br />
2014-12-22 12:37:49 +01:00
home: [nodemcu.com ](http://www.nodemcu.com )< br />
2015-01-23 04:48:05 +01:00
bbs: [Chinese bbs ](http://bbs.nodemcu.com )< br />
2015-03-06 09:38:28 +01:00
docs: [NodeMCU docs ](http://www.nodemcu.com/docs/ )< br />
2015-01-23 04:48:05 +01:00
Tencent QQ group: 309957875< br / >
2014-12-22 12:35:05 +01:00
2015-01-06 14:52:14 +01:00
# Summary
- Easy to access wireless router
2015-03-06 09:34:24 +01:00
- Based on Lua 5.1.4 (without *debug, os* module.)
2015-01-06 14:52:14 +01:00
- Event-Drive programming preferred.
2015-03-06 09:34:24 +01:00
- Build-in file, timer, pwm, i2c, spi, 1-wire, net, mqtt, coap, gpio, wifi, adc, uart and system api.
2015-01-06 14:52:14 +01:00
- GPIO pin re-mapped, use the index to access gpio, i2c, pwm.
# To Do List (pull requests are very welcomed)
2015-03-06 09:34:24 +01:00
- loadable c module
2015-01-06 14:52:14 +01:00
- fix wifi smart connect
2015-01-24 16:36:04 +01:00
- add spi module (done)
- add mqtt module (done)
2015-03-06 09:34:24 +01:00
- add coap module (done)
2015-02-13 12:35:05 +01:00
- cross compiler (done)
2015-01-06 14:52:14 +01:00
2015-01-26 16:41:24 +01:00
# Change log
2015-03-15 13:12:29 +01:00
2015-03-15< br / >
bugs fixed: #239 , #273 .< br />
reduce coap module memory usage, add coap module to default built.
2015-03-11 06:21:19 +01:00
2015-03-11< br / >
fix bugs of spiffs.< br / >
build both float and integer version [latest releases ](https://github.com/nodemcu/nodemcu-firmware/releases/latest ).< br />
fix tmr.time().< br / >
fix memory leak when DNS fail.
2015-03-09 18:12:36 +01:00
2015-03-10< br / >
update to the recent spiffs.< br / >
add file.fsinfo() api, usage: remain, used, total = file.fsinfo().< br / >
add Travis CI. please download the latest firmware from [releases ](https://github.com/nodemcu/nodemcu-firmware/releases ).< br />
add math lib, partial api work.< br / >
u8g module, ws2812 module default enabled in dev-branch build.
2015-02-13 08:11:59 +01:00
2015-02-13< br / >
add node.compile() api to compile lua text file into lua bytecode file.< br / >
this will reduce memory usage noticeably when require modules into NodeMCU.< br / >
raise internal LUA_BUFFERSIZE from 1024 to 4096.< br / >
lua require("mod") will load "mod.lc" file first if exist.< br / >
build latest pre_build bin.
2015-02-12 04:31:06 +01:00
2015-02-12< br / >
fix float print.< br / >
update spiffs, add file.rename api to file module.< br / >
fix some file system bug. need more tests.< br / >
add support to 8Mbyte, 16Mbyte flash.< br / >
remove node.led() and node.key() api.< br / >
some update to lua_modules and examples.< br / >
build latest pre_build bin.
2015-01-26 16:46:01 +01:00
2015-01-27< br / >
2015-01-27 06:56:34 +01:00
support floating point LUA.< br / >
use macro LUA_NUMBER_INTEGRAL in user_config.h control this feature.< br / >
2015-01-27 06:55:13 +01:00
LUA_NUMBER_INTEGRAL to disable floating point support,< br / >
// LUA_NUMBER_INTEGRAL to enable floating point support.< br / >
2015-01-27 13:47:41 +01:00
fix tmr.time(). #132 < br />
fix filesystem length. #113 < br />
fix ssl reboots. #134 < br />
2015-01-26 16:46:01 +01:00
build pre_build bin.
2015-01-26 16:41:24 +01:00
2015-01-26< br / >
2015-01-27 06:56:34 +01:00
applied sdk095_patch1 to sdk 0.9.5.< br / >
added LUA examples and modules [by dvv ](https://github.com/dvv ). < br />
added node.readvdd33() API [by alonewolfx2 ](https://github.com/alonewolfx2 ).< br />
2015-01-26 16:41:24 +01:00
build pre_build bin.
2015-01-08 09:03:21 +01:00
2015-01-25 05:10:40 +01:00
[more change log ](https://github.com/nodemcu/nodemcu-firmware/wiki )< br />
2014-11-07 05:59:19 +01:00
2014-12-19 09:56:07 +01:00
##GPIO NEW TABLE ( Build 20141219 and later)
< a id = "new_gpio_map" > < / a >
< table >
< tr >
< th scope = "col" > IO index< / th > < th scope = "col" > ESP8266 pin< / th > < th scope = "col" > IO index< / th > < th scope = "col" > ESP8266 pin< / th >
< / tr >
< tr >
< td > 0 [*]< / td > < td > GPIO16< / td > < td > 8< / td > < td > GPIO15< / td >
< / tr >
< tr >
2014-12-28 08:59:46 +01:00
< td > 1< / td > < td > GPIO5< / td > < td > 9< / td > < td > GPIO3< / td >
2014-12-19 09:56:07 +01:00
< / tr >
< tr >
2014-12-28 08:59:46 +01:00
< td > 2< / td > < td > GPIO4< / td > < td > 10< / td > < td > GPIO1< / td >
2014-12-19 09:56:07 +01:00
< / tr >
< tr >
< td > 3< / td > < td > GPIO0< / td > < td > 11< / td > < td > GPIO9< / td >
< / tr >
< tr >
< td > 4< / td > < td > GPIO2< / td > < td > 12< / td > < td > GPIO10< / td >
< / tr >
< tr >
< td > 5< / td > < td > GPIO14< / td > < td > < / td > < td > < / td >
< / tr >
< tr >
< td > 6< / td > < td > GPIO12< / td > < td > < / td > < td > < / td >
< / tr >
< tr >
2015-02-27 22:55:20 +01:00
< td > 7< / td > < td > GPIO13< / td > < td > < / td > < td > < / td >
2014-12-19 09:56:07 +01:00
< / tr >
< / table >
#### [*] D0(GPIO16) can only be used as gpio read/write. no interrupt supported. no pwm/i2c/ow supported.
2014-12-22 12:35:05 +01:00
#Build option
2015-03-06 09:34:24 +01:00
####file ./app/include/user_modules.h
2014-12-22 12:35:05 +01:00
```c
2015-03-06 09:34:24 +01:00
#define LUA_USE_BUILTIN_STRING // for string.xxx()
#define LUA_USE_BUILTIN_TABLE // for table.xxx()
#define LUA_USE_BUILTIN_COROUTINE // for coroutine.xxx()
#define LUA_USE_BUILTIN_MATH // for math.xxx(), partially work
// #define LUA_USE_BUILTIN_IO // for io.xxx(), partially work
// #define LUA_USE_BUILTIN_OS // for os.xxx(), not work
// #define LUA_USE_BUILTIN_DEBUG // for debug.xxx(), not work
2014-12-22 12:35:05 +01:00
#define LUA_USE_MODULES
2015-03-06 09:34:24 +01:00
2014-12-22 12:35:05 +01:00
#ifdef LUA_USE_MODULES
#define LUA_USE_MODULES_NODE
#define LUA_USE_MODULES_FILE
#define LUA_USE_MODULES_GPIO
#define LUA_USE_MODULES_WIFI
#define LUA_USE_MODULES_NET
#define LUA_USE_MODULES_PWM
#define LUA_USE_MODULES_I2C
2015-03-06 09:34:24 +01:00
#define LUA_USE_MODULES_SPI
2014-12-22 12:35:05 +01:00
#define LUA_USE_MODULES_TMR
#define LUA_USE_MODULES_ADC
#define LUA_USE_MODULES_UART
#define LUA_USE_MODULES_OW
2015-01-05 14:56:55 +01:00
#define LUA_USE_MODULES_BIT
2015-03-06 09:34:24 +01:00
#define LUA_USE_MODULES_MQTT
// #define LUA_USE_MODULES_COAP // need about 4k more ram for now
#define LUA_USE_MODULES_U8G
2015-02-05 19:04:09 +01:00
#define LUA_USE_MODULES_WS2812
2014-12-22 12:35:05 +01:00
#endif /* LUA_USE_MODULES */
```
2014-11-07 05:59:19 +01:00
2014-11-12 17:17:08 +01:00
#Flash the firmware
2015-01-05 14:56:55 +01:00
nodemcu_latest.bin: 0x00000< br / >
2014-12-05 08:57:34 +01:00
for most esp8266 modules, just pull GPIO0 down and restart.< br / >
You can use the [nodemcu-flasher ](https://github.com/nodemcu/nodemcu-flasher ) to burn the firmware.
2014-11-11 05:31:45 +01:00
2014-12-22 13:20:20 +01:00
Or, if you build your own bin from source code.< br / >
2015-01-24 16:36:04 +01:00
0x00000.bin: 0x00000< br / >
0x10000.bin: 0x10000< br / >
2014-12-22 13:20:20 +01:00
2015-01-07 08:57:17 +01:00
*Better run file.format() after flash*
2014-12-22 13:20:20 +01:00
2014-11-12 17:17:08 +01:00
#Connect the hardware in serial
2014-12-30 16:26:05 +01:00
baudrate:9600
2014-11-11 05:31:45 +01:00
2014-11-12 17:17:08 +01:00
#Start play
2014-11-11 05:31:45 +01:00
2014-11-12 17:17:08 +01:00
####Connect to your ap
2014-10-13 14:24:52 +02:00
2014-11-12 17:17:08 +01:00
```lua
2015-01-16 14:04:38 +01:00
ip = wifi.sta.getip()
print(ip)
--nil
2014-11-07 05:59:19 +01:00
wifi.setmode(wifi.STATION)
2014-11-12 17:17:08 +01:00
wifi.sta.config("SSID","password")
2015-01-16 14:04:38 +01:00
ip = wifi.sta.getip()
print(ip)
2014-11-12 17:17:08 +01:00
--192.168.18.110
2014-11-07 05:59:19 +01:00
```
2014-10-13 14:24:52 +02:00
2014-11-12 17:17:08 +01:00
####Manipulate hardware like a arduino
2015-03-03 10:20:02 +01:00
2014-11-12 17:17:08 +01:00
```lua
pin = 1
gpio.mode(pin,gpio.OUTPUT)
gpio.write(pin,gpio.HIGH)
print(gpio.read(pin))
2014-11-07 05:59:19 +01:00
```
2014-10-13 14:24:52 +02:00
2014-11-12 17:17:08 +01:00
####Write network application in nodejs style
2015-03-03 10:20:02 +01:00
2014-11-12 17:17:08 +01:00
```lua
-- A simple http client
2015-03-03 10:20:02 +01:00
conn=net.createConnection(net.TCP, 0)
2014-11-25 12:28:44 +01:00
conn:on("receive", function(conn, payload) print(payload) end )
2014-11-12 17:17:08 +01:00
conn:connect(80,"115.239.210.27")
conn:send("GET / HTTP/1.1\r\nHost: www.baidu.com\r\n"
.."Connection: keep-alive\r\nAccept: */* \r\n\r\n")
2014-11-07 05:59:19 +01:00
```
2014-10-13 14:24:52 +02:00
2015-01-26 16:50:40 +01:00
####Or a simple http server
2015-03-03 10:20:02 +01:00
2015-01-26 16:50:40 +01:00
```lua
-- A simple http server
2015-03-03 10:20:02 +01:00
srv=net.createServer(net.TCP)
srv:listen(80,function(conn)
conn:on("receive",function(conn,payload)
print(payload)
2015-01-26 16:50:40 +01:00
conn:send("< h1 > Hello, NodeMcu.< / h1 > ")
2015-03-03 10:20:02 +01:00
end)
2015-01-26 16:50:40 +01:00
conn:on("sent",function(conn) conn:close() end)
end)
```
2015-01-23 04:48:05 +01:00
####Connect to MQTT Broker
```lua
-- init mqtt client with keepalive timer 120sec
m = mqtt.Client("clientid", 120, "user", "password")
-- setup Last Will and Testament (optional)
2015-03-03 10:20:02 +01:00
-- Broker will publish a message with qos = 0, retain = 0, data = "offline"
2015-01-23 04:48:05 +01:00
-- to topic "/lwt" if client don't send keepalive packet
m:lwt("/lwt", "offline", 0, 0)
m:on("connect", function(con) print ("connected") end)
m:on("offline", function(con) print ("offline") end)
-- on publish message receive event
2015-03-03 10:20:02 +01:00
m:on("message", function(conn, topic, data)
print(topic .. ":" )
2015-01-23 04:48:05 +01:00
if data ~= nil then
print(data)
end
end)
-- for secure: m:connect("192.168.11.118", 1880, 1)
m:connect("192.168.11.118", 1880, 0, function(conn) print("connected") end)
-- subscribe topic with qos = 0
m:subscribe("/topic",0, function(conn) print("subscribe success") end)
2015-02-02 10:58:54 +01:00
-- or subscribe multiple topic (topic/0, qos = 0; topic/1, qos = 1; topic2 , qos = 2)
-- m:subscribe({["topic/0"]=0,["topic/1"]=1,topic2=2}, function(conn) print("subscribe success") end)
2015-01-23 04:48:05 +01:00
-- publish a message with data = hello, QoS = 0, retain = 0
m:publish("/topic","hello",0,0, function(conn) print("sent") end)
m:close();
-- you can call m:connect again
```
2015-01-26 16:50:40 +01:00
#### UDP client and server
2014-11-12 17:17:08 +01:00
```lua
2015-01-26 16:50:40 +01:00
-- a udp server
2015-03-03 10:20:02 +01:00
s=net.createServer(net.UDP)
2015-01-26 16:50:40 +01:00
s:on("receive",function(s,c) print(c) end)
s:listen(5683)
-- a udp client
2015-03-03 10:20:02 +01:00
cu=net.createConnection(net.UDP)
cu:on("receive",function(cu,c) print(c) end)
cu:connect(5683,"192.168.18.101")
2015-01-26 16:50:40 +01:00
cu:send("hello")
2014-11-12 17:17:08 +01:00
```
####Do something shining
```lua
2015-03-03 10:20:02 +01:00
function led(r,g,b)
pwm.setduty(1,r)
pwm.setduty(2,g)
pwm.setduty(3,b)
2014-11-12 17:17:08 +01:00
end
2015-03-03 10:20:02 +01:00
pwm.setup(1,500,512)
pwm.setup(2,500,512)
2014-12-19 09:56:07 +01:00
pwm.setup(3,500,512)
2015-03-03 10:20:02 +01:00
pwm.start(1)
pwm.start(2)
2014-12-19 09:56:07 +01:00
pwm.start(3)
2014-11-29 16:33:45 +01:00
led(512,0,0) -- red
led(0,0,512) -- blue
2014-11-12 17:17:08 +01:00
```
####And blink it
```lua
lighton=0
2014-12-09 12:07:06 +01:00
tmr.alarm(1,1000,1,function()
2015-03-03 10:20:02 +01:00
if lighton==0 then
lighton=1
led(512,512,512)
else
lighton=0
led(0,0,0)
end
2014-11-12 17:17:08 +01:00
end)
```
####If you want to run something when system started
```lua
--init.lua will be excuted
file.open("init.lua","w")
file.writeline([[print("Hello, do this at the beginning.")]])
file.close()
node.restart() -- this will restart the module.
```
2014-11-18 12:58:48 +01:00
####With below code, you can telnet to your esp8266 now
```lua
-- a simple telnet server
2015-03-03 10:20:02 +01:00
s=net.createServer(net.TCP,180)
s:listen(2323,function(c)
function s_output(str)
if(c~=nil)
then c:send(str)
end
end
2014-11-18 12:58:48 +01:00
node.output(s_output, 0) -- re-direct output to function s_ouput.
2015-03-03 10:20:02 +01:00
c:on("receive",function(c,l)
2014-11-18 12:58:48 +01:00
node.input(l) -- works like pcall(loadstring(l)) but support multiple separate line
2015-03-03 10:20:02 +01:00
end)
c:on("disconnection",function(c)
2014-11-18 12:58:48 +01:00
node.output(nil) -- un-regist the redirect output function, output goes to serial
2015-03-03 10:20:02 +01:00
end)
2014-11-22 14:24:51 +01:00
print("Welcome to NodeMcu world.")
2014-11-18 12:58:48 +01:00
end)
```
2014-12-09 07:41:14 +01:00
2014-12-09 07:45:37 +01:00
####Use DS18B20 module extends your esp8266
2014-12-09 07:41:14 +01:00
```lua
-- read temperature with DS18B20
2015-02-13 08:11:59 +01:00
node.compile("ds18b20.lua") -- run this only once to compile and save to "ds18b20.lc"
2014-12-09 07:41:14 +01:00
t=require("ds18b20")
t.setup(9)
addrs=t.addrs()
-- Total DS18B20 numbers, assume it is 2
print(table.getn(addrs))
-- The first DS18B20
print(t.read(addrs[1],t.C))
print(t.read(addrs[1],t.F))
print(t.read(addrs[1],t.K))
-- The second DS18B20
print(t.read(addrs[2],t.C))
print(t.read(addrs[2],t.F))
print(t.read(addrs[2],t.K))
-- Just read
print(t.read())
-- Just read as centigrade
print(t.read(nil,t.C))
-- Don't forget to release it after use
t = nil
2014-12-23 09:24:53 +01:00
ds18b20 = nil
2015-03-03 10:20:02 +01:00
package.loaded["ds18b20"]=nil
2014-12-09 07:41:14 +01:00
```
2015-02-05 00:42:00 +01:00
####Operate a display via I2c with u8glib
u8glib is a graphics library with support for many different displays.
The integration in nodemcu is developed for SSD1306 based display attached via the I2C port. Further display types and SPI connectivity will be added in the future.
2015-02-08 21:32:32 +01:00
U8glib v1.17
2015-02-05 00:42:00 +01:00
#####I2C connection
Hook up SDA and SCL to any free GPIOs. Eg. [lua_examples/graphics_test.lua ](https://github.com/devsaurus/nodemcu-firmware/blob/dev/lua_examples/graphics_test.lua ) expects SDA=5 (GPIO14) and SCL=6 (GPIO12). They are used to set up nodemcu's I2C driver before accessing the display:
```lua
sda = 5
scl = 6
i2c.setup(0, sda, scl, i2c.SLOW)
```
#####Library usage
The Lua bindings for this library closely follow u8glib's object oriented C++ API. Based on the u8g class, you create an object for your display type:
```lua
sla = 0x3c
disp = u8g.ssd1306_128x64_i2c(sla)
```
This object provides all of u8glib's methods to control the display.
2015-02-08 21:32:32 +01:00
Again, refer to [lua_examples/graphics_test.lua ](https://github.com/devsaurus/nodemcu-firmware/blob/dev/lua_examples/u8g_graphics_test.lua ) to get an impression how this is achieved with Lua code. Visit the [u8glib homepage ](https://code.google.com/p/u8glib/ ) for technical details.
2015-02-05 00:42:00 +01:00
#####Fonts
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/user_config.h ](https://github.com/devsaurus/nodemcu-firmware/blob/dev/app/include/user_config.h ) and recompile. Simply add the desired fonts to the font table:
```c
#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.
2015-02-08 21:32:32 +01:00
#####Unimplemented functions
- [ ] Cursor handling
- [ ] disableCursor()
- [ ] enableCursor()
- [ ] setCursorColor()
- [ ] setCursorFont()
- [ ] setCursorPos()
- [ ] setCursorStyle()
- [ ] Bitmaps
- [ ] drawBitmap()
- [ ] drawXBM()
- [ ] General functions
2015-02-08 21:41:02 +01:00
- [x] begin()
2015-02-08 21:32:32 +01:00
- [ ] print()
- [ ] setContrast()
- [ ] setPrintPos()
- [ ] setHardwareBackup()
- [ ] setRGB()
2015-02-10 21:40:29 +01:00
2015-02-05 19:04:09 +01:00
####Control a WS2812 based light strip
```lua
2015-03-03 10:20:02 +01:00
-- set the color of one LED on GPIO2 to red
ws2812.writergb(4, string.char(255, 0, 0))
-- set the color of 10 LEDs on GPIO0 to blue
ws2812.writergb(3, string.char(0, 0, 255):rep(10))
2015-02-05 19:04:09 +01:00
-- first LED green, second LED white
2015-03-03 10:20:02 +01:00
ws2812.writergb(4, string.char(0, 255, 0, 255, 255, 255))
2015-02-05 19:04:09 +01:00
```
2015-03-15 13:12:29 +01:00
####coap client and server
```lua
-- use copper addon for firefox
cs=coap.Server()
cs:listen(5683)
myvar=1
cs:var("myvar") -- get coap://192.168.18.103:5683/v1/v/myvar will return the value of myvar: 1
-- function should tack one string, return one string.
function myfun(payload)
print("myfun called")
respond = "hello"
return respond
end
cs:func("myfun") -- post coap://192.168.18.103:5683/v1/f/myfun will call myfun
cc = coap.Client()
cc:get(coap.CON, "coap://192.168.18.100:5683/.well-known/core")
cc:post(coap.NON, "coap://192.168.18.100:5683/", "Hello")
```