Sie sind auf Seite 1von 30

Development

of NodeMCU

Table of Contents
1. introduction
2. Section 1: Development Process for DoitCar
3. Section 2: Burn the firmware
4. Section 3: Program Debug
i. Subsection 3.1: Hellow World
ii. Subsection 3.2: GPIO Test
iii. Subsection 3.3: System Command Test
iv. Subsection 3.4: WiFi Test
v. Subsection 3.5: File Operation
vi. Subsection 3.6: Program Edit
5. Section 4: Example for Code
i. Subsection 4.1: init.lua
ii. Subsection 4.2: WiFi Mode
iii. Subsection 4.3: PWM Function
6. Section 5: Summary
7. Section 6: Technical Support
8. How to Get it

2
Development of NodeMCU

Introduction
This document presents the development process for DoitCar.

introduction 3
Development of NodeMCU

# Section 1: Development Process for


DoitCar

When you get the DoitCar, can develop it by the following steps

Step 1) Hardware: one computer, one piece of MicroUSB cable (not MiniUSB);

Step 2) Software:

(a) DoitRobo is designed based on NodeMCU. The newest version of NodeMCU firmware can be downloaded from
https://github.com/nodemcu/nodemcu-firmware/releases.

Tips: firmware can be devided Float version and Integer version. To minus the power consume as soos as possible,
Interger version is generally used.

(b) Firmware download tools: Two types: nodemcu_flasher32bit.exe/nodemcu_flasher64bit.exe, can be chosen by the
system Win32 or Win64. The newest version is https://github.com/nodemcu/nodemcu-flasher.

(c) Debug tool: The debug tool is LuaLoader. The newest version is downloaded from
https://github.com/GeoNomad/LuaLoader. or http://benlo.com/esp8266/index.html#LuaLoader.

(d) other tools:internet debug tool, serial tool, ect.

Section 1: Development Process for DoitCar 4


Development of NodeMCU

Step 3): Burnthe firmware by Section 2;

Step 4): Debug by Section 3;

Step 5): Program the Lua file. Then, download and debug.

Section 1: Development Process for DoitCar 5


Development of NodeMCU

Section 2: Burn the firmware 6


Development of NodeMCU

Section 3: Program Debug


The program debug tool can provide the inter-active based on serial, which includes the basic command, and the download
function for application program. Certainly, there are many other applicable open-source tools, such as, LuaLoader,
NodeMCUStudioIDE, Decoda, etc. In this manual, LuaLoader is used to show the debug steps.

Tips: the download address for NodeMCUStudioIDE:

https://github.com/nodemcu/nodemcu-studio-csharp

the download site for Decoda:

https://github.com/unknownworlds/decoda/releases

Section 3: Program Debug 7


Development of NodeMCU

Subsection 3.1: Hellow World


Close the USB connection from NodeMCU, then run Lualoader, shown as follows.

In the "Setting" meanu, choose "Comm Port Settings", then pop up "Serial Advanced Setting", set the serial number in the

"Port", other settings are defualt values.

Insert USB NodeMCU cable, and then choose "Connect" to complete the connection.

Subsection 3.1: Hellow World 8


Development of NodeMCU

There is a edit block in Lualoader, which can used to send commands to connect the NodeMCU. For example.

We will have the following results.

print("Hello World!")
Hello World!

The first Lua code is successfully run. Is it very simple?

Tips: during the debug, may need to check the API function for NodeMCU with the following sites.

http://www.nodemcu.com/docs/node-module/

https://github.com/nodemcu/nodemcu-firmware/wiki/nodemcu_api_cn

Subsection 3.1: Hellow World 9


Development of NodeMCU

Subsection 3.2: GPIO Test


After LuaLoader connects NodeMCU, can use the shortcuts to get the NodeMCU opration for the LuaLoader.

From NodeMCU API, we know that the GPIO16 is mapped to the 0th IO port in the ESP8266. That is, the operation for the
oth IO port in Lua is the same as the operation of GPIO16 in ESP8266. When use "GPIO" in LuaLoader, can complete the
GPIO test.

