Skip to content

Instantly share code, notes, and snippets.

@aallan
Created July 7, 2022 13:43
Show Gist options
  • Save aallan/447c3bf43faf2c82e8bd4afdea9eb63e to your computer and use it in GitHub Desktop.
Save aallan/447c3bf43faf2c82e8bd4afdea9eb63e to your computer and use it in GitHub Desktop.
Running a web server on an wireless Access Point for Raspberry Pi Pico W in MicroPython
import socket
import network
import machine
ssid = 'MicroPython-AP'
password = '123456789'
led = machine.Pin("LED",machine.Pin.OUT)
ap = network.WLAN(network.AP_IF)
ap.config(essid=ssid, password=password)
ap.active(True)
while ap.active() == False:
pass
print('Connection successful')
print(ap.ifconfig())
html = """<!DOCTYPE html>
<html>
<head> <title>Pico W</title> </head>
<body> <h1>Pico W</h1>
<p>Hello from Pico W.</p>
</body>
</html>
"""
addr = socket.getaddrinfo('0.0.0.0', 80)[0][-1]
s = socket.socket()
s.bind(addr)
s.listen(1)
print('listening on', addr)
led.off()
# Listen for connections
while True:
try:
cl, addr = s.accept()
print('client connected from', addr)
request = cl.recv(1024)
led.on()
print(request)
cl.send('HTTP/1.0 200 OK\r\nContent-type: text/html\r\n\r\n')
cl.send(html)
cl.close()
led.off()
except OSError as e:
cl.close()
print('connection closed')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment