安装头文件
![请添加图片描述](https://i-blog.csdnimg.cn/direct/a26147e1fde241acbf95dc3f3fc60c92.png)
添加头文件
#include <ESP8266WiFi.h>
#include <WiFiServer.h>
#include <Servo.h>
初始化串口
Serial.begin(115200);
初始化舵机
Servo myservo;
myservo.attach(D4);
myservo.write(90);
初始化wifi模块
const char* ssid = "led_control";
const char* password = "12345678";
WiFiServer server(80);
WiFi.mode(WIFI_AP);
WiFi.softAP(ssid, password);
完整代码
#include <ESP8266WiFi.h>
#include <WiFiServer.h>
#include <Servo.h>
const char* ssid = "led_control";
const char* password = "12345678";
Servo myservo;
WiFiServer server(80);
void setup() {
myservo.attach(D4);
myservo.write(90);
Serial.begin(115200);
WiFi.mode(WIFI_AP);
WiFi.softAP(ssid, password);
server.begin();
Serial.println();
Serial.println("Server started");
}
void loop() {
WiFiClient client = server.available();
if (client) {
Serial.println("New Client.");
while(client.connected()) {
if (client.available()) {
String data = client.readStringUntil('\n');
Serial.print("Received: ");
Serial.println(data);
if (data == "open") {
Serial.println("led open");
myservo.write(0);
delay(1000);
myservo.write(90);
}
if (data == "close") {
Serial.println("led close");
myservo.write(180);
delay(1000);
myservo.write(90);
}
}
}
client.stop();
Serial.println("Client disconnected.");
delay(1);
}
}