For example, if set the 0th IO port as output, only choose "0 GPIO16", "Output", then click "Set". Click "0" or "1" can set the
port as low voltage level or high voltage level. The output code is "gpio.write(0,gpio.LOW)" or "gpio.write(0,gpio.HIGH)" .

Tips: since the 0th port is connected the LED on board, if the 0th port is set as output mode and low voltage level, LED is

lightened, or put out.

gpio.mode(0,gpio.OUTPUT)
> gpio.write(0,gpio.LOW)
> gpio.write(0,gpio.HIGH)
>

To test the input function of the 1st port, a dupont cable is used to connect the 1st port and GND. Then, choose "1 GPIO5",
"Input", "Pullup", and click "Set" to complete the input-settings of this port. Click "Read" would run a read operation, and
return "0". In addition, if the dupont cable is connected the 1st port and 3v3 port, "1" would be returned by using "Read".
The timer can read the information periodically.

Subsection 3.2: GPIO Test 10


Development of NodeMCU

gpio.mode(1,gpio.INPUT,gpio.PULLUP)
> = gpio.read(1)
1
> = gpio.read(1)
0
>

Tipsif let the mouse on the LuaLoader button, would display the funtion description. In addition, if there are too many
characters in the main display, and affect the read effect, you can use "Clear" to clear the screen.

Subsection 3.2: GPIO Test 11


Development of NodeMCU

Subsection 3.3: System Command Test


LuaLoader provides many usual NodeMCU system commands. For example, read the left memory capacity "Heap" button;
read the ID of ESP8266 chip "chipID" button; software restart command button "Restart", and stop timer "tmr.stop" button,
where, by the left click "tmr.stop" can choose the timer number to stop.

If NodeMCU restart, there is error characters. This is because the baud is74880bps, not the default baud 9600bps in the
NodeMCU.

"lua:cannot open init.lua" shows that init.lua file cannot be found when the firmware starts. This is because init.lua is used
as the application entrance for NodeMCU.

Subsection 3.3: System Command Test 12


Development of NodeMCU

Subsection 3.4: WiFi Test


WiFi in NodeMCU has three modes: AP(Access Point), STA(STATION), and AP+STA. In the AP mode, NodeMCU is used
as a sender to send WiFi signal, and can accept the wireless connection from other nodes. At this time, NodeMCU can be
viewed as a wireless router. In STA mode, NodeMCU is used as a node, and can connect to other AP (e.g., smartphone
and/or wireless router).

LuaLoader can test the functions of NodeMCU at STA mode. About the test of AP and AP+STA modes, please read the
NodeMCU documents. In the next section, we would present the example source code.

"SSID" and "Password" edit blocks are used to input the SSID name and password of wireless router.

"Survery"is used to search the SSID in the environment of NodeMCU,and print. The function is similar to the following code.

wifi.setmode(wifi.STATION)
wifi.sta.getap(function(t)
if t then print("\n\nVisible Access Points:\n")
for k,v in pairs(t) do l = string.format("%-10s",k) print(l.." "..v) end
else
print("Try again")
end
end)

The result is listed as follows.

Subsection 3.4: WiFi Test 13


Development of NodeMCU

"Set AP" is that using SSID and Password to connect the wireless router.

Subsection 3.4: WiFi Test 14


Development of NodeMCU

Subsection 3.5: File Operation


NodeMCU uses files to save Lua code because of the illustrative feature of Lua. Lua source program can be compiled and
run online. Therefore, file operation is very important.

NodeMCU can support many types of file operations, like upload, delete, compile to binary file, run, etc. Moreover,
LuaLoader also provides the richful file operations.

(1) "Upload File"upload the prepaired Lua source code into NodeMCU. For example, upload init.lua is shown as folows.

print("\n")
print("NodeMCU Started")
print("Type something to start")

click "Upload" to complete the upload function.

Tips:

