乐鑫ESP32-WROOM-32E最小开发板编程入门(Arduino基础篇)

一、开发环境搭建

作为一名入门初学者,我们计划采用Arduino IDE开发工具进行初步的入门级开发,使用Arduino IDE实现程序的编译、下载程序至 ESP32 开发板等步骤。

1.1.概述

Arduino 是一款便捷灵活、方便上手的开源电子原型平台,包含硬件(各种型号的 Arduino 板)和软件(Arduino IDE)。由于Arduino的编程语言更为简单和人性化,非常适用于入门初学者做为进行嵌入式开发的首选工具。

1.2.准备工作

1.2.1.硬件

  • ESP32-WROOM-32E最小开发板
  • USB 数据线 (Type-C 数据线)
  • 电脑(Windows 10)

1.2.2.软件

准备软件安装包:

我们在开发资料包中提供的Arduino IDE版本是 2.3.2

如需要安装其它版本,可到Arduino官网去另行下载并安装。

Arduino官网地址: https://www.arduino.cc/en/software

1.3.安装

1.3.1.Arduino开发环境搭建

准备安装文件,进入开发工具文件夹下的02开发工具Arduino文件夹,运行安装程序即可。

如果安装Arduino其它版本,可以它的官网去另行下载安装。

运行Arduino安装程序

image-20240605162109208

点击 我同意开始安装

image-20240605162156847

点击 下一步,进入下一步

image-20240605162235479

点击 安装,开始安装

image-20240605162311330

直到安装完成

image-20240605162451890

安装完成,点击 完成 结束安装程序

双击桌面的Arduino图标进入到Arduino IDE主界面

image-20240605162910825

[注意事项]

初次运行会弹出防火墙访问网络及安装驱动的窗口,直接允许和安装即可

默认界面语言为英文,可在File–>Preferences中,将Language的值设置为中文

1.3.2.配置ESP32开发板

设置开发板管理器,如下图所示,进入工具->开发板->开发板管理器

image-20240605163848961

如下图所示在开发板搜索栏中输入ESP32,注意这里是选择第二项由Espressif发行的版本,选择最新版,点击安装

image-20240605164226909

开始安装后,下方进度条会提示安装进度,耐心等待直到安装完成

image-20240605164300072

安装完成后左侧面板中会有3.0.0已安装的提示,进入开板板管理器,可以看到ESP32 Arduino已安装,这里选择ESP32 Dev Module这个选项。

然后打开 工具->开发板->esp32,在弹出的esp32列表中选择ESP32 Dev Module模组

image-20240605170950882

至此我们的开发环境已经搭建完成,接下来我们就可以开始开发应用程序了!

二、编译运行第一个示例程序

本示例执行一个打印动作,每两秒钟打印一次字符串"Hello ESP32!"到串口。

2.1.输入代码

接下来我们就可以开始编写代码了,输入以下代码

void setup() {
  //初始化波特率
  Serial.begin(9600);
}

void loop() {
  //打印字符串
  Serial.println("Hello ESP32!");
  //延时2s执行
  delay(2000);
}

2.2.编译并上传程序

输入完成后,初次编译需要指定程序的存放目录,并命名一个工程名称,我这里简单命名一个Hello,然后执行上传命令即可进行代码的编译并上传至开发板,点击上传按钮开始上传

image-20240610113721070

看到这样的执行结果就说明程序已经正常上传到开发板,我们打开串口监视器

image-20240610113629691

可以看到程序已经正常运行。

三、点亮LED三色灯程序设计

本示例学习如何点亮板卡自带的LED三色灯,三色灯由红、绿、蓝三种颜色输出,通过PWM控制颜色的叠加混合来组成不同颜色。

开发板上设计了两个三色灯,LED2和LED3,我们这里是点亮的LED2灯,通过原理图可以看到LED2对应的GPIO口分别是25(绿色),26(蓝色),27(红色)

3.1.点亮LED灯

打开Arduino IDE,输入以下代码

int redPin = 25;
int greenPin = 26;
int bluePin = 27;

void setup() {
  pinMode(redPin, OUTPUT);
  pinMode(greenPin, OUTPUT);
  pinMode(bluePin, OUTPUT);
}

void loop() {
  setColor(255, 0, 0);  // 红色
  delay(1000);
  setColor(0, 255, 0);  // 绿色
  delay(1000);
  setColor(0, 0, 255);  // 蓝色
  delay(1000);
}

