NodeMCU Learning (2)-Introduction and Use of NodeMCU (1)

Original: NodeMCU (ESP8266) Development Notes

1. Introduction to NodeMCU

NodeMCU is an open source development platform for the Internet of Things. Its firmware and development board are both open source and come with a WIFI module. Based on this platform, IoT applications can be developed with a few lines of simple Lua scripts.

Its main features are as follows:

  • Operate the hardware IO like Arduino, and provide high-level hardware interfaces, which can free application developers from complicated hardware configuration and register operations. Use interactive Lua scripts to write hardware code like Arduino!

  • Using Nodejs similar syntax to write network application event-driven API greatly facilitates users to develop network applications. Use a Nodejs-like way to write network code and run on a 5mm*5mm MCU to speed up your IoT development progress .

  • The ultra-low cost WIFI module is used for rapid prototyping development boards, and integrates the WIFI chip ESP8266 with a price of less than RMB 10, providing you with the most cost-effective IoT application development platform.

The NodeMCU development board based on ESP8266, with GPIO, PWM, I2C, 1-Wire, ADC and other functions, combined with NodeMCU firmware to provide the fastest way for your prototype development.

The detailed hardware configuration is as follows:

  • The core module is ESP8266
  • MCU is Xtensa L106
  • RAM 50K
  • Flash 512K
  • D1~D10: 10 GPIO, each can be configured as PWM, I2C, 1-wire

There are two serial port configurations

  • CP2102 USB serial port, plug and play (the official driver supports Windows, OS X, Linux and VMware)
  • CH340 USB serial port, which is mostly used in China, needs to install ch340 driver.
    Note: In VMware, to install Ubuntu system, you only need to install the driver in the host. Then connect the device to VMware. Virtual machine -> removable device, you can see the ch340 device, just click connect.

CP2102 version
Write picture description here

CH340 version
Write picture description here

2. Development environment

NodeMCU currently supports the types of development host systems covering Windows, Linux and Mac OS X. It also supports Linux environments built through VMware virtual machines.

It should be mentioned that if you use a virtual machine, please use VMware instead of VirtualBox. Although VirtualBox can also be used for Lua development through serial port virtualization, it is inconvenient because VirtualBox cannot virtualize uhci's cp210x, so the Linux in VirtualBox cannot directly burn firmware.

The following is the preferred development environment we recommend:
pure Linux (ubuntu first)
install VMware on Mac OS X, run Linux in VMware (Lubuntu first)
install VMware on Windows, run Linux in VMware (Lubuntu first)
This article mainly introduces the above three. In fact, the core is still the Linux development environment. The latter two only need to install VMware and install the USB serial port driver on the host.

If you don't have a Linux environment at hand, it is recommended to install Linux through VMware, which is safer and more reliable and avoids breaking the original system.

VMware can be downloaded from the official website. After installation, download the Lubuntu 14.04 ISO from the Ubuntu official, and then start VMware to install Lubuntu. When installing, first create/Add a Lubuntu (Create a custom virtual machine -> Linux -> Ubuntu 64bit), then select Startup Disk through Settings/Settings to start the installation from CD/DVD, select the ISO just downloaded, and then pass Startup Disk select back to start from Hard Disk (SCSI).

As for the pure Windows or pure Mac OS X environment, it is not recommended, because under Linux, most of the tools needed can be installed with one command, while in Windows and Mac OS X, a lot of time will be wasted to find different tools in different places , And then spend more time to solve all kinds of software compilation and installation problems, with less effort. Moreover, OS X often has paid services for the basic development environment, which is really unfriendly. Cherish your life and stay away from them!

But if you really love them, you can still refer to the follow-up materials, especially the Noduino/Noduino SDK related parts, which introduces the development environment of Windows and Mac OS X in detail. In addition, the other software or tools in the reference materials are basically all README.md is provided to explain the respective installation, please refer to it.

3. Operation steps

Take the Ubuntu environment under Windows and VMware as an example (CH340 version):


NodeMCU development under Windows environment

Tools needed:

  • sciTe-lua development environment for lua learning. Specific reference: http://www.runoob.com/lua/lua-environment.html
    Note: During the installation process, if the connection to the server fails. You can download the required library yourself, and then reinstall it.
  • CH340 driver
  • ESP8266Flasher-Flash ESP firmware
  • ESPlorer-similar to the serial debugging assistant, used to interact with NodeMCU, you can also upload lua scripts.
    Note: Java environment is required and JDK is required.
  • Fans_ESP8266_SDK_Make.exe – Reference for compiling ESP firmware: http://blog.csdn.net/u013636122/article/details/50630235

(1) Install CH340 driver

Download the CH340 driver, after installation.
Right click on My Computer -> Management -> Device Manager -> Port (COM and LPT), you can see the ch340 driver and serial port number.

(2) Obtain firmware

There are three ways to get the firmware:

  1. NodeMCU provides online cloud compilation, select the required module, fill in the email address, and it will be sent to the mailbox soon. Reference: https://nodemcu-build.com/ (recommended)

  2. Download the firmware package compiled by others.

  3. Compile by yourself to get the firmware package.

(3) Firmware burning

Set the serial port number,
click Config->click the small gear button, set the firmware path you get,
click Flash
Write picture description here
Write picture description here

Write picture description here

