Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
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
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.
Step 5): Program the Lua file. Then, download and debug.
https://github.com/nodemcu/nodemcu-studio-csharp
https://github.com/unknownworlds/decoda/releases
In the "Setting" meanu, choose "Comm Port Settings", then pop up "Serial Advanced Setting", set the serial number in the
Insert USB NodeMCU cable, and then choose "Connect" to complete the connection.
There is a edit block in Lualoader, which can used to send commands to connect the NodeMCU. For example.
print("Hello World!")
Hello World!
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
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
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.
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.
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.
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)
"Set AP" is that using SSID and Password to connect the wireless router.
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")
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.
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")
Tipsas for a long-time cycle or traffic, tmr.wdclr() is called to clear the watchdog counter, and to avoid overflow and
restart.
Click the button "dofile" to run this file, and output the result, similar to dofile("init.lua").
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")
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.
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").
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".
init.lua 70 bytes
click "Format" button to format the file system, which is similar to the
file.format("file.lua").
file.format()
format done.
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.
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;
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.
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.
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.
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;
Download this program, after run, the wireless devices can search the AP signal from NodeMCU. The following Figure is
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.
line 4: set the wireless router SSID and password under the STA mode;
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.
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
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.
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.
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.
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.
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.
Section 5: Summary
(1) ESP8266 official site:
http://espressif.com/zh-hans/
https://github.com/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
Contact Information:
skype yichone
Emails support@doit.am;yichoneyi@163.com
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