The uploaded file can be shown in the below of "Upload File". The button " " beside "Upload File" is used to open the Lua
file and edit it.

(2) The button "cat" is used to check and read the file.

For example, if the following code is downloaded into the NodeMCU.

print("\n\ncat init.lua\n")
file.open( "init.lua", "r")
while true
do
line = file.readline()
if (line == nil) then break
end
print(string.sub(line, 1, -2))
tmr.wdclr()
end
file.close()

where tmr.wdclr() is used to clear the watchdog to avoid to trig it. The results is as follows.

cat init.lua
print("\n")
print("NodeMCU Started")
print("Type something to start")

Subsection 3.5: File Operation 15


Development of NodeMCU

Tipsas for a long-time cycle or traffic, tmr.wdclr() is called to clear the watchdog counter, and to avoid overflow and
restart.

(3) run the files

Click the button "dofile" to run this file, and output the result, similar to dofile("init.lua").

dofile(init.lua) 19, Feb.,2015 16:07:24


dofile("init.lua")
NodeMCU Started
Type something to start

(4) file compile

click "compile" button to compile, similar to node.compile(). After compile, "file.lc" is generate automatically.

node.compile("init.lua")

However, if the "print("NodeMCU Started")" in init.lua is changed into "print("NodeMCU Started"", then an error would
happen with an error prompt.

node.compile("init.lua")

stdin:1: init.lua:3: ')' expected (to close '(' at line 2)

near 'print'

Tips: the file format of Lua binary is Lua.lc. If the source code of .lua is changed into binary file, the program efficiency
would be improved, and can reduce the memory usage. So, .lc file is suggested to used in the program.

(5) run lc file

click "dolc" to run the lc file, and output the results. it is similar to dofile("init.lc"). The result is the same as that of
dofile("init.lc").

(6) file list

click "file.list", which is similar to "for k,v in pairs(file.list()) do l = string.format("%-15s",k) print(l.." "..v.." bytes") end".

The results is as follows.

init.lua 70 bytes

init.lc 160 bytes

(7) delete file

click "remove", which is similar to the "file.remove("file.lua")".

For more details about LuaLoader, please visit: http://benlo.com/esp8266/index.html#LuaLoader.

(8) format file system

click "Format" button to format the file system, which is similar to the

file.format("file.lua").

Subsection 3.5: File Operation 16


Development of NodeMCU

file.format()
format done.

Subsection 3.5: File Operation 17


Development of NodeMCU

Subsection 3.6: Program Edit


Lua program can be edited directly by notebook, and/or use the special IDE. In this section, LuaStudio is used, as shown in
the following Figure.

The project manage mode is used in LuaStduio source code. In "Project" meanu can new and/or add the current project.

In the left side, the option "solution" can view the current project. On the poject name, can manage the source file by
clicking right button, e.g., new, add, delete, and rename.

When edit lua file, LuaStudio can highlight the keywords with different colors by the different data types, together with
prompt. It is very convenient to DIY.

After editing lua file, then the one lua file can be download into NodeMCU to run.

Tips: If there is a BUG in the Lua program, after download to NodeMCU, there are some problems; for example, cannot
download by serial port, and/or cannot run the serial commands normally (e.g., file.formart() cannot download to
NodeMCU). At this time, the firmware must be burnt again.

Subsection 3.6: Program Edit 18


Development of NodeMCU

Section 4: Example for Code

Section 4: Example for Code 19


Development of NodeMCU

Subsection 4.1: init.lua


init.lua can be used as the applicaiton entrance for NodeMCU. If no, can ignore; if has, then run it. Therefore, some codes
can be put here to run automatically after power.

1 print("\n")
2 print("ESP8266 Started")
3
4 local luaFile = {"fileName.lua"}
5 for i, f in ipairs(luaFile) do
6 if file.open(f) then
7 file.close()
8 print("Compile File:"..f)
9 node.compile(f)
10 print("Remove File:"..f)
11 file.remove(f)
12 end
13 end
14
15 luaFile = nil
16 collectgarbage()
17
18 dofile("fileName.lc");

