# Rui Santos & Sara Santos - Random Nerd Tutorials # Complete project details at https://RandomNerdTutorials.com/raspberry-pi-pico-w-asynchronous-web-server-micropython/ # Import necessary modules import network import asyncio import socket import time import random from machine import Pin # Wi-Fi credentials ssid = 'REPLACE_WITH_YOUR_SSID' password = 'REPLACE_WITH_YOUR_PASSWORD' # Create several LEDs led_blink = Pin(20, Pin.OUT) led_control = Pin(19, Pin.OUT) # Initialize variables state = "OFF" random_value = 0 # HTML template for the webpage def webpage(random_value, state): html = f"""
LED state: {state}
Fetched value: {random_value}
""" return str(html) # Init Wi-Fi Interface def init_wifi(ssid, password): wlan = network.WLAN(network.STA_IF) wlan.active(True) # Connect to your network wlan.connect(ssid, password) # Wait for Wi-Fi connection connection_timeout = 10 while connection_timeout > 0: print(wlan.status()) if wlan.status() >= 3: break connection_timeout -= 1 print('Waiting for Wi-Fi connection...') time.sleep(1) # Check if connection is successful if wlan.status() != 3: print('Failed to connect to Wi-Fi') return False else: print('Connection successful!') network_info = wlan.ifconfig() print('IP address:', network_info[0]) return True # Asynchronous functio to handle client's requests async def handle_client(reader, writer): global state print("Client connected") request_line = await reader.readline() print('Request:', request_line) # Skip HTTP request headers while await reader.readline() != b"\r\n": pass request = str(request_line, 'utf-8').split()[1] print('Request:', request) # Process the request and update variables if request == '/lighton?': print('LED on') led_control.value(1) state = 'ON' elif request == '/lightoff?': print('LED off') led_control.value(0) state = 'OFF' elif request == '/value?': global random_value random_value = random.randint(0, 20) # Generate HTML response response = webpage(random_value, state) # Send the HTTP response and close the connection writer.write('HTTP/1.0 200 OK\r\nContent-type: text/html\r\n\r\n') writer.write(response) await writer.drain() await writer.wait_closed() print('Client Disconnected') async def blink_led(): while True: led_blink.toggle() # Toggle LED state await asyncio.sleep(0.5) # Blink interval async def main(): if not init_wifi(ssid, password): print('Exiting program.') return # Start the server and run the event loop print('Setting up server') server = asyncio.start_server(handle_client, "0.0.0.0", 80) asyncio.create_task(server) asyncio.create_task(blink_led()) while True: # Add other tasks that you might need to do in the loop await asyncio.sleep(5) print('This message will be printed every 5 seconds') # Create an Event Loop loop = asyncio.get_event_loop() # Create a task to run the main function loop.create_task(main()) try: # Run the event loop indefinitely loop.run_forever() except Exception as e: print('Error occurred: ', e) except KeyboardInterrupt: print('Program Interrupted by the user')