2014-11-12 17:17:08 +01:00
|
|
|
# **NodeMcu** #
|
2014-12-22 12:35:05 +01:00
|
|
|
version 0.9.4
|
2014-11-12 17:17:08 +01:00
|
|
|
###A lua based firmware for wifi-soc esp8266
|
2014-12-22 12:37:49 +01:00
|
|
|
Build on [ESP8266 sdk 0.9.4](http://bbs.espressif.com/viewtopic.php?f=5&t=90)<br />
|
|
|
|
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
|
|
|
|
2014-12-22 12:37:49 +01:00
|
|
|
wiki: [nodemcu wiki](https://github.com/nodemcu/nodemcu-firmware/wiki)<br />
|
|
|
|
home: [nodemcu.com](http://www.nodemcu.com)<br />
|
|
|
|
bbs: [中文论坛Chinese bbs](http://bbs.nodemcu.com)<br />
|
|
|
|
Tencent QQ group QQ群: 309957875<br />
|
2014-12-22 12:35:05 +01:00
|
|
|
|
2014-11-17 18:27:46 +01:00
|
|
|
# Change log
|
2014-12-30 13:00:50 +01:00
|
|
|
2014-12-30<br />
|
|
|
|
modify uart.on api, when run_input set to 0, uart.on now can read raw data from uart.<br />
|
2014-12-30 13:17:16 +01:00
|
|
|
serial input now accept non-ascii chars.<br />
|
|
|
|
fix dev-kit gpio map.<br />
|
|
|
|
add setip, setmac, sleeptype api to wifi module. <br />
|
|
|
|
add tmr.time() api to get rtc time and calibration.
|
2014-12-08 13:30:00 +01:00
|
|
|
|
2014-11-28 18:45:54 +01:00
|
|
|
[more change log](https://github.com/nodemcu/nodemcu-firmware/wiki/nodemcu_api_en#change_log)<br />
|
|
|
|
[更多变更日志](https://github.com/nodemcu/nodemcu-firmware/wiki/nodemcu_api_cn#change_log)
|
2014-11-07 05:59:19 +01:00
|
|
|
# Summary
|
|
|
|
- Easy to access wireless router
|
2014-11-12 17:17:08 +01:00
|
|
|
- Based on Lua 5.1.4
|
|
|
|
- Event-Drive programming preferred.
|
2014-12-07 05:21:24 +01:00
|
|
|
- Build-in file, timer, pwm, i2c, 1-wire, net, gpio, wifi, adc, uart and system api.
|
2014-11-12 17:17:08 +01:00
|
|
|
- GPIO pin re-mapped, use the index to access gpio, i2c, pwm.
|
2014-11-10 14:00:17 +01:00
|
|
|
- GPIO Map Table:
|
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>
|
|
|
|
<td>7</td><td>GPIO13</td<td></td><td></td>
|
|
|
|
</tr>
|
|
|
|
</table>
|
|
|
|
#### [*] D0(GPIO16) can only be used as gpio read/write. no interrupt supported. no pwm/i2c/ow supported.
|
|
|
|
|
|
|
|
##GPIO OLD TABLE (Before build 20141212)
|
|
|
|
|
|
|
|
<a id="old_gpio_map"></a>
|
2014-11-07 05:59:19 +01:00
|
|
|
<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>GPIO12</td><td>8</td><td>GPIO0</td>
|
|
|
|
</tr>
|
|
|
|
<tr>
|
|
|
|
<td>1</td><td>GPIO13</td><td>9</td><td>GPIO2</td>
|
|
|
|
</tr>
|
|
|
|
<tr>
|
|
|
|
<td>2</td><td>GPIO14</td><td>10</td><td>GPIO4</td>
|
|
|
|
</tr>
|
|
|
|
<tr>
|
|
|
|
<td>3</td><td>GPIO15</td><td>11</td><td>GPIO5</td>
|
|
|
|
</tr>
|
|
|
|
<tr>
|
|
|
|
<td>4</td><td>GPIO3</td><td></td><td></td>
|
|
|
|
</tr>
|
|
|
|
<tr>
|
|
|
|
<td>5</td><td>GPIO1</td><td></td><td></td>
|
|
|
|
</tr>
|
|
|
|
<tr>
|
|
|
|
<td>6</td><td>GPIO9</td><td></td><td></td>
|
|
|
|
</tr>
|
|
|
|
<tr>
|
|
|
|
<td>7</td><td>GPIO10</td<td></td><td></td>
|
|
|
|
</tr>
|
|
|
|
</table>
|
|
|
|
|
2014-12-22 12:35:05 +01:00
|
|
|
#Build option
|
2014-12-23 04:52:09 +01:00
|
|
|
####*GNU toolchain is not tested*
|
2014-12-22 12:35:05 +01:00
|
|
|
####file ./app/include/user_config.h
|
|
|
|
```c
|
|
|
|
#define FLASH_512K
|
|
|
|
// #define FLASH_1M
|
|
|
|
// #define FLASH_2M
|
|
|
|
// #define FLASH_4M
|
|
|
|
...
|
|
|
|
#define LUA_USE_MODULES
|
|
|
|
#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
|
|
|
|
#define LUA_USE_MODULES_TMR
|
|
|
|
#define LUA_USE_MODULES_ADC
|
|
|
|
#define LUA_USE_MODULES_UART
|
|
|
|
#define LUA_USE_MODULES_OW
|
|
|
|
//#define LUA_USE_MODULES_BIT
|
|
|
|
#endif /* LUA_USE_MODULES */
|
|
|
|
```
|
2014-11-07 05:59:19 +01:00
|
|
|
|
2014-11-12 17:17:08 +01:00
|
|
|
#Flash the firmware
|
|
|
|
nodemcu_512k.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 />
|
|
|
|
eagle.app.v6.flash.bin: 0x00000<br />
|
|
|
|
eagle.app.v6.irom0text.bin: 0x10000<br />
|
|
|
|
esp_init_data_default.bin: 0x7c000<br />
|
|
|
|
blank.bin: 0x7e000<br />
|
|
|
|
|
|
|
|
|
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
|
|
|
|
print(wifi.sta.getip())
|
|
|
|
--0.0.0.0
|
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")
|
2014-11-07 05:59:19 +01:00
|
|
|
print(wifi.sta.getip())
|
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
|
|
|
|
|
|
|
|
```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
|
|
|
|
|
|
|
|
```lua
|
|
|
|
-- A simple http client
|
2014-11-20 08:45:21 +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
|
|
|
|
2014-11-12 17:17:08 +01:00
|
|
|
####Or a simple http server
|
|
|
|
|
|
|
|
```lua
|
|
|
|
-- A simple http server
|
|
|
|
srv=net.createServer(net.TCP)
|
|
|
|
srv:listen(80,function(conn)
|
|
|
|
conn:on("receive",function(conn,payload)
|
|
|
|
print(payload)
|
|
|
|
conn:send("<h1> Hello, NodeMcu.</h1>")
|
|
|
|
end)
|
2014-11-22 14:24:51 +01:00
|
|
|
conn:on("sent",function(conn) conn:close() end)
|
2014-11-12 17:17:08 +01:00
|
|
|
end)
|
|
|
|
```
|
|
|
|
|
|
|
|
####Do something shining
|
|
|
|
```lua
|
|
|
|
function led(r,g,b)
|
2014-12-19 09:56:07 +01:00
|
|
|
pwm.setduty(1,r)
|
|
|
|
pwm.setduty(2,g)
|
|
|
|
pwm.setduty(3,b)
|
2014-11-12 17:17:08 +01:00
|
|
|
end
|
2014-11-29 16:33:45 +01:00
|
|
|
pwm.setup(1,500,512)
|
2014-12-19 09:56:07 +01:00
|
|
|
pwm.setup(2,500,512)
|
|
|
|
pwm.setup(3,500,512)
|
2014-11-12 17:17:08 +01:00
|
|
|
pwm.start(1)
|
2014-12-19 09:56:07 +01:00
|
|
|
pwm.start(2)
|
|
|
|
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()
|
2014-11-12 17:17:08 +01:00
|
|
|
if lighton==0 then
|
|
|
|
lighton=1
|
2014-11-29 16:33:45 +01:00
|
|
|
led(512,512,512)
|
2014-11-12 17:17:08 +01:00
|
|
|
else
|
|
|
|
lighton=0
|
|
|
|
led(0,0,0)
|
|
|
|
end
|
|
|
|
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
|
2014-11-22 14:24:51 +01:00
|
|
|
s=net.createServer(net.TCP,180)
|
2014-11-18 12:58:48 +01:00
|
|
|
s:listen(2323,function(c)
|
|
|
|
function s_output(str)
|
2014-11-22 14:24:51 +01:00
|
|
|
if(c~=nil)
|
|
|
|
then c:send(str)
|
2014-11-18 12:58:48 +01:00
|
|
|
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)
|
|
|
|
node.output(nil) -- un-regist the redirect output function, output goes to serial
|
|
|
|
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
|
|
|
|
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
|
2014-12-09 07:41:14 +01:00
|
|
|
package.loaded["ds18b20"]=nil
|
|
|
|
```
|