line 1-2: print the character information; line 4: define the lua file, where "fileName" must be revised by different file. If many
files need to be compiled, can be added in the later, such as, local luaFile = {"fileName1.lua","fileName2.lua"};

line 5: for cycle is used to complete the file operations for many files;

line 6: judge whether the file exists, if exists, then run, or ignore it;

line 7: close the opened files;

line 8-11: compete the complie, and generate "filename.lc" automatically;

line 15-16: release memory;

line 17: run the completed binary file.

Tips: In lua, the variable type is default set as global. If the variabe is only used in this file, a key word "local" must be
added. After that, the value is set as nil, and use collectgarbage() to show the release memory.

When downloading and restart NodeMCU, if a warm on memory not enough happens, shown in the following.

lua: init.lua:8: not enough memory

This is because memory consumption is big when Lua just starts, and it would normal after start is finished. Therefore,
compile should be done after a while for the start.

Subsection 4.1: init.lua 20


Development of NodeMCU

Subsection 4.2: WiFi Mode

WiFi Mode
As above metioned, NodeMCU has three types of WiFi modes: AP (Access Point), STA (Station), and AP+STA. The code is
listed as follows.

AP mode
file name is ap.lua in the documents.

the "fileName.lua" in "init.lua" is revised as "ap.lua", "fileName.lc" is revised as "ap.lc", and then download to NodeMCU.

1 print("Ready to start soft ap")


2
3 local str=wifi.ap.getmac();
4 local ssidTemp=string.format("%s%s%s",string.sub(str,10,11),string.sub(str,13,14),string.sub(str,16,17));
5
6 cfg={}
7 cfg.ssid="ESP8266_"..ssidTemp;
8 cfg.pwd="12345678"
9 wifi.ap.config(cfg)
10
11 cfg={}
12 cfg.ip="192.168.1.1";
13 cfg.netmask="255.255.255.0";
14 cfg.gateway="192.168.1.1";
15 wifi.ap.setip(cfg);
16 wifi.setmode(wifi.SOFTAP)
17
18 str=nil;
19 ssidTemp=nil;
20 collectgarbage();
21
22 print("Soft AP started")
23 print("Heep:(bytes)"..node.heap());
24 print("MAC:"..wifi.ap.getmac().."\r\nIP:"..wifi.ap.getip());

line 1: print the character;

line 3-4: get MAC address with AP mode, and the SSID from 6bits at the end of MAC. Certainly, you can use others name
as ID as well. For example, node.chipid() can get the ID of ESP8266;

line 6-9: set SSID under AP mode. The format of SSID is "ESP8266_XXXXXX", where "XXXXXX" is the 6bits of the end of
MAC address;

line 11-15: set the IP address, subnet mask, and gateway address for the module;

line 16: call wifi.setmode to run;

line 23: print the current memory.

Download this program, after run, the wireless devices can search the AP signal from NodeMCU. The following Figure is

Subsection 4.2: WiFi Mode 21


Development of NodeMCU

shown the connection with computer.

Log after run

1 NodeMCU 0.9.6 build 20150406 powered by Lua 5.1.4


2
3
4 ESP8266 Started
5 Compile File:ap.lua
6 Remove File:ap.lua
7 Readly to start soft ap
8 Soft AP started
9 Heep:(bytes)15328
10 MAC:1A-FE-34-A1-14-A7
11 IP:192.168.1.1
12 >

Subsection 4.2: WiFi Mode 22


Development of NodeMCU

From the above log, ap.lua is deleted after compile.

If restart NodeMCU, the log is as follows.

1 NodeMCU 0.9.6 build 20150406 powered by Lua 5.1.4


2
3
4 ESP8266 Started
5 Ready to start soft ap
6 Soft AP started
7 Heep:(bytes)15048
8 MAC:1A-FE-34-A1-14-A7
9 IP:192.168.1.1
10 >

For more information, please visit https://github.com/nodemcu/nodemcu-firmware/wiki/nodemcu_api_cn.

