turn_on_led_using_webpage
Differences
This shows you the differences between two versions of the page.
| Next revision | Previous revision | ||
| turn_on_led_using_webpage [2026/07/09 18:00] – created walkeradmin | turn_on_led_using_webpage [2026/07/09 19:07] (current) – walkeradmin | ||
|---|---|---|---|
| Line 6: | Line 6: | ||
| ---- | ---- | ||
| ===== Introduction ===== | ===== Introduction ===== | ||
| + | \\ | ||
| + | I got the idea for this from this webpage [[https:// | ||
| + | \\ | ||
| \\ | \\ | ||
| In this example, we are going to turn on an LED on our Raspberry Pi Pico using a simple web page with two buttons. There is a second function where we can display the state of a hardware button also. | In this example, we are going to turn on an LED on our Raspberry Pi Pico using a simple web page with two buttons. There is a second function where we can display the state of a hardware button also. | ||
| Line 25: | Line 28: | ||
| \\ | \\ | ||
| \\ | \\ | ||
| - | {{ :corporate: | + | {{ : |
| In this example, we have the LED being connected to GP14 (pin 19) and GND (pin 18) via a 100 ohm resistor. You can use one of the GND pins on the Pico, or a common GND rail if you have made one. | In this example, we have the LED being connected to GP14 (pin 19) and GND (pin 18) via a 100 ohm resistor. You can use one of the GND pins on the Pico, or a common GND rail if you have made one. | ||
| Line 67: | Line 71: | ||
| \\ | \\ | ||
| \\ | \\ | ||
| - | {{ :corporate: | + | {{ : |
| Looking at the simulation above, we can see that when the button is not pressed, we get a delay of 1000mS as per the MicroPython code, so 1 flash every two seconds (1s on, 1s off). | Looking at the simulation above, we can see that when the button is not pressed, we get a delay of 1000mS as per the MicroPython code, so 1 flash every two seconds (1s on, 1s off). | ||
| Line 240: | Line 244: | ||
| \\ | \\ | ||
| \\ | \\ | ||
| - | {{ :corporate: | + | {{ : |
| \\ | \\ | ||
| The page clearly states that the Button is NOT pressed, and the LED is off. | The page clearly states that the Button is NOT pressed, and the LED is off. | ||
| Line 248: | Line 252: | ||
| \\ | \\ | ||
| \\ | \\ | ||
| - | {{ :corporate: | + | {{ : |
| \\ | \\ | ||
| You should see now that the page states that the button IS pressed and that the LED is on. | You should see now that the page states that the button IS pressed and that the LED is on. | ||
| Line 302: | Line 306: | ||
| \\ | \\ | ||
| \\ | \\ | ||
| - | {{ :corporate: | + | {{ : |
| \\ | \\ | ||
| 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. | 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 ==== | ||
| + | |||
| + | < | ||
| + | # Simple HTTP Server Example | ||
| + | # Control an LED and read a Button using a web browser | ||
| + | |||
| + | import time | ||
| + | import network | ||
| + | import socket | ||
| + | from machine import Pin | ||
| + | |||
| + | led = Pin(14, Pin.OUT) | ||
| + | ledState = 'LED State Unknown' | ||
| + | |||
| + | button = Pin(16, Pin.IN, Pin.PULL_UP) | ||
| + | |||
| + | ssid = ' | ||
| + | password = ' | ||
| + | |||
| + | wlan = network.WLAN(network.STA_IF) | ||
| + | wlan.active(True) | ||
| + | wlan.connect(ssid, | ||
| + | |||
| + | # replace the " | ||
| + | html = """< | ||
| + | < | ||
| + | <link rel=" | ||
| + | < | ||
| + | .buttonGreen { background-color: | ||
| + | .buttonRed { background-color: | ||
| + | text-decoration: | ||
| + | </ | ||
| + | < | ||
| + | < | ||
| + | < | ||
| + | < | ||
| + | < | ||
| + | </ | ||
| + | < | ||
| + | < | ||
| + | < | ||
| + | """ | ||
| + | |||
| + | # 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(' | ||
| + | time.sleep(1) | ||
| + | | ||
| + | # Handle connection error | ||
| + | if wlan.status() != 3: | ||
| + | raise RuntimeError(' | ||
| + | else: | ||
| + | print(' | ||
| + | status = wlan.ifconfig() | ||
| + | print( 'ip = ' + status[0] ) | ||
| + | | ||
| + | | ||
| + | # Open socket | ||
| + | addr = socket.getaddrinfo(' | ||
| + | s = socket.socket() | ||
| + | s.bind(addr) | ||
| + | s.listen(1) | ||
| + | print(' | ||
| + | |||
| + | # Listen for connections, | ||
| + | while True: | ||
| + | try: | ||
| + | cl, addr = s.accept() | ||
| + | print(' | ||
| + | request = cl.recv(1024) | ||
| + | print(" | ||
| + | print(request) | ||
| + | request = str(request) | ||
| + | led_on = request.find(' | ||
| + | led_off = request.find(' | ||
| + | | ||
| + | print( 'led on = ' + str(led_on)) | ||
| + | print( 'led off = ' + str(led_off)) | ||
| + | | ||
| + | if led_on == 8: | ||
| + | print(" | ||
| + | led.value(1) | ||
| + | if led_off == 8: | ||
| + | print(" | ||
| + | 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(" | ||
| + | buttonState = " | ||
| + | else: | ||
| + | print(" | ||
| + | buttonState = " | ||
| + | | ||
| + | # Create and send response | ||
| + | stateis = ledState + " and " + buttonState | ||
| + | response = html % stateis | ||
| + | cl.send(' | ||
| + | cl.send(response) | ||
| + | cl.close() | ||
| + | | ||
| + | except OSError as e: | ||
| + | cl.close() | ||
| + | print(' | ||
| + | |||
| + | </ | ||
| ---- | ---- | ||
turn_on_led_using_webpage.1783620038.txt.gz · Last modified: by walkeradmin
