esp32 运行其他服务的时候 单独开一个线程 监听80端口

可以将HTTP服务器部分放在一个单独的线程中运行,这样就可以同时执行其他代码

import usocket as socket
import _thread

def handle_request(client_socket):
    # 处理HTTP请求
    request_data = client_socket.recv(1024)
    request_lines = request_data.split(b"\r\n")
    if len(request_lines) > 0:
        request_line = request_lines[0].decode("utf-8")
        method, path, protocol = request_line.split(" ")
        if method == "GET" and path == "/":
            response_body = "Hello from MicroPython HTTP server!"
            response = (
                "HTTP/1.1 200 OK\r\n"
                "Content-Type: text/plain\r\n"
                "Content-Length: {}\r\n"
                "\r\n"
                "{}"
            ).format(len(response_body), response_body)
            client_socket.send(response.encode("utf-8"))
    client_socket.close()

def start_server():
    server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    server_socket.bind(('0.0.0.0', 80))
    server_socket.listen(5)
    print("Server is listening on port 80...")

    while True:
        client_socket, addr = server_socket.accept()
        print("Connection from:", addr)
        _thread.start_new_thread(handle_request, (client_socket,))

def main():
    # 在这里执行其他你需要的代码
    pass

if __name__ == "__main__":
    _thread.start_new_thread(start_server, ())
    main()

优化public读取


def handle_request(client_socket):
    # 处理HTTP请求
    request_data = client_socket.recv(1024)
    request_lines = request_data.split(b"\r\n")
    if len(request_lines) > 0:
        request_line = request_lines[0].decode("utf-8")
        method, path, protocol = request_line.split(" ")
        if method == "GET" and path == "/":
            with open("public/index.html", "rb") as f:
                response_body = f.read()
            response = (
                "HTTP/1.1 200 OK\r\n"
                "Content-Type: text/html\r\n"
                "Content-Length: {}\r\n"
                "\r\n"
            ).format(len(response_body))
            client_socket.send(response.encode("utf-8"))
            client_socket.send(response_body)
        elif path == "/xuanzhuan1":
            # 调用 xuanzhuan1 方法
            xuanzhuan1()
            response = "HTTP/1.1 200 OK\r\n\r\n"
            client_socket.send(response.encode("utf-8"))
        elif path == "/xuanzhuan2":
            # 调用 xuanzhuan2 方法
            xuanzhuan2()
            response = "HTTP/1.1 200 OK\r\n\r\n"
            client_socket.send(response.encode("utf-8"))

    client_socket.close()

def start_server():
    server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    server_socket.bind(('0.0.0.0', 80))
    server_socket.listen(5)
    print("Server is listening on port 80...") 
    while True:
        client_socket, addr = server_socket.accept()
        print("Connection from:", addr)
        _thread.start_new_thread(handle_request, (client_socket,))

HTTP服务器部分被放在一个名为start_server的函数中,并且使用_thread.start_new_thread来启动一个新的线程来运行HTTP服务器。这样,当程序执行到main()时,HTTP服务器将会在单独的线程中运行,而你可以在main()函数中执行其他需要的代码。

html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Door Control Panel</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            background-color: #f0f0f0;
            margin: 0;
            padding: 0;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
        }
        .container {
            text-align: center;
        }
        button {
            padding: 10px 20px;
            font-size: 16px;
            border: none;
            border-radius: 5px;
            background-color: #4caf50;
            color: white;
            cursor: pointer;
            margin: 10px;
            transition: background-color 0.3s;
        }
        button:hover {
            background-color: #45a049;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>Door Control Panel</h1>
        <button onclick="sendCommand('xuanzhuan1')">Open the Door (Servo to 0)</button>
        <button onclick="sendCommand('xuanzhuan2')">Close the Door (Servo to 180)</button>
    </div>

    <script>
        function sendCommand(command) {
            var xhr = new XMLHttpRequest();
            xhr.open("GET", "/" + command, true);
            xhr.send();
        }
    </script>
</body>
</html>