如何在 CircuitPython 中创建具有软接入点的网络服务器?

问题描述 投票:0回答:1

我想创建一个网络服务器,仅使用我的 raspberry pi pico w 提供基本的 index.html 文件。我可以在 micropython 中执行此操作,但我需要在该项目中使用 Circuitpython。我找到了一些代码来创建软访问点,但我无法使用该代码创建网络服务器。

页面链接:无法在 CircuitPython 中设置软接入点

我找到的代码:

# import wifi module
import wifi

# set access point credentials
ap_ssid = "myAP"
ap_password = "password123"

# You may also need to enable the wifi radio with wifi.radio.enabled(true)

# configure access point
wifi.radio.start_ap(ssid=ap_ssid, password=ap_password)

"""
start_ap arguments include: ssid, password, channel, authmode, and max_connections
"""

# print access point settings
print("Access point created with SSID: {}, password: {}".format(ap_ssid, ap_password))

# print IP address
print("My IP address is", wifi.radio.ipv4_address)

我尝试添加网络服务器的代码:


import wifi
import socketpool

# Set access point credentials
ap_ssid = "myAP"
ap_password = "password123"

# Configure access point
wifi.radio.start_ap(ssid=ap_ssid, password=ap_password)

# Print access point settings
print("Access point created with SSID: {}, password: {}".format(ap_ssid, ap_password))
print("My IP address is", wifi.radio.ipv4_address)

# Create a socket pool
pool = socketpool.SocketPool(wifi.radio)

# Create a simple HTTP server function
def simple_http_server():
    server_socket = pool.socket()
    server_socket.bind((wifi.radio.ipv4_address, 80))
    server_socket.listen(1)

    print("Server is listening on {}:80".format(wifi.radio.ipv4_address))

    while True:
        print("Waiting for a connection...")
        client_socket, client_address = server_socket.accept()
        print("Accepted connection from:", client_address)

        client_socket.send("HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n")
        client_socket.send("<html>Hello, World!</html>\r\n")
        client_socket.close()

# Start the HTTP server
simple_http_server()

现在我得到的错误是:

Access point created with SSID: myAP, password: password123
My IP address is None
Traceback (most recent call last):
  File "<stdin>", line 36, in <module>
  File "<stdin>", line 21, in simple_http_server
TypeError: can't convert 'NoneType' object to str implicitly

我尝试使用 chatgpt 来帮助我解决这个问题,因为我不熟悉 CircuitPython,但即使他也无法弄清楚,它一直希望我使用 adafruit_requests 作为模块,但我没有该模块。

欢迎所有帮助。预先感谢!

webserver raspberry-pi-pico adafruit-circuitpython
1个回答
0
投票

发生这种情况的原因是因为

wifi.radio.ipv4_address
仅在连接到现有 AP 时才有效。如果您有自己的AP,则需要使用
wifi.radio.ipv4_address_ap

ipv4_address: ipaddress.IPv4Address

IP v4 连接到接入点时的站地址。否则没有。 (只读)
ipv4_subnet_ap: ipaddress.IPv4Address

IPv4 接入点子网的地址(启用后)。否则没有。 (只读) https://docs. Circuitpython.org/en/latest/shared-bindings/wifi/index.html#wifi.Radio

这是更新后的代码:

import wifi
import socketpool

# Set access point credentials
ap_ssid = "myAP"
ap_password = "password123"

# Configure access point
wifi.radio.start_ap(ssid=ap_ssid, password=ap_password)

# Print access point settings
print("Access point created with SSID: {}, password: {}".format(ap_ssid, ap_password))
print("My IP address is", str(wifi.radio.ipv4_address_ap))

# Create a socket pool
pool = socketpool.SocketPool(wifi.radio)

# Create a simple HTTP server function
def simple_http_server():
    server_socket = pool.socket()
    server_socket.bind((str(wifi.radio.ipv4_address_ap), 80))
    server_socket.listen(1)

    print("Server is listening on {}:80".format(wifi.radio.ipv4_address))

    while True:
        print("Waiting for a connection...")
        client_socket, client_address = server_socket.accept()
        print("Accepted connection from:", client_address)

        client_socket.send("HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n")
        client_socket.send("<html>Hello, World!</html>\r\n")
        client_socket.close()

# Start the HTTP server
simple_http_server()

注意: 确保在使用之前将

wifi.radio.ipv4_address_ap
转换为字符串,否则会出现此错误:
TypeError: can't convert 'Address' object to str implicitly

© www.soinside.com 2019 - 2024. All rights reserved.