STA Mode
The file name is "sta.lua".

The "fileName.lua" in init.lu is revised into "sta.lua", and "fileName.lc" is revised into "sta.lc". After revised, can download
into the NodeMCU. The code is as follows.

1 print("Ready to Set up wifi mode")


2 wifi.setmode(wifi.STATION)
3
4 wifi.sta.config("MERCURY_1013","123456789")
5 wifi.sta.connect()
6 local cnt = 0
7 tmr.alarm(3, 1000, 1, function()
8 if (wifi.sta.getip() == nil) and (cnt < 20) then
9 print("Trying Connect to Router, Waiting...")
10 cnt = cnt + 1
11 else
12 tmr.stop(3)
13 if (cnt < 20) then print("Config done, IP is "..wifi.sta.getip())
14 else print("Wifi setup time more than 20s, Please verify wifi.sta.config() function. Then re-download the file.")
15 end
16 cnt = nil;
17 collectgarbage();
18 end
19 end)

line 1: print the character;

line 2: set WiFi as STA mode;

line 4: set the wireless router SSID and password under the STA mode;

line 5: send a connection;

line 6-19: start the 3rd timer, check the connection after each 1000ms. If don't connect the wireless router within 20s, there
is a connection failure. If successfully connect the wireless router within 20ms, the 3rd timer stops, and print the IP address.
The log is as follows.

1 NodeMCU 0.9.6 build 20150406 powered by Lua 5.1.4

Subsection 4.2: WiFi Mode 23


Development of NodeMCU

2
3
4 ESP8266 Started
5 Compile File:sta.lua
6 Remove File:sta.lua
7 Ready to Set up wifi mode
8 > Trying Connect to Router, Waiting...
9 Trying Connect to Router, Waiting...
10 Config done, IP is 192.168.1.100

It shows that 192.168.1.100 is gotten by NodeMCU.

AP+STA Mode
The file name is "apsta.lua".

In the file "init.lua", "fileName.lua: is revised as "apsta.lua", "fileName.lc" is revised as "apsta.lc". After revised, can
download into NodeMCU. The code is as follows.

1 print("Ready to start soft ap AND station")


2 local str=wifi.ap.getmac();
3 local ssidTemp=string.format("%s%s%s",string.sub(str,10,11),string.sub(str,13,14),string.sub(str,16,17));
4 wifi.setmode(wifi.STATIONAP)
5
6 local cfg={}
7 cfg.ssid="ESP8266_"..ssidTemp;
8 cfg.pwd="12345678"
9 wifi.ap.config(cfg)
10 cfg={}
11 cfg.ip="192.168.2.1";
12 cfg.netmask="255.255.255.0";
13 cfg.gateway="192.168.2.1";
14 wifi.ap.setip(cfg);
15
16 wifi.sta.config("MERCURY_1013","123456789")
17 wifi.sta.connect()
18
19 local cnt = 0
20 gpio.mode(0,gpio.OUTPUT);
21 tmr.alarm(0, 1000, 1, function()
22 if (wifi.sta.getip() == nil) and (cnt < 20) then
23 print("Trying Connect to Router, Waiting...")
24 cnt = cnt + 1
25 if cnt%2==1 then gpio.write(0,gpio.LOW);
26 else gpio.write(0,gpio.HIGH); end
27 else
28 tmr.stop(0);
29 print("Soft AP started")
30 print("Heep:(bytes)"..node.heap());
31 print("MAC:"..wifi.ap.getmac().."\r\nIP:"..wifi.ap.getip());
32 if (cnt < 20) then print("Conected to Router\r\nMAC:"..wifi.sta.getmac().."\r\nIP:"..wifi.sta.getip())
33 else print("Conected to Router Timeout")
34 End
35 gpio.write(0,gpio.LOW);
36 cnt = nil;cfg=nil;str=nil;ssidTemp=nil;
37 collectgarbage()
38 end
39 end)

line 1: print the character;

line 2-14: set AP;

