629浏览
查看: 629|回复: 0

[Micropython] FireBeetle 2 ESP32-S3 Micropython webserver

[复制链接]
本帖最后由 米菲爸爸 于 2023-10-20 16:13 编辑

FireBeetle-ESP32-S3是一款基于ESP32-S3的开发板,支持MicroPython编程。要实现FireBeetle-ESP32-S3上的Web服务器功能,可以遵循以下步骤:

1. 连接到Wi-Fi网络:在FireBeetle-ESP32-S3上使用`network`库连接到您的Wi-Fi网络。您需要提供Wi-Fi的SSID和密码。
import network

# 连接到Wi-Fi网络
def connect_to_wifi(ssid, password):
    wlan = network.WLAN(network.STA_IF)
    wlan.active(True)

    if not wlan.isconnected():
        print("Connecting to WiFi...")
        wlan.connect(ssid, password)

        while not wlan.isconnected():
            pass

    print("Connected to WiFi")
    print("IP address:", wlan.ifconfig()[0])

# WiFi配置
wifi_ssid = "Your_WiFi_SSID"
wifi_password = "Your_WiFi_Password"

# 连接到Wi-Fi网络
connect_to_wifi(wifi_ssid, wifi_password)

当然这个wifi用户名密码使用明文不是很好,我们把他们放在配置文件config.json里
import network
import ujson as json

# 从JSON配置文件中读取WiFi的SSID和密码
def read_wifi_config(file_path):
    with open(file_path, "r") as f:
        config = json.load(f)
        ssid = config["wifi_ssid"]
        password = config["wifi_password"]
    return ssid, password

# 连接到WLAN
def connect_to_wifi(ssid, password):
    wlan = network.WLAN(network.STA_IF)
    wlan.active(True)

    if not wlan.isconnected():
        print("Connecting to WiFi...")
        wlan.connect(ssid, password)

        while not wlan.isconnected():
            pass

    print("Connected to WiFi")
    print("IP address:", wlan.ifconfig()[0])

# JSON配置文件路径
config_file = "config.json"

# 读取WiFi配置
wifi_ssid, wifi_password = read_wifi_config(config_file)

# 连接到WLAN并获取IP地址
connect_to_wifi(wifi_ssid, wifi_password)

config.json 样例
{
  "wifi_ssid": "MiffyDad",
  "wifi_password": "xxxxxxx"
}

2. 创建Web服务器:
这里我们的web基于此工程
https://github.com/lemariva/uPyCam/blob/master/webserver.py
3. 处理HTTP请求:在处理HTTP请求时,您可以根据请求的URL路径和方法执行相应的操作。例如,如果请求的路径是`/`,您可以返回主页的HTML内容;如果请求的路径是`/api/data`,您可以返回一些JSON数据。

        self.routeHandlers = [
            ("/", "GET", self._httpHello),
            ("/index", "GET", self._httpHandlerIndex),
            ("/logo.svg", "GET", self._httpLogo),
            ...其他可按需书写
        ]

4. 发送HTTP响应:根据请求处理的结果,您可以构建适当的HTTP响应并发送回客户端。响应可以包括状态码、响应头和响应正文。

下面是几个简单的示例代码:

文字响应
    def _httpHello(self, httpClient, httpResponse):
        response = "HTTP/1.0 200 OK\r\n"
        httpResponse.WriteResponseOk(headers=None,
                                    contentType="text/html",
                                    contentCharset="UTF-8",
                                    content=response)

FireBeetle 2 ESP32-S3 Micropython webserver图1

html 页面相应
    def _httpHandlerIndex(self, httpClient, httpResponse):
        f = open("www/index.html", "r")
        content =  f.read()
        f.close()

        headers = { 'Last-Modified' : 'Thu, 7 Sep 2023 10:00:00 GMT', \
                            'Cache-Control' : 'no-cache, no-store, must-revalidate' }

        httpResponse.WriteResponseOk(headers=None,
                                    contentType="text/html",
                                    contentCharset="UTF-8",
                                    content=content)
FireBeetle 2 ESP32-S3 Micropython webserver图2

SVG文件相应(也可以是其他文件响应)
    def _httpLogo(self, httpClient, httpResponse):
        f = open("www/Logo.svg", "r")
        content =  f.read()
        f.close()

        httpResponse.WriteResponseOk(headers=None,
                                    contentType="image/svg+xml",
                                    contentCharset="UTF-8",

                                    content=content)
FireBeetle 2 ESP32-S3 Micropython webserver图3

启动webserver
server = webcam()
server.run()

因为是后台线程启动,所以这里是有提示符显示的,但是后台程序依然在运行。这一点新手要注意
FireBeetle 2 ESP32-S3 Micropython webserver图4

在下面的文章中我将继续探索FireBeetle 2 ESP32-S3摄像头在Micropython中的使用。关于这个在网上的资料极少,所以我只能缓步前行。

谢谢大家



您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

为本项目制作心愿单
购买心愿单
心愿单 编辑
[[wsData.name]]

硬件清单

  • [[d.name]]
btnicon
我也要做!
点击进入购买页面
上海智位机器人股份有限公司 沪ICP备09038501号-4

© 2013-2024 Comsenz Inc. Powered by Discuz! X3.4 Licensed

mail