This program turns your Pico W into a small web server. The web page has two links on it. One link will turn the on-board LED on and the other link will turn the LED off.
# Code taken from https://www.cnx-software.com/2022/07/03/getting-started-with-wifi-on-raspberry-pi-pico-w-board/importnetworkimportsocketimporttimeimportsecretsfrommachineimportPin# Select the onboard LEDled=machine.Pin("LED",machine.Pin.OUT)wlan=network.WLAN(network.STA_IF)wlan.active(True)wlan.connect(secrets.SSID,secrets.PASSWORD)stateis="LED is OFF"html="""<!DOCTYPE html><html> <head> <title>Web Server On Pico W </title> </head> <body> <h1>Pico Wireless Web Server</h1> <p>%s</p> <a href="/light/on">Turn On</a> <a href="/light/off">Turn Off</a> </body></html>"""# Wait for connect or failmax_wait=10whilemax_wait>0:ifwlan.status()<0orwlan.status()>=3:breakmax_wait-=1print('waiting for connection...')time.sleep(1)# Handle connection errorifwlan.status()!=3:raiseRuntimeError('network connection failed')else:print('We are connected to WiFI access point:',secrets.SSID)status=wlan.ifconfig()print('The IP address of the pico W is:',status[0])# Open socketaddr=socket.getaddrinfo('0.0.0.0',80)[0][-1]print('addr:',addr)s=socket.socket()#if not addr:s.bind(addr)s.listen(1)print('listening on',addr)# Listen for connectionswhileTrue:try:cl,addr=s.accept()print('client connected from',addr)request=cl.recv(1024)print(request)request=str(request)led_on=request.find('/light/on')led_off=request.find('/light/off')print('led on = '+str(led_on))print('led off = '+str(led_off))ifled_on==6:print("led on")led.value(1)stateis="LED is ON"ifled_off==6:print("led off")led.value(0)stateis="LED is OFF"# generate the we page with the stateis as a parameterresponse=html%stateiscl.send('HTTP/1.0 200 OK\r\nContent-type: text/html\r\n\r\n')cl.send(response)cl.close()exceptOSErrorase:cl.close()print('connection closed')