开发环境搭建
前言: ESP32,作为一款高性能、低功耗的Wi-Fi与蓝牙双模芯片,因其强大的功能和广泛的应用场景,深受物联网开发者喜爱。本文将为您详细解析如何从零开始搭建ESP32的开发环境。
一、硬件准备
首先,您需要一块ESP32开发板,市面上有许多基于ESP32芯片设计的开发板,如ESP32-DevKitC、WEMOS D1 mini等。确保您的开发板工作正常,并且具备USB接口以便于连接计算机。
二、软件环境配置
1. 安装Arduino IDE
ESP32开发主要使用Arduino IDE或者PlatformIO等开发工具,这里以Arduino IDE为例进行说明:
- 下载并安装最新版本的Arduino IDE(https://www.arduino.cc/en/Main/Software)。
- 打开Arduino IDE,点击“文件”->“首选项”,在附加开发板管理器网址中添加ESP32的包仓库地址:
http://arduino.esp8266.com/stable/package_esp32_index.json
。
2. 安装ESP32 Board支持
-
在Arduino IDE中,点击“工具”->“板”->“开发板管理器”。
-
在弹出的窗口中搜索"ESP32",找到"ESP32 by Espressif Systems"并点击安装。这一步骤会下载并安装适用于ESP32的编译工具链和库文件。
注意:
下载源位于国外,因此速度极慢,需要科学上网。
并且科学上网前提下,Arduino IDE的2.0以上版本(新版)下载极慢且总是中断,建议电脑同时安装Arduino的1.8的版本进行开发板的下载安装。
不同版本的 Arduino IDE 可在同一台电脑共存,并且共用同样的库文件,开发板文件,配置文件。
3. 配置端口
将ESP32开发板通过USB数据线连接到电脑后,在Arduino IDE中选择正确的串口:“工具”->“端口”,连接ESP32 Dev Module的开发板型号,并且选择正确的COM口即可。
三、编写并上传代码
- 创建新项目:点击“文件”->“新建”创建一个新的Arduino Sketch。
- 编写代码:可以参考ESP32官方示例或自行编写代码,例如下面是一个简单的Blink程序:
void setup() {
pinMode(LED_BUILTIN, OUTPUT); // 设置LED引脚为输出模式
}
void loop() {
digitalWrite(LED_BUILTIN, HIGH); // LED亮
delay(1000); // 延时1秒
digitalWrite(LED_BUILTIN, LOW); // LED灭
delay(1000); // 延时1秒
}
- 上传代码:点击工具栏上的右箭头按钮(或快捷键Ctrl+U),等待IDE编译并通过USB将代码上传至ESP32开发板。
四、示例-启动WIFI
以下为 Arduino 代码:
WIFI 实验 - Client:
/* 深圳市普中科技有限公司(PRECHIN 普中)
技术支持:www.prechin.net
*
* 实验名称:WIFI实验--Client
*
* 接线说明:
*
* 实验现象:程序下载成功后,手机连接的WIFI需和ESP32连接的WIFI处于同一频段(比如192.168.1.xx),
然后在手机网页输入串口控制台输出的本机IP地址即可进入手机端网页控制板子LED。
*
* 注意事项:
*/
#include "public.h"
#include <WiFiMulti.h>
WiFiMulti WiFiMulti;
const char* ssid = "puzhong88";
const char* password = "PUZHONG88";
//WIFI连接路由器
void wifi_connect(void)
{
Serial.print("Connecting to ");
delay(10);
// We start by connecting to a WiFi network
WiFiMulti.addAP(ssid, password);
Serial.println();
Serial.println();
Serial.print("Waiting for WiFi... ");
while(WiFiMulti.run() != WL_CONNECTED) {
Serial.print(".");
delay(500);
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
delay(500);
}
void setup(){
Serial.begin(115200);
wifi_connect();
}
void loop(){
const uint16_t port = 1337;
const char * host = "192.168.1.17"; // ip or dns
Serial.print("Connecting to ");
Serial.println(host);
// Use WiFiClient class to create TCP connections
WiFiClient client;
if (!client.connect(host, port)) {
Serial.println("Connection failed.");
Serial.println("Waiting 5 seconds before retrying...");
delay(5000);
return;
}
// This will send a request to the server
//uncomment this line to send an arbitrary string to the server
//client.print("Send this data to the server");
//uncomment this line to send a basic document request to the server
client.print("GET /index.html HTTP/1.1\n\n");
int maxloops = 0;
//wait for the server's reply to become available
while (!client.available() && maxloops < 1000)
{
maxloops++;
delay(1); //delay 1 msec
}
if (client.available() > 0)
{
//read back one line from the server
String line = client.readStringUntil('\r');
Serial.println(line);
}
else
{
Serial.println("client.available() timed out ");
}
Serial.println("Closing connection.");
client.stop();
Serial.println("Waiting 5 seconds before restarting...");
delay(5000);
}
WIFI 实验 - WebServer:
/* 深圳市普中科技有限公司(PRECHIN 普中)
技术支持:www.prechin.net
*
* 实验名称:WIFI实验--WebServer
*
* 接线说明:LED模块-->ESP32 IO
(D1)-->(15)
*
* 实验现象:程序下载成功后,手机连接的WIFI需和ESP32连接的WIFI处于同一频段(比如192.168.1.xx),
然后在手机网页输入串口控制台输出的本机IP地址即可进入手机端网页控制板子LED。
*
* 注意事项:
*/
#include "public.h"
#include <WiFi.h>
const char* ssid = "ESP32";
const char* password = "123456789";
WiFiServer server(80);
//LED控制引脚
#define led_pin 2
//WIFI连接路由器
void wifi_connect(void)
{
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected.");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
void setup(){
Serial.begin(115200);
pinMode(led_pin, OUTPUT); // set the LED pin mode
delay(10);
wifi_connect();
server.begin();
}
void loop(){
/*
WiFiClient client = server.available(); // listen for incoming clients
if (client) { // if you get a client,
Serial.println("New Client."); // print a message out the serial port
String currentLine = ""; // make a String to hold incoming data from the client
while (client.connected()) { // loop while the client's connected
if (client.available()) { // if there's bytes to read from the client,
char c = client.read(); // read a byte, then
Serial.write(c); // print it out the serial monitor
if (c == '\n') { // if the byte is a newline character
// if the current line is blank, you got two newline characters in a row.
// that's the end of the client HTTP request, so send a response:
if (currentLine.length() == 0) {
// HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
// and a content-type so the client knows what's coming, then a blank line:
client.println("HTTP/1.1 200 OK");
client.println("Content-type:text/html");
client.println();
// the content of the HTTP response follows the header:
client.print("Click <a href=\"/H\">here</a> to turn the LED on pin 15 on.<br>");
client.print("Click <a href=\"/L\">here</a> to turn the LED on pin 15 off.<br>");
// The HTTP response ends with another blank line:
client.println();
// break out of the while loop:
break;
} else { // if you got a newline, then clear currentLine:
currentLine = "";
}
} else if (c != '\r') { // if you got anything else but a carriage return character,
currentLine += c; // add it to the end of the currentLine
}
// Check to see if the client request was "GET /H" or "GET /L":
if (currentLine.endsWith("GET /H")) {
digitalWrite(led_pin, HIGH); // GET /H turns the LED on
}
if (currentLine.endsWith("GET /L")) {
digitalWrite(led_pin, LOW); // GET /L turns the LED off
}
}
}
// close the connection:
client.stop();
Serial.println("Client Disconnected.");
}
*/
}
五、后续操作
一旦代码成功上传,您将在ESP32开发板上看到预期的行为(例如上述代码会让板载LED闪烁)。接下来,您可以根据实际需求进一步探索ESP32的各种功能,利用其丰富的外设接口和网络功能实现各种物联网应用。
总结起来,搭建ESP32的开发环境是一项系统性的工作,包括硬件准备、软件配置以及代码编写和上传等步骤。正确完成这些步骤后,您就拥有了一个可以高效开发ESP32项目的平台,尽情发挥您的创新思维,开启IoT世界的大门吧!
六、ESP32程序分享
整理了一些 ESP32 入手的基础实验程序,是基于 C 开发的 Arduino 程序,关注我下载程序吧。
链接:https://pan.baidu.com/s/1BPgTYlYdx_JYIHu8haKcxA?pwd=2003 提取码:2003