Robotics & Engineering Python
Expert
60 mins
Teacher/Student led
+145 XP
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.

Teacher Class Feed

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
    123learn · Online learning platform

    Unlock the full learning experience

    You're previewing this lesson. Get full access to this lesson and hundreds more — each one ready to teach, with interactive activities, printable resources and pupil progress tracking built in.

    Hundreds of curriculum-aligned lessons
    Interactive activities in every lesson
    Printable resources & progress tracking
    Copyright Notice
    This lesson is copyright of Coding Ireland 2017 - 2026. 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