import socket
import network
import time
from machine import Pin
# Wi-Fi credentials
ssid = "YourOwnSSID" # Replace with your SSID
password = "YourOwnPassword" # Replace with your password
# LED setup
led = Pin(15, Pin.OUT)
# Configure the Pico W as an Access Point (Soft AP Mode)
ap = network.WLAN(network.AP_IF)
ap.config(essid=ssid, password=password)
ap.active(True)
# Wait until the AP is active
while not ap.active():
pass
print('Connection successful')
print('Access Point Config:', ap.ifconfig())
# HTML template for the web page
html = """<!DOCTYPE html>
<html>
<head>
<title>EMJ47703</title>
</head>
<body>
<h1>EMJ47703 IoT and Data Analytics</h1>
<form action="/on">
<input type="submit" value="ON" />
</form>
<form action="/off">
<input type="submit" value="OFF" />
</form>
<p>%s</p>
</body>
</html>
"""
# Start the web server
addr = socket.getaddrinfo("0.0.0.0", 80)[0][-1]
s = socket.socket()
s.bind(addr)
s.listen(1)
print('Listening on', addr)
# Listen for connections
while True:
try:
cl, addr = s.accept()
print('Client connected from', addr)
request = cl.recv(1024)
print("Request:", request)
# Decode the request
request = str(request)
led_on = request.find('/on')
led_off = request.find('/off')
# LED control
if led_on != -1:
print("LED ON")
led.value(1)
stateis = "LED is ON"
elif led_off != -1:
print("LED OFF")
led.value(0)
stateis = "LED is OFF"
else:
stateis = "LED state unchanged"
# Send response
response = html % stateis
cl.send('HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n')
cl.send(response)
cl.close()
except OSError as e:
cl.close()
print("Connection closed")