ESP32进程开启http和webSocket服务
ESP32进程开启http和webSocket服务意义
在esp32上同时开启http(用来提供html页面),和webSocket(用来接受数据,控制芯片引脚)
代码环境
esp32+Micropython
实现代码
import usocket as socket
import network
import time
import ubinascii
import hashlib
import struct
import _thread
# 连接到Wi-Fi
ssid = 'your_SSID'
password = 'your_PASSWORD'
station = network.WLAN(network.STA_IF)
station.active(True)
station.connect(ssid, password)
while not station.isconnected():
time.sleep(1)
print('Connection successful')
print(station.ifconfig())
# WebSocket服务器
def websocket_server():
addr = socket.getaddrinfo('0.0.0.0', 8080)[0][-1]
s = socket.socket()
s.bind(addr)
s.listen(1)
print('WebSocket Server Listening on', addr)
# WebSocket握手过程
def websocket_handshake(client):
request = clie