Hello, World!
%s
""" # Wait for Wi‑Fi connection or timeout max_wait = 10 while max_wait > 0: if wlan.status() < 0 or wlan.status() >= 3: # Error or connected break max_wait -= 1 print('waiting for connection...') time.sleep(1) # If not connected, stop the program if wlan.status() != 3: raise RuntimeError('network connection failed') else: print('Connected') status = wlan.ifconfig() # Get IP configuration print('ip = ' + status[0]) # Print IP address # Create a listening TCP socket on port 80 (HTTP) addr = socket.getaddrinfo('0.0.0.0', 80)[0][-1] # Bind to all interfaces s = socket.socket() # Create socket s.bind(addr) # Bind to address/port s.listen(1) # Listen for connections print('listening on', addr) # Main server loop while True: try: cl, addr = s.accept() # Accept a client connection print('client connected from', addr) request = cl.recv(1024) # Read HTTP request print("request:") print(request) request = str(request) # Convert bytes → string # Look for URL parameters: ?led=on or ?led=off led_on = request.find('led=on') led_off = request.find('led=off') print('led on = ' + str(led_on)) print('led off = ' + str(led_off)) # If found at position 8 (typical for GET requests) if led_on == 8: print("led on") led.value(1) if led_off == 8: print("led off") led.value(0) # Update LED state text ledState = "LED is OFF" if led.value() == 0 else "LED is ON" # Read button state if button.value() == 1: # Button not pressed (pull‑up = HIGH) print("button NOT pressed") buttonState = "Button is NOT pressed" led.value(0) # Force LED OFF else: print("button pressed") buttonState = "Button is pressed" led.value(1) # Force LED ON # Build webpage content stateis = ledState + " and " + buttonState response = html % stateis # Insert text into HTML template # Send HTTP response cl.send('HTTP/1.0 200 OK\r\nContent-type: text/html\r\n\r\n') cl.send(response) cl.close() # Close connection except OSError as e: cl.close() print('connection closed') Before you attempt to run this code, look at the top of the code, lines 14 and 15. \\ ssid = 'YourSSID' password = 'YourWifiPassword' You need to enter your wifi details here, then you can run the code. \\ \\ I suspect the one thing that will cause issues for you will be connecting to the Wifi, so take your time and get this right. \\ \\ ---- === Running the Code === When you run the code, it will initially do three things: \\ - Connect to your SSID - Login using your credentials - Get an IP Address Once all three of these criteria are met, you will see the IP Address in the bottom window of Thonny. \\ \\Hello, World!
%s
""" What we need to do, is to replace the lines above, with the following block of code: \\%s
"""
\\
All we have to do now is to refresh the webpage (check your IP has not changed).
\\
\\
{{ :button-led-pico-007.png?800 |}}
\\
You can now change the LED status on or off by simply clicking the buttons on the webpage, clicking either button also refreshed the webpage. This means, if you hold the button down (or not) and click a button to change the LED, the LED status will change, and the text at the bottom of the webpage will also change to reflect the button status.
\\
\\
----
==== Full Code ====
%s
"""
# Wait for connect or fail
max_wait = 10
while max_wait > 0:
if wlan.status() < 0 or wlan.status() >= 3:
break
max_wait -= 1
print('waiting for connection...')
time.sleep(1)
# Handle connection error
if wlan.status() != 3:
raise RuntimeError('network connection failed')
else:
print('Connected')
status = wlan.ifconfig()
print( 'ip = ' + status[0] )
# Open socket
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, serve client
while True:
try:
cl, addr = s.accept()
print('client connected from', addr)
request = cl.recv(1024)
print("request:")
print(request)
request = str(request)
led_on = request.find('led=on')
led_off = request.find('led=off')
print( 'led on = ' + str(led_on))
print( 'led off = ' + str(led_off))
if led_on == 8:
print("led on")
led.value(1)
if led_off == 8:
print("led off")
led.value(0)
ledState = "LED is OFF" if led.value() == 0 else "LED is ON" # a compact if-else statement
if button.value() == 1: # button not pressed
print("button NOT pressed")
buttonState = "Button is NOT pressed"
else:
print("button pressed")
buttonState = "Button is pressed"
# Create and send response
stateis = ledState + " and " + buttonState
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')
----Control Panel