Getting Started with Raspberry Pi Pico W and building a LED Webserver using Micropython:
Jai Prajapati
BBA Digital Marketing | Passionate about Leveraging Data-Driven Strategies for Business Growth. #digitalmarketing #Seospecilist #certifiedigitalmarketer, #contentcreator
In this blog, we will discuss the basics on Micropython for Raspberry Pico W and will get in depth details of its Wi-Fi operation as well. The excitement doesn't end there, this way you can design any program you like without any data plan or internet connection as you are communicating via your own webserver and ultimately you can control LEDs from anywhere. Alternatively, you can download the ESP32 web server installation or the NodeMCU Webserver processes if you prefer to find examples beyond the Raspberry pi Pico W.
Raspberry Pi Pico W:
The Raspberry Pi Foundation has a newbie that already has the RP2040 chip called the Pico W microcontroller. They priced it at 6$. With the Pico W WiFi module, it is feasible to use this module for different IoT projects. The Pico W offered an 802.11n WiFi radio. Create your own personalized AI training program and gain an edge in the job market
Working flawlessly as a plug and play option for projects that one might have had been built with its earlier sibling i.e. the Pico, which facilitates built-in WiFi and Bluetooth.
The most novel format provides Bluetooth support, but sadly, it only allows the c/c++ programming language. Though there is a development requirement of micropython on pico W(no need to cover Bluetooth here as this tutorial will only cover Wi-Fi), the pico W's firmware can be updated by a programming language such as C/C++, JavaScript and Rust.
Circuit Diagram:
Below is the circuit diagram for IoT based Raspberry Pi controlled LED: Below is the circuit diagram for IoT based Raspberry Pi controlled LED:
Setting up Raspberry Pi Pico W for Micropython:
Moreover, this is what the result will be.
Uploading code to the Pico:
Writing Micropython code for connecting to Wi-Fi:
# Writing Micropython code for connecting to Wi-Fi
# Electro Global- Innovate with us
import network
import time
# Electro Global- Innovate with us
def connect_to_wifi(ssid, password):
????wlan = network.WLAN(network.STA_IF)
????wlan.active(True)
????if not wlan.isconnected():
????????print('Connecting to WiFi...')
????????wlan.connect(ssid, password)
????????while not wlan.isconnected():
????????????pass
????print('Connected to WiFi')
????print('Network config:', wlan.ifconfig())
# Electro Global- Innovate with us
# Example usage:
# Replace 'your_wifi_ssid' and 'your_wifi_password' with your actual Wi-Fi credentials
connect_to_wifi('your_wifi_ssid', 'your_wifi_password')
Writing Micropython Code for LED Webserver:
# Writing Micropython Code for LED Webserver
# Electro Global- Innovate with us
import network
import socket
import machine
# Electro Global- Innovate with us
led = machine.Pin(2, machine.Pin.OUT)
def web_page():
领英推荐
????html = """
?
Control LED
????LED ON
????LED OFF
??
"""
????return html
# Electro Global- Innovate with us
def main():
????wlan = network.WLAN(network.STA_IF)
????wlan.active(True)
????wlan.connect('Your_WiFi_SSID', 'Your_WiFi_Password')
????while not wlan.isconnected():
????????pass
????print('Connected to WiFi')
????s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
????s.bind(('', 80))
????s.listen(5)
????print('Listening on port 80...')
????while True:
????????conn, addr = s.accept()
????????print('Got a connection from %s' % str(addr))
????????request = conn.recv(1024)
????????request = str(request)
????????print('Content = %s' % request)
????????led_on = request.find('/?LED=ON')
????????led_off = request.find('/?LED=OFF')
????????if led_on == 6:
????????????print('LED ON')
????????????led.value(1)
????????if led_off == 6:
????????????print('LED OFF')
????????????led.value(0)
????????response = web_page()
????????conn.send('HTTP/1.1 200 OK\n')
????????conn.send('Content-Type: text/html\n')
????????conn.send('Connection: close\n\n')
????????conn.sendall(response)
????????conn.close()
# Electro Global- Innovate with us
mai
()
Test the Web Server: