Arduino D1/Esp8266 web page controls servo rotation and reset - realizing smart switch

Use Arduino D1 and ESP8266 modules to build a simple web page program to control the rotation of the servo. By building a web server-based system, you can control the rotation of the servo by clicking a button by accessing the IP address of the Arduino D1. This article will detail the required hardware and libraries, as well as the code implementation steps.

1. Required hardware and libraries:

Arduino D1 development board
ESP8266 module
steering gear
DuPont cable

2. You need to use the following libraries:

ESP8266WiFi library: used to connect to WiFi networks
ESP8266WebServer library: used to create a web server
Servo library: used to control the servo

In the setup() function, set the servo pin to output mode and attach the servo object to the pin. Then, use the WiFi.begin() function to connect to the WiFi network and use a while loop to wait for the connection to succeed. Initialize the Web server object, set the processing functions of the root path and servo control path, and call server.begin() to start the server.
In the loop() function, use server.handleClient() to handle client requests.

Upload the program to the Arduino D1 board and connect to the corresponding WiFi network. By accessing the Arduino D1's IP address, you will see a page containing control buttons. Click the button and the servo will rotate to a specific angle. Click the button again and the servo will return to the original angle.

3. We can see the IP address on the serial monitor of the Arduino IDE. Mine is 192.168.137.40. The specific situation depends on the actual situation.

IP address

4. Then enter the IP address in the serial port on the browser, and you will see the control interface of the servo.

control interface

5. Code, upload program

#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <Servo.h>

const char* ssid = "3199999";
const char* password = "5201314520";

const int servoPin = D2; // 控制舵机的引脚
Servo servo; // 舵机对象

ESP8266WebServer server(80); // 创建Web服务器,监听端口80

const int initialAngle = 30; // 初始角度值
const int rotatedAngle = 180; // 旋转角度值
bool isRotated = false; // 是否旋转标志

void handleRoot() {
    
    
  String buttonText = isRotated ? "Restore" : "rotate";
  String html = "<html>"
                "<body>"
                "<h1>Servo Control</h1>"
                "<form action=\"/Servo\" method=\"POST\">"
                "<input type=\"submit\" value=\"" + buttonText + "\">"
                "</form>"
                "</body>"
                "</html>";
  server.send(200, "text/html", html);
}

void handleServo() {
    
    
  isRotated = !isRotated; // 切换旋转状态

  if (isRotated) {
    
    
    servo.write(rotatedAngle); // 旋转到指定角度
  } else {
    
    
    servo.write(initialAngle); // 恢复到初始角度
  }

  server.sendHeader("Location", "/");
  server.send(303);
}

void setup() {
    
    
  pinMode(servoPin, OUTPUT); // 将舵机引脚设置为输出模式
  servo.attach(servoPin); // 将舵机对象附加到舵机引脚

  Serial.begin(115200);

  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    
    
    delay(1000);
    Serial.println("Connecting to WiFi...");
  }

  Serial.println("WiFi connected");
  Serial.println("IP address: " + WiFi.localIP().toString());

  server.on("/", handleRoot);
  server.on("/Servo", handleServo);

  server.begin();
  Serial.println("Server started");

  servo.write(initialAngle); // 初始角度
}

void loop() {
    
    
  server.handleClient();
}

Then you can control the rotation of the servo through the web page! !

Guess you like

Origin blog.csdn.net/m0_63715549/article/details/131744057