Reputation: 1
I have a soft access point (hotspot) without internet connection set up on my Raspberry Pi Pico W see the code below:
# 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)
Special thanks to Alek V for helping me with this code.
Now what I want is to be able to join the network with my phone and go to 192.168.4.1:80 in my web browser. Now I tried getting the web server to work using a module called wgiserver, but I am not that good at CircuitPython and it just doesn't work.
Does anybody know how to set up a web server using CircuitPython?
Upvotes: 0
Views: 928
Reputation: 436
Try this code:
wifi.radio.connect(
ssid=os.getenv("CIRCUITPY_MY_WIFI_SSID"),
password=os.getenv("CIRCUITPY_MY_WIFI_PASSWORD"),
)
mdns_server = mdns.Server(wifi.radio)
print("MDNS Hostname: " + mdns_server.hostname)
mdns_server.hostname = "HOSTNAME"
mdns_server.advertise_service(service_type="_http", protocol="_tcp", port=80)
pool = socketpool.SocketPool(wifi.radio)
server = Server(pool, "/static", debug=True)
@server.route("/<path:path>", GET)
def static(request: Request, path: str):
"""
Serve files from the /static directory.
"""
return FileResponse(request, path, "/static")
server.serve_forever(str(wifi.radio.ipv4_address))
Put your static files inside a folder named "static" and you should be able to see them at "HOSTNAME".local
Upvotes: 0