line 16-17: set STA parameters;

Subsection 4.2: WiFi Mode 24


Development of NodeMCU

line 19-39: set timer 0 to check whether it is connected to the wireless router after each 1s;

line 20: set D0 (i.e., GPIO16) as output. Revise the port to connect the LED light on NodeMCU development board.

line 25-26: shows the state of WiFi connection state.

The log is as follows.

1 NodeMCU 0.9.6 build 20150406 powered by Lua 5.1.4


2
3
4 ESP8266 Started
5 Compile File:apsta.lua
6 Remove File:apsta.lua
7 Ready to start soft ap AND station
8 > Trying Connect to Router, Waiting...
9 Trying Connect to Router, Waiting...
10 Trying Connect to Router, Waiting...
11 Soft AP started
12 Heep:(bytes)16192
13 MAC:1A-FE-34-A1-14-A7
14 IP:192.168.2.1
15 Conected to Router
16 MAC:18-FE-34-A1-14-A7
17 IP:192.168.1.100

Subsection 4.2: WiFi Mode 25


Development of NodeMCU

Subsection 4.3: PWM Function


The file name is "pwm.lua".

In NodeMCU, D1~12 pins have the PWM function (excluding D0). PWM frequency can cover 1~1000Hz, the cycle scope is
0~1023(i.e., 0%~100%).

In "init.lua", the "fileName.lua" is revised as "pwm.lua", and "fileName.lc" is revised as "pwm.lc". After revision, and then
download it. The code for PWM is as follows.

1 print("PWM Function test")


2 pwm.setup(1,1000,1023);
3 pwm.start(1);
4
5 local r=512;
6 local flag=1;
7 tmr.alarm(2,100,1,function()
8 pwm.setduty(1,r);
9 if flag==1 then
10 r=r-50; if r0 then flag=0 r=0 end
11 else
12 r= r+50; if r>1023 then flag=1 r=1023 end
13 end
14 end)

line 1: print character;

line 2: set the 1st port as 1000Hz, the cycle is 1023;

line 3: start the PWM output;

line 5-14: set timer2, add or reduce cycle "r" after each 100ms; The variable flag can remember the change.

Tips: when download, can see the change of the ouput volage from port D1 by using multimeter and/or oscilloscope.
Another way, if a LED is inserted between D1 and GND. It can see the change of brightness.

Subsection 4.3: PWM Function 26


Development of NodeMCU

Tipsfor LED, the longer pin is positive and connected to D1; and the short pin is negative, and connect to GND. The log is
as follows.

1 NodeMCU 0.9.6 build 20150406 powered by Lua 5.1.4


2
3
4 ESP8266 Started
5 Compile File:pwm.lua
6 Remove File:pwm.lua
7 PWM Function test
8 >

Subsection 4.3: PWM Function 27


Development of NodeMCU

Section 5: Summary
(1) ESP8266 official site:

http://espressif.com/zh-hans/

(2) Manual for Nodemcu

https://github.com/nodemcu

API for NodeMCU:

https://github.com/nodemcu/nodemcu-firmware

(3) Lua

http://www.lua.org/

http://baike.baidu.com/view/416116.htm

http://zh.wikipedia.org/wiki/Lua

(4) eLua:

http://www.eluaproject.net/

(5) LuaLoader

http://benlo.com/esp8266/index.html#LuaLoader

https://github.com/GeoNomad/LuaLoader

Section 5: Summary 28
Development of NodeMCU

Section 6: Technical Support


For more information about our products, please visit http://www.doit.am.

Contact Information:

Company Shenzhen Doctors of Intelligence & Technology (SZDOIT)

Tel +86-158 9988 0115

skype yichone

Emails support@doit.am;yichoneyi@163.com

Section 6: Technical Support 29


Development of NodeMCU

How to Get it
Here, you can get this NodeMCU: http://www.smartarduino.com/nodemcu-based-on-esp-12e-from-esp8266_p94571.html

How to Get it 30

Das könnte Ihnen auch gefallen