ESP32从零开始(4)—— 手把手带你构建自己第一个项目(hello world!)


这是本人的学习历程,属于学习总结。分享给大家,仅作参考。

(如果哪里有问题请留言指出。谢谢~)


概述

本项目通过Cmake构建。


一、用到的Linux操作指令

//切换目录
cd 目录

//新建文件夹。   p 确保目录名称存在,不存在的就建一个。
mkdir -p 文件夹名
		
//删除文件夹及里面所有文件
rm -rf 目录名字	

//新建文件
touch 文件名.后缀

///

//vim 操作
//打开文件
vim 文件名.后缀
//编辑文件:键盘按“i“
//保存文件:键盘按“ESC“,然后输入“:wq“

二、建立项目

1、要建立的项目结构

/*
 *  - my_tamplate/
 *            		- CMakeLists.txt
 *            		- main/       - CMakeLists.txt
 *                                - main.c
 * 								  - include/ - main.h
 *            		- components/ - component1/ - CMakeLists.txt
 *                                        		- comp1.c
 *                                        		- include/ - comp1.h
 */
  • my_tamplate 为项目文件夹

    • CMakeLists.txt项目 CMakeLists 文件,包含整个项目的构建设置

    • main文件夹 为主程序文件夹

      • CMakeLists.txt组件 CMakeLists 文件

      • main.c主源程序

      • include/ - main.h头文件

    • components 为 组件文件夹

      • component1 为 组件成员文件夹,与 main文件夹 相似。

2、通过 终端 建立项目 (目的是练习一下linux操作)

  • 按 ctrl + alt + T 打开终端

  • 创建文件夹
mkdir -p ~/esp/myPrj/my_tamplate/main/include
mkdir -p ~/esp/myPrj/my_tamplate/components/component1/include

  • 创建 项目 CMakeLists 文件
cd ~/esp/myPrj/my_tamplate
touch CMakeLists.txt
vim CMakeLists.txt
  • 键盘按 “i” 进入编辑模式:
cmake_minimum_required(VERSION 3.5)
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
project(my_tamplate)
  • 键盘按ESC,然后输入“:wq”,进行保存

  • 创建 main文件夹 里的文件
cd ~/esp/myPrj/my_tamplate/main
touch CMakeLists.txt
touch main.c
cd ~/esp/myPrj/my_tamplate/main/include
touch main.h
  • 编辑 CMakeLists.txt
cd ~/esp/myPrj/my_tamplate/main
vim CMakeLists.txt
  • 方法同上,编辑内容为:
idf_component_register(SRCS "main.c"
                       INCLUDE_DIRS "include")

  • 创建 component1文件夹 里的文件
cd ~/esp/myPrj/my_tamplate/components/component1
touch CMakeLists.txt
touch comp1.c
cd ~/esp/myPrj/my_tamplate/components/component1/include
touch comp1.h
  • 编辑 CMakeLists.txt
cd ~/esp/myPrj/my_tamplate/components/component1
vim CMakeLists.txt
  • 编辑内容为:
idf_component_register(SRCS "comp1.c"
                       INCLUDE_DIRS "include")

  • 最后得到的项目结构:
└── my_tamplate
    ├── CMakeLists.txt
    ├── components
    │   └── component1
    │       ├── CMakeLists.txt
    │       ├── comp1.c
    │       └── include
    │           └── comp1.h
    └── main
        ├── CMakeLists.txt
        ├── include
        │   └── main.h
        └── main.c

.c和.h文件,在vscode里再编辑。


3、在VScode编译项目

  • 使用 VScode 打开项目
cd ~/esp/myPrj
code my_tamplate

在这里插入图片描述

  • 编辑 comp1.h
#ifndef _COMP1_H_
#define _COMP1_H_

void comp1(void);

#endif //_COMP1_H_
  • 编辑 comp1.c
#include <stdio.h>
#include "comp1.h"

void comp1(void)
{
    printf("Hello world!\n");
}
  • 编辑 main.c
#include "sdkconfig.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_system.h"
#include "comp1.h"

void app_main(void)
{
	//调用组件,串口输出helloworld
    comp1();
    //延时10秒
    vTaskDelay(10000 / portTICK_PERIOD_MS);
    //重启
    esp_restart();
}
  • 编译项目: ctrl+E, 然后B
    在这里插入图片描述
  • 烧录程序: ctrl+E, 然后F
    在这里插入图片描述

  • 监控串口:ctrl+E, 然后M
    在这里插入图片描述

  • 退出监控: ctrl+]

4、补全 VScode头文件路径

  • 点击 灯泡, 选择 编辑“includePath”
    在这里插入图片描述
  • vscode自动生成,点击进入。
    在这里插入图片描述
  • 添加路径
"${workspaceFolder}/**",
"~/esp/esp-idf/components/freertos/include/",
"~/esp/esp-idf/components/esp_system/include/"

在这里插入图片描述

注:补全路径,是为了写代码的时候能补全代码;或者查看源文件。不补全路径,vscode会提示错误,但不影响编译!
  • VScode关闭错误提示。点击 灯泡, 选择 ”禁用错误波形曲线“
    在这里插入图片描述
  • VScode 启用 错误波形曲线。将对应内容删除即可。
    在这里插入图片描述
  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
好的,下面是具体步骤: 1. 连接ESP32开发板,并在Arduino IDE中安装ESP32开发板支持。 2. 打开Arduino IDE,新建一个项目。 3. 在“文件”菜单中,选择“示例”->“WiFi”->“WiFiWebServer”打开示例代码。 4. 修改代码,将WiFi的SSID和密码替换为你自己的WiFi网络的名称和密码。 5. 在“setup”函数中,添加代码 `Serial.begin(115200);` 打开串口,方便调试。 6. 在“loop”函数中,添加代码 `Serial.println("Hello World from ESP32!");` 打印“Hello World from ESP32!”。 7. 上传代码到ESP32开发板。 8. 打开串口监视器,查看ESP32是否连接到WiFi网络。 9. 在浏览器中输入ESP32的IP地址,即可看到“Hello World from ESP32!”的输出。 下面是完整的代码: ```c #include <WiFi.h> #include <WiFiClient.h> #include <WebServer.h> const char* ssid = "your_SSID"; const char* password = "your_PASSWORD"; WebServer server(80); void setup() { Serial.begin(115200); // Connect to Wi-Fi network with SSID and password Serial.print("Connecting to "); Serial.println(ssid); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(1000); Serial.println("Connecting to WiFi..."); } Serial.println("WiFi connected"); // Print ESP32 IP address Serial.print("IP address: "); Serial.println(WiFi.localIP()); server.on("/", []() { server.send(200, "text/plain", "Hello World from ESP32!"); }); server.begin(); } void loop() { server.handleClient(); Serial.println("Hello World from ESP32!"); delay(1000); } ``` 注意:在代码中需要将 `your_SSID` 和 `your_PASSWORD` 替换为你自己的WiFi网络的名称和密码。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值