释放双眼,带上耳机,听听看~!
了解如何创建网页来控制带有ESP32的继电器模块。在本项目中,我们将使用ESP32控制继电器模块。我们将ESP32连接到Wi-Fi网络,然后通过IP地址和端口(显示在串行监视器上)创建一个网页。使用该网页,我们会将命令发送到ESP32以打开或关闭继电器模块。
目录
目录
硬件清单
- 1个 ESP32
- 1个 继电器模块
- 1个 连接线
电路原理图
代码说明
让我们遍历代码的不同方面以及代码的每个部分查看都负责做什么。完整的代码,可以在本文结尾处找到。
首先,我们包括Wi-Fi库,以便我们可以连接到Internet。然后,我们存储Wi-Fi名称和密码,以便我们可以连接到该网络。之后,我们定义用于创建Web服务器的端口。
const char* wifi_name = "Tenda_31BC98"; // Your Wifi network name here
const char* wifi_pass = "barcelona"; // Your Wifi network password here
WiFiServer server(80); // Server will be at port 80
在设置功能中,我们使用上面提供的信息连接到Wi-Fi网络。如果与Wi-Fi网络的连接成功,则串行监视器上将显示“连接成功”。如果与Wi-Fi网络的连接不成功,则它将继续尝试直到连接至Wi-Fi网络。
WiFi.begin(wifi_name, wifi_pass); // Connecting to the wifi network
while (WiFi.status() != WL_CONNECTED) // Waiting for the response of wifi network
{
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("Connection Successful");
下面的命令将在串行监视器上显示IP地址。这是将在我们的服务器上/通过它创建的IP地址。
Serial.println(WiFi.localIP()); // Getting the IP address下面的命令将启动服务器。
server.begin(); // Starting the server
Serial.println(WiFi.localIP()); // Getting the IP address下面的命令将启动服务器。
server.begin(); // Starting the server
在循环功能中,我们首先检查是否有任何客户端请求可用。如果有任何客户端请求可用,则我们将读取请求并将其存储在字符中。我们通过发送HTML命令来响应客户请求,这些命令创建了可以控制中继的网页。
if (client)
{
boolean currentLineIsBlank = true;
String buffer = "";
while (client.connected())
{
if (client.available()) // if there is some client data available
{
char c = client.read();
buffer+=c; // read a byte
if (c == 'n' && currentLineIsBlank) // check for newline character,
{
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println();
client.print("");
client.print("
ESP32 Standalone Relay Control
");
client.print("
Relay Control
");
client.print("ON");
client.print("OFF");
client.print("");
break; // break out of the while loop:
}
}
}
完整的项目代码
#include <WiFi.h>
const char* wifi_name = "Tenda_31BC98"; // Your Wifi network name here
const char* wifi_pass = "barcelona"; // Your Wifi network password here
WiFiServer server(80); // Server will be at port 80
int relay_pin = 2;
void setup()
{
Serial.begin (115200);
pinMode (relay_pin, OUTPUT);
Serial.print ("Connecting to ");
Serial.print (wifi_name);
WiFi.begin (wifi_name, wifi_pass); // Connecting to the wifi network
while (WiFi.status() != WL_CONNECTED) // Waiting for the response 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
Serial.println("Type the above IP address into browser search bar");
server.begin(); // Starting the server
}
void loop()
{
WiFiClient client = server.available(); //Checking if any client request is available or not
if (client)
{
boolean currentLineIsBlank = true;
String buffer = "";
while (client.connected())
{
if (client.available()) // if there is some client data available
{
char c = client.read();
buffer += c; // read a byte
if (c == 'n' && currentLineIsBlank) // check for newline character,
{
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println();
client.print("");
client.print("
ESP32 Standalone Relay Control
");
client.print("
Relay Control
");
client.print("ON");
client.print("OFF");
client.print("");
break; // break out of the while loop:
}
if (c == 'n') {
currentLineIsBlank = true;
buffer = "";
}
else
if (c == 'r') {
if (buffer.indexOf("GET /?relayon") >= 0)
digitalWrite(relay_pin, LOW);
if (buffer.indexOf("GET /?relayoff") >= 0)
digitalWrite(relay_pin, HIGH);
}
else {
currentLineIsBlank = false;
}
}
}
client.stop();
}
}
q求代码
OK
666