void setColor(int red, int green, int blue) {
  analogWrite(redPin, 255 - red);
  analogWrite(greenPin, 255 - green);
  analogWrite(bluePin, 255 - blue);
}

保存程序并命名为LED_RGB,然后编译并上传程序到板卡,注意编译时选择正确的开发板和端口,
image-20240623115528751

可以看到指示灯成功点亮并可以实现颜色的渐变。

四、实现WiFi联网获取数据程序设计

本示例学习如何使用板卡的ESP32模组来实现连接WIFI网络,并通过HttpClient协议来获取网络数据。

通过访问由聚合数据提过的天气预报API接口来获取天气预报数据,注意需要提前注册并获取密钥,申请天气预报的API接口即可

聚合数据官网https://www.juhe.cn/

注册并申请服务成功后,可登录聚合数据后台查看天气预报的接口文档,可以看到API返回的类型是json格式,因此我们需要引第三方的类来进行返回数据解析

image-20240213114046044

4.1.引入第三方库

本示例中需要用到第三方库,由于API返回的类型是json格式,我们需要使用ArduinoJson这个第三方库来解析获取到的数据。

引入方法,我们打开Arduino IDE,进入项目->导入库–>管理库

在左侧库管理界面搜索栏中输入我们想要查找的库名称,我们这里输入"ArduinoJson",回车进行搜索

image-20240610133742181

选择ArduinoJson的版本,这里 我选择的是7.0.4版本,点击安装开始这个库的安装,待提示完成后即可进行下一步编程。

4.2.实现WiFi联网

打开Arduino IDE,输入以下代码

#include <WiFi.h>
#include <HTTPClient.h>
#include <ArduinoJson.h>

const char * ssid = "此处为WIFI网络名称";
const char * password = "此处为WIFI密码";

void setup() {

  Serial.begin(115200);

  WiFi.begin(ssid, password);
  Serial.print("正在连接WIFI");
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("连接成功");
  Serial.print("IP Address");
  Serial.println(WiFi.localIP());
}

void loop() {

}

注意将程序中的相关WiFi参数配置,保存程序到HTTP目录下,然后编译和上传程序到板卡

image-20240610134058057

打开串口监视器,此处注意波特率应设置为115200

可以看到连接WiFi成功,并打印出了当前板卡的IP地址,说明板卡已经正常接入WiFi网络连网成功。

4.3.获取并解析天气预报数据

打开Arduino IDE,修改4.2章节代码

#include <WiFi.h>
#include <HTTPClient.h>
#include <ArduinoJson.h>

const char * ssid = "此处为WIFI网络名称";
const char * password = "此处为WIFI密码";

String url = "http://apis.juhe.cn/simpleWeather/query";
String city = "石家庄";
String key = "此处为聚合数据密钥ID";

void setup() {

  Serial.begin(115200);

  WiFi.begin(ssid, password);
  Serial.print("正在连接WIFI");
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("连接成功");
  Serial.print("IP Address");
  Serial.println(WiFi.localIP());

  HTTPClient http;

  http.begin(url + "?city=" + city + "&key=" + key);

  int http_code = http.GET();
  Serial.printf("HTTP STATUS: %d\n", http_code);

  String response = http.getString();
  Serial.print("响应数据");
  Serial.println(response);

  http.end();

  DynamicJsonDocument doc(1024);

  deserializeJson(doc, response);

  unsigned int temp = doc["result"]["realtime"]["temperature"].as<unsigned int>();
  String info = doc["result"]["realtime"]["info"].as<String>();
  int aqi = doc["result"]["realtime"]["aqi"].as<int>();

  Serial.printf("温度: %d, 天气: %s,空气指数: %d\n", temp, info, aqi);
}

void loop() {

}

然后编译并上传程序到板卡,上传程序程序后会自动运行,打开串口监视器

image-20240610135138482

可以看到联网成功并查询到天气预报数据。

五、点亮OLED显示屏

开发板集成了0.96寸OLED显示屏,用于我们开发过程中直观的显示信息

OLED显示屏分辨率为128*64,使用I2C协议进行通讯,I2C通讯地址为0X3C,驱动芯片为SSD1306

5.1.引入第三方库

为了点亮OLED屏幕,我们需要引入U8g2第三方库,我们进入库管理,搜索并安装U8g2库

image-20240628121052307

