如何利用ESP32创建WEB服务器

  • 内容
  • ....
  • 相关

在本文中,我们将使用ESP32来创建一个web服务器。我们将把ESP32连接到Wi-Fi网络,然后ESP32将获得一个IP地址,WEB服务器将基于该地址创建。当您在浏览器中键入这个IP地址时,浏览器将向webserver发送一个连接请求,建立连接后,webserver将返回一个网页。

HTTP协议格式

客户机和web服务器之间的连接是基于HTTP协议建立的。HTTP协议的格式: HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n”

“HTTP/1.1 200 OK”表示服务器已接受请求,并以“200 OK”作为响应,这是状态码,后面跟着“\r\n”,这是HTTP协议的约束。“content – type: text/html\r\n”表示响应内容类型为html格式,后面是“\r\n”。在HTTP协议结束时,webserver将返回一个显示有“Hello world”的网页。

完整代码如下:

#include <WiFi.h>

const char* wifi_name = “Tenda”; //Your Wifi name
const char* wifi_pass = “XXXXXX”; //Your Wifi password
WiFiServer server(80); //Port 80

void setup()
{
Serial.begin(115200);

// Let’s connect to wifi network
Serial.print(“Connecting to “);
Serial.print(wifi_name);
WiFi.begin(wifi_name, wifi_pass); //Connecting to wifi network

while (WiFi.status() != WL_CONNECTED) { //Waiting for the responce of wifi network
delay(500);
Serial.print(“.”);
}
Serial.println(“”);
Serial.println(“Connection Successful”);
Serial.print(“IP address: “);
Serial.println(WiFi.localIP()); //Getting the IP address at which our webserver will be created
Serial.println(“Put the above IP address into a browser search bar”);
server.begin(); //Starting the server
}

void loop()
{
WiFiClient client = server.available(); //Checking for incoming clients

if (client)
{
Serial.println(“new client”);
String currentLine = “”; //Storing the incoming data in the string
while (client.connected())
{
if (client.available()) //if there is some client data available
{
char c = client.read(); //read a byte
Serial.print(c);
if (c == ‘\n’) //check for newline character,
{
if (currentLine.length() == 0) //if line is blank it means its the end of the client HTTP request
{
client.print(“<title>ESP32 Webserver</title>”);
client.print(“<body><h1>Hello World </h1>”);

break; //Going out of the while loop
}
else
{
currentLine = “”; //if you got a newline, then clear currentLine
}
}
else if (c != ‘\r’)
{
currentLine += c; //if you got anything else but a carriage return character,
}
}
}
}
delay(2000);
}

代码解释

const char* wifi_name = “Tenda”; //Your Wifi name
const char* wifi_pass = “XXXXXX”; //Your Wifi password
WiFiServer server(80); //Port 80

首先,我们包含了Wi-Fi库,它将帮助我们创建webserver。然后我们存储Wi-Fi名称和密码,这样我们就可以连接到这个Wi-Fi网络。之后,我们定义了要将数据发送到的端口。

Serial.print(“Connecting to “);
Serial.print(wifi_name);
WiFi.begin(wifi_name, wifi_pass); //Connecting to wifi network

while (WiFi.status() != WL_CONNECTED) { //Waiting for the responce of wifi network
delay(500);
Serial.print(“.”);
}
Serial.println(“”);
Serial.println(“Connection Successful”);

在setup函数中,我们使用上面提供的Wi-Fi信息将ESP32连接到我们的Wi-Fi网络。如果连接到Wi-Fi网络成功,则串行监视器上显示“Connection Successful”。否则,它将继续尝试,直到连接到Wi-Fi网络。下面的命令将获取IP地址,同时将在串行监视器上显示IP地址。

Serial.println(WiFi.localIP());

然后我们启动了服务器,这样我们就可以获取并发送数据到浏览器。

server.begin();

在LOOP函数中,代码将检查是否有客户端发送了http请求。如果有请求可用,它将显示在串行监视器上。在请求结束时,将发送HTML命令,并在网页上打印“Hello world”。

WiFiClient client = server.available(); //Checking for incoming clients

if (client)
{
Serial.println(“new client”);
String currentLine = “”; //Storing the incoming data in the string
while (client.connected())
{
if (client.available()) //if there is some client data available
{
char c = client.read(); //read a byte
Serial.print(c);
if (c == ‘\n’) //check for newline character,
{
if (currentLine.length() == 0) //if line is blank it means it’s the end of the client HTTP request
{
client.print(“<title>ESP32 Webserver</title>”);
client.print(“<body><h1>Hello World </h1>”);

break; //Going out of the while loop
}

如何运行代码

使用您的Wi-Fi名称和密码更改代码中的Wi-Fi名称和密码。然后上传代码并打开串口监视器。串行监视器将显示如下图所示的IP地址。

esp32 webserver serial monitor

在浏览器中输入这个IP地址,网页会显示如下图所示。

esp32 webserver