NodeMCU learning (1)-environment construction (1)

Article Directory

 

NodeMCU is an open source hardware platform, suitable for prototype development and functional verification of IoT applications. Using Lua as a development language, functions such as networking can be realized with just a few lines of scripts.

The NodeMCU development board based on Espressif's esp8266 has functions such as GPIO, PWM, I2C, ADC, etc. It also includes 4M external flash. Only a dozen soft coins are sold on TB, which is very cost-effective. Using Lua script development, the code is easy to be read, and the running efficiency is not high, so NodeMCU is mainly used to realize the Internet of Things application prototype using Wifi networking, personal small production, etc.

This series is to record learning NodeMCU, the goal is to finally realize an IoT display based on MQTT protocol. The display on the development board displays the data sent from the mobile phone/PC, and can collect sensor data and send it to the cloud and the mobile phone/PC.

1. Environment Setup

The main tools needed for NodeMCU development are:

  1. Serial port driver, use serial port to download code, debug and output log
  2. ESP8266Flasher is used to download NodeMCU basic firmware
  3. ESPlorer is used to download Lua scripts, debug and view logs. Note: Java environment 7 or above needs to be installed
    https://esp8266.ru/esplorer/
  4. Optional, Visual Studio Code is used to write Lua scripts, ESPlorer can also write scripts, but it is not a level compared to the code artifact VScode
  5. Optional, MQTT client on mqttfx PC, used to communicate with NodeMCU
  6. Optional, MQTT Client MQTT client on Android, used to communicate with NodeMCU

2. Development Process

The general development process of NodeMCU is as follows:

2.1 Compile basic firmware

The firmware can be compiled online or locally. Since the firmware does not need to be changed frequently, online compilation is used. The online compiling address is https://nodemcu-build.com/, enter the email address and confirm. Select the module you want to add according to your needs:

click the Start your build button at the bottom of the page to start compiling, and then you will receive an e-mail to start compiling, and you will receive a compiling e-mail after a while. This email contains the bin files of the two versions of the firmware, where float means that floating-point operations are supported, and integer means that only shaping operations are supported. The float version is chosen here, which is more convenient to support floating-point numbers, but the firmware is slightly larger, which has little effect on 4M flash.

2.2 Basic firmware download

Open the tool ESP8266Flasher, configure the serial port parameters:

select the basic firmware file to download:

connect to the NodeMCU development board, click the FLASH button of the development board, click the Flash (F) button of the download tool, and wait for the download to complete:

2.3 Lua script download

After downloading the basic firmware, power on again, the code will automatically start to run from the init.lua file, so the entry point for the Lua script is the init.lua file.
Connect the development board, open ESPlorer.bat, configure the baud rate to 115200, and click the open button:

Tip: Sometimes the serial communication is not normal, you can press the reset button on the development board, or click the FS Info button of ESPlorer to try.

After connecting to the development board, you can develop and write Lua scripts. Write init.lua first. Refer to Lua's tutorial for script syntax. Refer to the link for functions of each module at https://nodemcu.readthedocs.io/en/master/Write
init.lua After the script, click the Save to ESP button to download the script to the flash, and the init.lua script will run after resetting. Note that there can be no Chinese in the script, otherwise it will not run.

Tip: After NodeMCU is powered on, run the init.lua script immediately. If there is an error in the script, it will run an error and reset. After the reset, the init.lua script will be run again, and it will fall into an infinite loop. The solution is to add a delay to the script and leave enough time to download the error script again or click Format to delete all scripts.
Sample code:
tmr.delay(1000000)
print(“nodemcu start 0.1”)
tmr.delay(1000000)

3. Summary

From the above introduction, it can be seen that NodeMCU development is very simple and convenient, but because it is too simple, the code has no protection, and it is not particularly stable, so NodeMCU is more suitable for prototype development and personal small production.

Guess you like

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