5.2.显示文字

打开Arduino IDE,输入程序,命名工程名称为Oled_Display_Text

#include <WiFi.h>
#include <Wire.h>
#include <U8g2lib.h>

/* u8g2 constructer */
U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, /* reset=*/U8X8_PIN_NONE, /* clock=*/SCL, /* data=*/SDA);  // ESP32 Thing, HW I2C with pin remapping

void setup() {
  Serial.begin(9600);
  //OLED初始化
  u8g2.begin();
  u8g2.enableUTF8Print();
  u8g2.clearBuffer();
  u8g2.setFont(u8g2_font_wqy13_t_gb2312);
  u8g2.setCursor(10, 30);
  u8g2.print("HTKI");
  u8g2.setCursor(10, 45);
  u8g2.print("物联网开发套件"); 
  u8g2.setCursor(10, 60);
  u8g2.print("汉图电子"); 
  u8g2.sendBuffer();
  delay(1500);
}

void loop() {
}

编译并上传成功后,屏幕即可点亮并打印文字信息。

Oled_Display_Text

5.3.显示网络时间

打开Arduino IDE,输入程序,命名工程名称为Oled_Display_Time

#include <WiFi.h>
#include <Wire.h>
#include <U8g2lib.h>

/* u8g2 constructer */
U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, /* reset=*/U8X8_PIN_NONE, /* clock=*/SCL, /* data=*/SDA);  // ESP32 Thing, HW I2C with pin remapping

const char* ntpServer = "pool.ntp.org";
const long gmtOffset_sec = 8 * 3600;
const int daylightOffset_sec = 0;

const char * ssid = "此处为WIFI网络名称";
const char * password = "此处为WIFI密码";

void printLocalTime(); // 获取网络时间

void setup() {
  Serial.begin(9600);
  //OLED初始化
  u8g2.begin();
  u8g2.enableUTF8Print();
  u8g2.clearBuffer();
  u8g2.setFont(u8g2_font_wqy13_t_gb2312);

  WiFi.begin(ssid, password);
  u8g2.setCursor(0, 15);
  u8g2.print("WIFI is connecting ...");
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  u8g2.setCursor(0, 30);
  u8g2.print("IP: " + WiFi.localIP().toString());
  u8g2.setCursor(0, 45);
  u8g2.print("Wifi connected");
  u8g2.sendBuffer();
  // 从网络时间服务器上获取时间并绘制到OLED
  configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);

}

void loop() {
  delay(1000);
  printLocalTime();
}

void printLocalTime() {
  struct tm timeinfo;
  if (!getLocalTime(&timeinfo)) {
    Serial.println("Failed to obtain time");
    return;
  }
  Serial.println(&timeinfo, "%F %T %A");  // 格式化输出
  char buffer[80];
  strftime(buffer, sizeof(buffer), "%Y/%m/%d %H:%M:%S", &timeinfo);
  u8g2.setCursor(0, 60);
  u8g2.print(buffer);
  u8g2.sendBuffer();
}

编译并上传成功后,屏幕上显示出网络时间,由于是联网获取网络时间,开发板上电运行后需要等待一会儿才可看到时间

Oled_Display_Time

5.4.显示图片

5.4.1.准备位图源数据

这里推荐使用一个在线工具image2cpp获取图像的源数据,

在线工具地址:https://javl.github.io/image2cpp/

打开在线工具

image-20240630102215963

在第一步Select image中选择要获取源数据的图像,其余选项保持默认即可,注意在第四步Output这一项中,需要将Swap bits in byte这个swap复选框选中,然后点击Generate code按钮即可获取到源数据

image-20240630102635481

5.4.2.显示图片

打开Arduino IDE,输入程序,命名工程名称为Oled_Display_Bitmap

#include <WiFi.h>
#include <Wire.h>
#include <U8g2lib.h>

/* u8g2 constructer */
U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, /* reset=*/U8X8_PIN_NONE, /* clock=*/SCL, /* data=*/SDA);  // ESP32 Thing, HW I2C with pin remapping

