Robotics Python
Expert
60 mins
Teacher/Student led
145 points
What you need:
Chromebook/Laptop/PC

IoT Web-Controlled LED Light

In this lesson, you will create a web page to control the LED light on your Raspberry Pi Pico using MicroPython and Thonny editor.
Learning Goals Learning Outcomes Teacher Notes

Live Class Feed

This is a live feed of the latest activity by your students on this lesson. It will update in real-time as they work on the lesson.
Load previous activity

    1 - Review the Completed Code

    Take a moment to review the completed code provided above. This is the code you will be working with throughout the lesson.

    2 - Import Required Libraries

    Start by importing the necessary libraries for this project. Add the following lines of code at the beginning of your script in Thonny editor:

    import network
    import socket
    from time import sleep
    from picozero import pico_temp_sensor, pico_led
    import machine

    3 - Connect to WiFi

    Replace 'WIFI_SSID' and 'WIFI_PASSWORD' with your WiFi network's SSID and password. Then, add the following code to create a function called 'connect' that connects your Raspberry Pi Pico to the WiFi network:

    ssid = 'WIFI_SSID'
    password = 'WIFI_PASSWORD'
    
    def connect():
        #Connect to WLAN
        wlan = network.WLAN(network.STA_IF)
        wlan.active(True)
        wlan.connect(ssid, password)
        while wlan.isconnected() == False:
            print('Waiting for connection...')
            sleep(1)
        ip = wlan.ifconfig()[0]
        print(f'Connected on {ip}')
        return ip

    4 - Create Socket

    Add the following code to create a function called 'open_socket' that opens a socket for your Raspberry Pi Pico:

    def open_socket(ip):
        # Open a socket
        address = (ip, 80)
        connection = socket.socket()
        connection.bind(address)
        connection.listen(1)
        print(connection)
        return connection

    5 - Create Web Page

    Create a function called 'webpage' that generates the HTML code for your web page. This page will have buttons to turn the LED light on and off, and display the current LED state and temperature:

    def webpage(temperature, state):
        #Template HTML
        html = f"""
                <!DOCTYPE html>
                <html>
                <form action="./lighton">
                <input type="submit" value="Light on" />
                </form>
                <form action="./lightoff">
                <input type="submit" value="Light off" />
                </form>
                <p>LED is {state}</p>
                <p>Temperature is {temperature}</p>
                </body>
                </html>
                """
        return str(html)

    Unlock the Full Learning Experience

    Get ready to embark on an incredible learning journey! Get access to this lesson and hundreds more in our Digital Skills Curriculum.

    Copyright Notice
    This lesson is copyright of Coding Ireland. Unauthorised use, copying or distribution is not allowed.
    🍪 Our website uses cookies to make your browsing experience better. By using our website you agree to our use of cookies. Learn more