(4) Start using

  1. Open ESPlorer.jar, you will see the following interface:
    Write picture description here
    Note: ESPlorer needs to install the JDK environment

  2. Click the refresh button, select the serial port number, click open, and it will show that it is connecting. If you cannot connect for a long time, you can press the reset button on NodeMCU. The firmware information will be displayed, click Chip ID, the ID information will be returned, indicating that the interaction is normal.

  3. The right area is the interactive page, which will display the interactive information.
    There is a line of input box at the bottom left, you can enter the command to be executed, or you can select it in the drop-down menu, and click send to send.
    Command reference:
    https://github.com/nodemcu/nodemcu-firmware/wiki/nodemcu_api_cn
    such as:

    >print(“Hello,NodeMCU”)  
    >Hello,NodeMCU  
    
    • 1
    • 2

Write picture description here
4. The left area is the Lua scripting area.

Write the lua script, click Send to ESP, you can see the right area starts to upload the code, after the end. The code starts to execute, and the interactive information is displayed in the right area.
Click Upload to upload the code to NodeMCU, enter dofile("filename.lua") on the command line, and start executing filename.lua.
Note: If Send or Upload fails, you can press reset and try multiple times.
Write picture description here
Note: There are some differences between Save to ESP, Send to ESP, Run, and Upload. You can find out by yourself, but I didn't go into it.


Ubuntu environment development under Vmare

Note: The version I use

  • VMware workstations 12 pro
  • Ubuntu 14.0 Desktop

Tools needed:

(1) Download and install the ch340 driver

The host needs to install the driver, and then click on the virtual machine -> removable device in VMware to see the ch340 device, and click connect.

dmesg  命令查看已经安装的ch340驱动  
ls -l /dev/ttyUSB0   查看驱动安装的时间等  
  • 1
  • 2

(2) Install minicom

minicom is a serial communication tool under linux, used for NodeMcu debugging

$ sudo apt-get install minicom    -- 下载安装minicom  
$ sudo minicom -D /dev/ttyUSB0    -- 使用-D可以指定串口设备  
  • 1
  • 2

After entering minicom, you can enter the setting mode through crtl-A, and then press A-Z keys to enter different setting interfaces.
Commonly used keys:
Q: Exit minicom
L: Save serial port log
Z: Get help
P: Set serial port

(3) Download and burn esp firmware

NodeMCU provides online cloud compilation, select the required module, fill in the mailbox, and it will be sent to the mailbox soon. Link: https://nodemcu-build.com/
You can also compile by yourself, at https://github.com /nodemcu/nodemcu-firmware/releases/ download the source code.
Can also use

$ wget -c https://github.com/nodemcu/nodemcu-firmware/``releases/download/0.9.6-dev_20150704/nodemcu_float_0.9.6-dev_20150704.bin  
  • 1

Download the firmware compiled
by others Then let's download the burning tool esptool.py. At the same time install other necessary tools.

$ sudo apt-get install git python python-serial python-setuptools  
$ git clone https://github.com/themadinventor/esptool.git  
$ cd esptool  
$ python setup.py install  
  • 1
  • 2
  • 3
  • 4

Then burn the firmware through esptool.py:

$ sudo esptool.py --port /dev/ttyUSB0 write_flash -fm dio -fs 32m -ff 40m 0x00000 nodemcu_float_0.9.6-dev_20150704.bin  
  • 1

After programming, press RST to reboot into the new firmware.
The default communication baud rate of sptool.py is 115200 during programming. In order to speed up the programming speed, you can set it to 921600 through -baud 921600.
After booting, the
file system can be formatted by file.format()

(4) Download and install luatool

I have tested that both tools can be used to upload Lua programs. They are:
luatool.py: can be used to transfer Lua scripts from the command line without the need for complex graphical tool support. It also supports uploading
nodemcu.py via serial port and Telnet : except that it does not support Except for uploading via Telnet, the basic functions are the same as luatool, but the operation method is slightly different.
Download the above tools:

$ git clone https://github.com/4refr0nt/luatool.git  
$ git clone https://github.com/md5crypt/nodemcu.py.git  
  • 1
  • 2

Both can be used for file transfer, the second can also be used as a serial terminal, both rely on pySerial, and the second requires a clipboard:

$ easy_install clipboard    
  • 1

(5) Start using

$ minicom -D /dev/ttyUSB0  --进入串口助手  
  file.format()    --格式化文件系统  
  print(“Hello,NodeMCU”) --打印字符串   
  gpio.mode(0, gpio.OUTPUT)  --设置0脚为输出模式  
  gpio.write(0, gpio.LOW)    --0脚置低  
  gpio.write(0, gpio.HIGH)   --0脚置高  
  node.restart()  --重启NodeMCU  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

The above is to execute each command through the serial port operation, which can be uploaded in batches through luatool, and the script is written in advance for batch upload.
Before uploading, write a lua script

$ vi test.lua    
  print('Hello, NodeMCU 1.0')  
  gpio.mode(4, gpio.OUTPUT)  
  gpio.write(4, gpio.LOW)  
  • 1
  • 2
  • 3
  • 4

Upload via luatool

$ cd /luatool
$ sudo ./luatool.py -p /dev/ttyUSB0 -b 9600 -f test.lua -r  
  • 1
  • 2

After the upload is complete,

$sudo minicom -D  /dev/ttyUSB0  --进入minicom  
dofile("test.lua")  --执行test脚本   
  • 1
  • 2

-p Specify upload device
-b Set baud rate
-f Specify script path
-r Restart after uploading
-h Get help

Guess you like

Origin blog.csdn.net/zwb_578209160/article/details/113122094