请你帮我设计一个main.py文件,用于esp32 wifi 控制 2个独立的灯泡,2个独立的继电器,并且通过html控制相应的状态和显示按钮控制
时间: 2023-05-11 22:03:59 浏览: 126
好的,以下是一个简单的main.py文件,用于esp32 wifi控制2个独立的灯泡和2个独立的继电器,并且通过html控制相应的状态和显示按钮控制:
```python
import machine
import network
import socket
import time
# 设置wifi连接信息
WIFI_SSID = 'your_wifi_ssid'
WIFI_PASSWORD = 'your_wifi_password'
# 设置灯泡和继电器的引脚
LED_PIN_1 = 2
LED_PIN_2 = 4
RELAY_PIN_1 = 16
RELAY_PIN_2 = 17
# 初始化wifi连接
wifi = network.WLAN(network.STA_IF)
wifi.active(True)
wifi.connect(WIFI_SSID, WIFI_PASSWORD)
while not wifi.isconnected():
time.sleep(1)
# 初始化灯泡和继电器的引脚
led1 = machine.Pin(LED_PIN_1, machine.Pin.OUT)
led2 = machine.Pin(LED_PIN_2, machine.Pin.OUT)
relay1 = machine.Pin(RELAY_PIN_1, machine.Pin.OUT)
relay2 = machine.Pin(RELAY_PIN_2, machine.Pin.OUT)
# 创建socket服务器
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.bind(('', 80))
server_socket.listen(5)
# 处理http请求
def handle_request(client_socket):
request = client_socket.recv(1024)
request_str = request.decode('utf-8')
request_lines = request_str.split('\r\n')
request_method, request_path, request_protocol = request_lines[0].split(' ')
if request_path == '/':
# 返回html页面
response_body = '''
<html>
<head>
<title>ESP32控制页面</title>
</head>
<body>
<h1>ESP32控制页面</h1>
<form action="/led1">
<button>灯泡1</button>
</form>
<form action="/led2">
<button>灯泡2</button>
</form>
<form action="/relay1">
<button>继电器1</button>
</form>
<form action="/relay2">
<button>继电器2</button>
</form>
</body>
</html>
'''
response_header = 'HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nContent-Length: {}\r\n\r\n'.format(len(response_body))
response = response_header.encode('utf-8') + response_body.encode('utf-8')
client_socket.send(response)
elif request_path == '/led1':
# 控制灯泡1
led1.value(not led1.value())
response_header = 'HTTP/1.1 302 Found\r\nLocation: /\r\n\r\n'
response = response_header.encode('utf-8')
client_socket.send(response)
elif request_path == '/led2':
# 控制灯泡2
led2.value(not led2.value())
response_header = 'HTTP/1.1 302 Found\r\nLocation: /\r\n\r\n'
response = response_header.encode('utf-8')
client_socket.send(response)
elif request_path == '/relay1':
# 控制继电器1
relay1.value(not relay1.value())
response_header = 'HTTP/1.1 302 Found\r\nLocation: /\r\n\r\n'
response = response_header.encode('utf-8')
client_socket.send(response)
elif request_path == '/relay2':
# 控制继电器2
relay2.value(not relay2.value())
response_header = 'HTTP/1.1 302 Found\r\nLocation: /\r\n\r\n'
response = response_header.encode('utf-8')
client_socket.send(response)
else:
# 返回404页面
response_body = '404 Not Found'
response_header = 'HTTP/1.1 404 Not Found\r\nContent-Type: text/plain\r\nContent-Length: {}\r\n\r\n'.format(len(response_body))
response = response_header.encode('utf-8') + response_body.encode('utf-8')
client_socket.send(response)
# 循环处理http请求
while True:
client_socket, client_address = server_socket.accept()
handle_request(client_socket)
client_socket.close()
```
这个main.py文件使用了esp32的GPIO控制2个独立的灯泡和2个独立的继电器,并且通过http请求控制相应的状态和显示按钮控制。当访问http://esp32_ip/时,会返回一个简单的html页面,页面上有4个按钮,分别控制2个灯泡和2个继电器的开关状态。
阅读全文