import network import urequests import time from machine import Pin, PWM # Wi-Fi Credentials wifi_options = [ {"ssid": "Aluminum_Flat", "password": "HowLargeAreYourHands"}, {"ssid": "iPhone SM", "password": "lalalala"} ] # Function to connect to Wi-Fi def connect_to_wifi(options): wlan = network.WLAN(network.STA_IF) wlan.active(True) for option in options: print(f"Trying to connect to {option['ssid']}...") wlan.connect(option['ssid'], option['password']) for _ in range(10): # Retry for 10 seconds if wlan.isconnected(): print(f"Connected to {option['ssid']}! IP:", wlan.ifconfig()) return wlan time.sleep(1) print(f"Failed to connect to {option['ssid']}.") return None # Attempt Wi-Fi connection wlan = connect_to_wifi(wifi_options) if not wlan: print("Failed to connect to any Wi-Fi network.") raise RuntimeError("Wi-Fi connection failed") # Configure onboard LED led = Pin("LED", Pin.OUT) # Configure servo on GPIO 15 servo = PWM(Pin(15)) servo.freq(50) # Standard servo frequency: 50 Hz # Function to set servo position def set_servo_angle(angle): # Ensure the angle is an integer and within valid range if not isinstance(angle, int) or not (0 <= angle <= 165): print(f"Invalid angle: {angle}. Must be an integer between 0 and 165.") return # Convert angle (0-180) to duty cycle (2000-9000 for 50Hz PWM) duty = int(2000 + (angle / 180) * 7000) servo.duty_u16(duty) # URL of the Flask app signal_url = "https://sergio.pythonanywhere.com/" while True: try: # Fetch the HTML content from the Flask app response = urequests.get(signal_url) html_content = response.text response.close() # Debugging output for HTML content print("Fetched HTML Content:", html_content) # Parse LED state led_state_marker = '