1)工程名称Name:template
2)开发板Board:NodeMCU 1.0(ESP-12E Module)
3)框架Framework:Arduino
1)找到Blink例程路径C:\Users\felix\.platformio\packages\framework-arduinoespressif8266\libraries\esp8266\examples\Blink
2)使用Nodepad++打开,复制里面的代码到src->main.cpp中,注意:main.cpp中的头文件不要删掉。
Blink例程代码:
void setup() {
pinMode(LED_BUILTIN, OUTPUT); // Initialize the LED_BUILTIN pin as an output
}
// the loop function runs over and over again forever
void loop() {
digitalWrite(LED_BUILTIN, LOW); // Turn the LED on (Note that LOW is the voltage level
// but actually the LED is on; this is because
// it is active low on the ESP-01)
delay(1000); // Wait for a second
digitalWrite(LED_BUILTIN, HIGH); // Turn the LED off by making the voltage HIGH
delay(2000); // Wait for two seconds (to demonstrate the active low LED)
}
3)在Blink基础上增加串口打印,整理后的代码如下。
宏定义#define LED_BUILTIN 2,可以加也可以不加,因为在pins_arduino.h中也会有定义。
注意:加了宏定义#define LED_BUILTIN 2,是无法跳转到定义的,需要注释掉,才能跳转。
main.cpp代码:
#include <Arduino.h>
//#define LED_BUILTIN 2 //ESP-12E模块自身的LED,对应GPIO2,低电平亮
//#define LED_BUILTIN 16 //nodemcu-esp8266开发板扩展的LED,对应GPIO16,低电平亮
void setup()
{
// put your setup code here, to run once:
Serial.begin(115200);//串口波特率配置
pinMode(LED_BUILTIN, OUTPUT);//设置引脚为输出模式
}
void loop()
{
// put your main code here, to run repeatedly:
Serial.print("Hello world!\r\n");//串口打印
//控制LED灯闪烁
digitalWrite(LED_BUILTIN, HIGH);//关闭LED灯
delay(1000);
digitalWrite(LED_BUILTIN, LOW); //打开LED灯
delay(1000);
}
pins_arduino.h代码(不需要修改):
#ifndef Pins_Arduino_h
#define Pins_Arduino_h
#define PIN_WIRE_SDA (4)
#define PIN_WIRE_SCL (5)
static const uint8_t SDA = PIN_WIRE_SDA;
static const uint8_t SCL = PIN_WIRE_SCL;
#ifndef LED_BUILTIN
#define LED_BUILTIN 2
#endif
#define LED_BUILTIN_AUX 16
static const uint8_t D0 = 16;
static const uint8_t D1 = 5;
static const uint8_t D2 = 4;
static const uint8_t D3 = 0;
static const uint8_t D4 = 2;
static const uint8_t D5 = 14;
static const uint8_t D6 = 12;
static const uint8_t D7 = 13;
static const uint8_t D8 = 15;
static const uint8_t D9 = 3;
static const uint8_t D10 = 1;
#include "../generic/common.h"
#endif /* Pins_Arduino_h */
platformio.ini代码:
修改VSCODE串口监视器波特率,增加代码:monitor_speed = 115200 #VSCODE串口监视器波特率
; PlatformIO Project Configuration File
;
; Build options: build flags, source filter
; Upload options: custom upload port, speed and extra flags
; Library options: dependencies, extra library storages
; Advanced options: extra scripting
;
; Please visit documentation for the other options and examples
; https://docs.platformio.org/page/projectconf.html
[env:nodemcuv2]
platform = espressif8266
board = nodemcuv2
framework = arduino
monitor_speed = 115200 #VSCODE串口监视器波特率
1)下载代码成功后,可以看到开发板上的LED灯闪烁。
2)VSCODE中打开串口监视器,可以看到打印Hello world!。