入門
客戶端與客戶端之間的連接webserver是使用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協議結束時,網絡服務器將返回顯示”Hello world“的網頁。
代碼
#include
const char* wifi_name = “Tenda_31BC98”; //Your Wifi name
const char* wifi_pass = “barcelona”; //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 == ’ ‘) //check for newline character,
{
if (currentLine.length() == 0) //if line is blank it means its the end of the client HTTP request
{
client.print(“”);
client.print(“
Hello World
”);
break; //Going out of the while loop
}
else
{
currentLine = “”; //if you got a newline, then clear currentLine
}
}
else if (c != ’ ‘)
{
currentLine += c; //if you got anything else but a carriage return character,
}
}
}
}
delay(2000);
}
代碼說明
首先,我們加入了Wi-Fi庫,它將幫助我們創建網絡服務器。然后我們存儲了Wi-Fi名稱和密碼,以便我們可以連接到這個Wi-Fi網絡。之后,我們定義了我們要將數據發送到的端口。
const char* wifi_name = “Tenda_31BC98”; //Your Wifi name
const char* wifi_pass = “barcelona”; //Your Wifi password
WiFiServer server(80); //Port 80
在setu中p功能,我們使用上面提供的Wi-Fi信息將ESP32連接到我們的Wi-Fi網絡。如果與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”);
以下命令將獲取IP地址,我們將在串行監視器上顯示它。
Serial.println(WiFi.localIP());
然后我們啟動了服務器,以便我們可以獲取數據并將數據發送到瀏覽器。
server.begin( );
在循環函數中,我們檢查了是否有客戶端發送了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 == ’ ‘) //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(“
”);
client.print(“
Hello World
”);
break; //Going out of the while loop
}
如何運行代碼
使用您的Wi-Fi名稱和密碼更改代碼中的Wi-Fi名稱和密碼。然后上傳代碼并打開串行監視器。串行監視器將顯示IP地址,如下圖所示。在瀏覽器中輸入此IP地址。
輸入IP地址后,網頁將如下所示。
-
Web服務器
+關注
關注
0文章
138瀏覽量
24371 -
ESP32
+關注
關注
18文章
958瀏覽量
17098
發布評論請先 登錄
相關推薦
評論