Commit b27fc6f5 authored by Nick Andrew's avatar Nick Andrew
Browse files

README: Move the simple programming examples up



The README is presently quite unreadable. The simpler examples of
programming an already-flashed ESP8266 are moved up, to give the
new user the flavor of what is possible with NodeMCU.

Instructions to build the firmware will follow.
Signed-off-by: default avatarNick Andrew <nick@nick-andrew.net>
parent de634a95
...@@ -34,105 +34,11 @@ Tencent QQ group: 309957875<br /> ...@@ -34,105 +34,11 @@ Tencent QQ group: 309957875<br />
- add coap module (done) - add coap module (done)
- cross compiler (done) - cross compiler (done)
##GPIO NEW TABLE ( Build 20141219 and later) # Programming Examples
<a id="new_gpio_map"></a> Because Lua is a high level language and several modules are built into the firmware, you can very easily program your ESP8266. Here are some examples!
<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 (SPI CS)</td>
</tr>
<tr>
<td>1</td><td>GPIO5</td><td>9</td><td>GPIO3 (UART RX)</td>
</tr>
<tr>
<td>2</td><td>GPIO4</td><td>10</td><td>GPIO1 (UART TX)</td>
</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 (SPI CLK)</td><td></td><td></td>
</tr>
<tr>
<td>6</td><td>GPIO12 (SPI MISO)</td><td></td><td></td>
</tr>
<tr>
<td>7</td><td>GPIO13 (SPI MOSI)</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.
#Build option
####file ./app/include/user_modules.h
```c
#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
#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_SPI
#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
#define LUA_USE_MODULES_MQTT
// #define LUA_USE_MODULES_COAP
#define LUA_USE_MODULES_U8G
#define LUA_USE_MODULES_WS2812
#define LUA_USE_MODULES_CJSON
#endif /* LUA_USE_MODULES */
```
#Online firmware custom build
For many application, some modules are not used, remove them can free many memory.<br /> ## Connect to your AP
Please try Marcel's [NodeMCU custom builds](http://frightanic.com/nodemcu-custom-build) cloud service and you can get your own firmware.<br />
#Flash the firmware
nodemcu_latest.bin: 0x00000<br />
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.
Or, if you build your own bin from source code.<br />
0x00000.bin: 0x00000<br />
0x10000.bin: 0x10000<br />
*Better run file.format() after flash*
#Connect the hardware in serial
baudrate:9600
#Write Lua script to filesystem
####Esplorer
Victor Brutskiy's [Esplorer](https://github.com/4refr0nt/ESPlorer) support most platforms such as Linux, Windows, Mac OS, etc. This software is opensource and can write lua/lc files to filesystem.
####NodeMCU Studio
[NodeMCU Studio](https://github.com/nodemcu/nodemcu-studio-csharp) is written in C# and support Windows. This software is opensource and can write lua files to filesystem.
#Start play
####Connect to your ap
```lua ```lua
ip = wifi.sta.getip() ip = wifi.sta.getip()
...@@ -145,7 +51,7 @@ Victor Brutskiy's [Esplorer](https://github.com/4refr0nt/ESPlorer) support most ...@@ -145,7 +51,7 @@ Victor Brutskiy's [Esplorer](https://github.com/4refr0nt/ESPlorer) support most
--192.168.18.110 --192.168.18.110
``` ```
####Manipulate hardware like a arduino ## Manipulate hardware like an Arduino
```lua ```lua
pin = 1 pin = 1
...@@ -154,7 +60,7 @@ Victor Brutskiy's [Esplorer](https://github.com/4refr0nt/ESPlorer) support most ...@@ -154,7 +60,7 @@ Victor Brutskiy's [Esplorer](https://github.com/4refr0nt/ESPlorer) support most
print(gpio.read(pin)) print(gpio.read(pin))
``` ```
####Write network application in nodejs style ## Write a network application in Node.js style
```lua ```lua
-- A simple http client -- A simple http client
...@@ -165,7 +71,7 @@ Victor Brutskiy's [Esplorer](https://github.com/4refr0nt/ESPlorer) support most ...@@ -165,7 +71,7 @@ Victor Brutskiy's [Esplorer](https://github.com/4refr0nt/ESPlorer) support most
.."Connection: keep-alive\r\nAccept: */*\r\n\r\n") .."Connection: keep-alive\r\nAccept: */*\r\n\r\n")
``` ```
####Or a simple http server ## Or a simple HTTP server
```lua ```lua
-- A simple http server -- A simple http server
...@@ -179,7 +85,7 @@ Victor Brutskiy's [Esplorer](https://github.com/4refr0nt/ESPlorer) support most ...@@ -179,7 +85,7 @@ Victor Brutskiy's [Esplorer](https://github.com/4refr0nt/ESPlorer) support most
end) end)
``` ```
####Connect to MQTT Broker ## Connect to MQTT broker
```lua ```lua
-- init mqtt client with keepalive timer 120sec -- init mqtt client with keepalive timer 120sec
...@@ -218,7 +124,8 @@ m:close(); -- if auto-reconnect == 1, will disable auto-reconnect and then disc ...@@ -218,7 +124,8 @@ m:close(); -- if auto-reconnect == 1, will disable auto-reconnect and then disc
``` ```
#### UDP client and server ## UDP client and server
```lua ```lua
-- a udp server -- a udp server
s=net.createServer(net.UDP) s=net.createServer(net.UDP)
...@@ -232,7 +139,8 @@ cu:connect(5683,"192.168.18.101") ...@@ -232,7 +139,8 @@ cu:connect(5683,"192.168.18.101")
cu:send("hello") cu:send("hello")
``` ```
####Do something shining ## Do something shiny with an RGB LED
```lua ```lua
function led(r,g,b) function led(r,g,b)
pwm.setduty(1,r) pwm.setduty(1,r)
...@@ -249,7 +157,8 @@ cu:send("hello") ...@@ -249,7 +157,8 @@ cu:send("hello")
led(0,0,512) -- blue led(0,0,512) -- blue
``` ```
####And blink it ## And blink it
```lua ```lua
lighton=0 lighton=0
tmr.alarm(1,1000,1,function() tmr.alarm(1,1000,1,function()
...@@ -263,7 +172,8 @@ cu:send("hello") ...@@ -263,7 +172,8 @@ cu:send("hello")
end) end)
``` ```
####If you want to run something when system started ## If you want to run something when the system boots
```lua ```lua
--init.lua will be excuted --init.lua will be excuted
file.open("init.lua","w") file.open("init.lua","w")
...@@ -272,7 +182,8 @@ cu:send("hello") ...@@ -272,7 +182,8 @@ cu:send("hello")
node.restart() -- this will restart the module. node.restart() -- this will restart the module.
``` ```
####With below code, you can telnet to your esp8266 now ## Add a simple telnet server to the Lua interpreter
```lua ```lua
-- a simple telnet server -- a simple telnet server
s=net.createServer(net.TCP,180) s=net.createServer(net.TCP,180)
...@@ -293,6 +204,103 @@ cu:send("hello") ...@@ -293,6 +204,103 @@ cu:send("hello")
end) end)
``` ```
# 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 (SPI CS)</td>
</tr>
<tr>
<td>1</td><td>GPIO5</td><td>9</td><td>GPIO3 (UART RX)</td>
</tr>
<tr>
<td>2</td><td>GPIO4</td><td>10</td><td>GPIO1 (UART TX)</td>
</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 (SPI CLK)</td><td></td><td></td>
</tr>
<tr>
<td>6</td><td>GPIO12 (SPI MISO)</td><td></td><td></td>
</tr>
<tr>
<td>7</td><td>GPIO13 (SPI MOSI)</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.
#Build option
####file ./app/include/user_modules.h
```c
#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
#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_SPI
#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
#define LUA_USE_MODULES_MQTT
// #define LUA_USE_MODULES_COAP
#define LUA_USE_MODULES_U8G
#define LUA_USE_MODULES_WS2812
#define LUA_USE_MODULES_CJSON
#endif /* LUA_USE_MODULES */
```
#Online firmware custom build
For many application, some modules are not used, remove them can free many memory.<br />
Please try Marcel's [NodeMCU custom builds](http://frightanic.com/nodemcu-custom-build) cloud service and you can get your own firmware.<br />
#Flash the firmware
nodemcu_latest.bin: 0x00000<br />
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.
Or, if you build your own bin from source code.<br />
0x00000.bin: 0x00000<br />
0x10000.bin: 0x10000<br />
*Better run file.format() after flash*
#Connect the hardware in serial
baudrate:9600
#Write Lua script to filesystem
####Esplorer
Victor Brutskiy's [Esplorer](https://github.com/4refr0nt/ESPlorer) support most platforms such as Linux, Windows, Mac OS, etc. This software is opensource and can write lua/lc files to filesystem.
####NodeMCU Studio
[NodeMCU Studio](https://github.com/nodemcu/nodemcu-studio-csharp) is written in C# and support Windows. This software is opensource and can write lua files to filesystem.
####Use DS18B20 module extends your esp8266 ####Use DS18B20 module extends your esp8266
```lua ```lua
-- read temperature with DS18B20 -- read temperature with DS18B20
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment