给我写个esp32接线控制电脑开机
时间: 2023-11-17 17:06:46 浏览: 26
以下是ESP32接线控制电脑开机的代码:
```
#include <WiFi.h>
const char* ssid = "YOUR_SSID"; //修改为你的Wi-Fi名称
const char* password = "YOUR_PASSWORD"; //修改为你的Wi-Fi密码
WiFiServer server(80);
void setup() {
Serial.begin(115200);
pinMode(2, OUTPUT);
digitalWrite(2, LOW);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
server.begin();
Serial.println("Server started");
}
void loop() {
WiFiClient client = server.available();
if (client) {
Serial.println("New client connected");
String currentLine = "";
while (client.connected()) {
if (client.available()) {
char c = client.read();
Serial.write(c);
if (c == '\n') {
if (currentLine.endsWith("GET /on")) {
digitalWrite(2, HIGH);
delay(1000);
digitalWrite(2, LOW);
}
client.println("HTTP/1.1 200 OK");
client.println("Content-type:text/html");
client.println();
client.println("<html><body><h1>ESP32 Remote Control</h1><p><a href=\"/on\">Turn on computer</a></p></body></html>");
break;
} else if (c == '\r') {
// ignore
} else {
currentLine += c;
}
}
}
client.stop();
Serial.println("Client disconnected");
}
}
```
需要将ESP32的GPIO2引脚连接到电脑电源开关的控制线上,以便控制电脑的开关。在浏览器中访问ESP32的IP地址,点击“Turn on computer”按钮即可打开电脑。
相关推荐
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)