入門(mén)
客戶(hù)端與客戶(hù)端之間的連接webserver是使用HTTP協(xié)議建立的。 HTTP協(xié)議如下所示:HTTP/1.1 200 OK r nContent-Type:text/html r n r n“。
”HTTP/1.1 200 OK“表示服務(wù)器已接受請(qǐng)求并已回復(fù)“200 OK”,這是狀態(tài)代碼,后跟“ r n”,這是HTTP協(xié)議的約束。
“Content-Type:text/html r n“表示響應(yīng)內(nèi)容類(lèi)型為HTML格式,后跟” r n“
在HTTP協(xié)議結(jié)束時(shí),網(wǎng)絡(luò)服務(wù)器將返回顯示”Hello world“的網(wǎng)頁(yè)。
代碼
#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);
}
代碼說(shuō)明
首先,我們加入了Wi-Fi庫(kù),它將幫助我們創(chuàng)建網(wǎng)絡(luò)服務(wù)器。然后我們存儲(chǔ)了Wi-Fi名稱(chēng)和密碼,以便我們可以連接到這個(gè)Wi-Fi網(wǎng)絡(luò)。之后,我們定義了我們要將數(shù)據(jù)發(fā)送到的端口。
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網(wǎng)絡(luò)。如果與Wi-Fi網(wǎng)絡(luò)的連接成功,則“連接成功”將顯示在串行監(jiān)視器上。否則,它將繼續(xù)嘗試,直到它將連接到Wi-Fi網(wǎng)絡(luò)。
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地址,我們將在串行監(jiān)視器上顯示它。
Serial.println(WiFi.localIP());
然后我們啟動(dòng)了服務(wù)器,以便我們可以獲取數(shù)據(jù)并將數(shù)據(jù)發(fā)送到瀏覽器。
server.begin( );
在循環(huán)函數(shù)中,我們檢查了是否有客戶(hù)端發(fā)送了http請(qǐng)求。如果有任何客戶(hù)端請(qǐng)求可用,它將存儲(chǔ)在字符中并顯示在串行監(jiān)視器上。在請(qǐng)求結(jié)束時(shí),我們將發(fā)送HTML命令,它將在網(wǎng)頁(yè)上打印“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
}
如何運(yùn)行代碼
使用您的Wi-Fi名稱(chēng)和密碼更改代碼中的Wi-Fi名稱(chēng)和密碼。然后上傳代碼并打開(kāi)串行監(jiān)視器。串行監(jiān)視器將顯示IP地址,如下圖所示。在瀏覽器中輸入此IP地址。
輸入IP地址后,網(wǎng)頁(yè)將如下所示。
-
Web服務(wù)器
+關(guān)注
關(guān)注
0文章
137瀏覽量
24356 -
ESP32
+關(guān)注
關(guān)注
17文章
951瀏覽量
16992
發(fā)布評(píng)論請(qǐng)先 登錄
相關(guān)推薦
評(píng)論