const unsigned char epd_bitmap_htki_logo[] PROGMEM = {
  // 'htki_logo_128_64', 128x64px
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  0xff, 0xff, 0xff, 0x3f, 0xe0, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  0xff, 0xff, 0xff, 0x07, 0x80, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  0xff, 0xff, 0xff, 0x03, 0x00, 0xfe, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  0xff, 0xff, 0xff, 0xc3, 0x1f, 0xf8, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  0xff, 0xff, 0x7f, 0xc2, 0x7f, 0xf0, 0x3f, 0xfe, 0x0f, 0x00, 0x00, 0xe0, 0x1f, 0x8c, 0xff, 0xff,
  0xff, 0xff, 0x3f, 0xc2, 0xff, 0xe1, 0x3f, 0xfe, 0x07, 0x00, 0x00, 0xe0, 0x1f, 0x86, 0xff, 0xff,
  0xff, 0xff, 0x3f, 0xc2, 0xff, 0xe3, 0x3f, 0xfe, 0x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, 0x3f, 0xfe, 0x87, 0x3f, 0xfc, 0xe1, 0x3f, 0x80, 0xff, 0xff,
  0xff, 0xff, 0x7f, 0xf0, 0x1f, 0xf3, 0x3f, 0xfe, 0x8f, 0x3f, 0xfc, 0xe1, 0x7f, 0x80, 0xff, 0xff,
  0xff, 0xff, 0xff, 0x01, 0x07, 0xfb, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  0xff, 0xff, 0xff, 0x03, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  0xff, 0xff, 0xff, 0x0f, 0xc0, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
};

void setup() {
  Serial.begin(9600);
  //OLED初始化
  u8g2.begin();
  u8g2.enableUTF8Print();
  u8g2.clearBuffer();
  u8g2.setFont(u8g2_font_wqy13_t_gb2312);
  u8g2.drawXBMP(0, 0, 128, 64, epd_bitmap_htki_logo);
  u8g2.sendBuffer();
  delay(1500);
}

void loop() {
}

编译并上传成功后,屏幕上显示出Logo图片

Oled_Display_Bitmap

5.5.显示天气

打开Arduino IDE,输入程序,命名工程名称为Oled_Display_Weather

#include <WiFi.h>
#include <Wire.h>
#include <HTTPClient.h>
#include <ArduinoJson.h>
#include <U8g2lib.h>

const char * ssid = "此处为WIFI网络名称";
const char * password = "此处为WIFI密码";

String url = "http://apis.juhe.cn/simpleWeather/query";
String city = "石家庄";
String key = "此处为聚合数据密钥ID";

/* u8g2 constructer */
U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, /* reset=*/U8X8_PIN_NONE, /* clock=*/SCL, /* data=*/SDA);  // ESP32 Thing, HW I2C with pin remapping

void setup() {
  Serial.begin(9600);
  //连接WIFI
  WiFi.begin(ssid, password);
  Serial.print("正在连接WIFI");
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("连接成功");
  Serial.print("IP Address");
  Serial.println(WiFi.localIP());

  //访问网络API获取天气数据
  HTTPClient http;

  http.begin(url + "?city=" + city + "&key=" + key);

  int http_code = http.GET();
  Serial.printf("HTTP STATUS: %d\n", http_code);

  String response = http.getString();
  Serial.print("响应数据");
  Serial.println(response);

  http.end();

  DynamicJsonDocument doc(1024);

  deserializeJson(doc, response);

  unsigned int temp = doc["result"]["realtime"]["temperature"].as<unsigned int>();
  String info = doc["result"]["realtime"]["info"].as<String>();
  int aqi = doc["result"]["realtime"]["aqi"].as<int>();

  Serial.printf("温度: %d, 天气: %s,空气指数: %d\n", temp, info, aqi);

  //OLED初始化
  u8g2.begin();
  u8g2.enableUTF8Print();
  u8g2.clearBuffer();
  u8g2.setFont(u8g2_font_helvB10_tf);
  u8g2.setCursor(0, 16);
  u8g2.print("Temp & Humidity");
  u8g2.setFont(u8g2_font_helvB08_tf);
  u8g2.setCursor(0, 32);
  u8g2.print("Temp= ");
  u8g2.print(temp);
  u8g2.print(" °C");
  u8g2.setCursor(0, 48);
  u8g2.print("Humidity= ");
  u8g2.print(aqi);
  u8g2.print(" %");
  u8g2.sendBuffer();
  delay(1500);
}

void loop() {
}

编译并上传成功后,屏幕上显示获取的当前天气数据

Snapshot002

注意事项

这里没有使用汉字,U8g2中文字体库非常占用空间,设置显示中文字体后会编译报错

  • 12
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值