Getting Started with Raspberry Pi Pico W and building a LED Webserver using Micropython:


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:

  • Connect the Pico W, which is a Raspberry Pi that is based on a microcontroller, to your laptop using a micro USB cable.
  • By downloading and installing the Thonny Integrated Development Environment.
  • Launch Thonny and navigate Tools>options
  • At the option menu you should see "Interpreter" and "Raspberry Pi Pico" should be among the items to select.
  • Thonky will solder the critical support library to the Pico W and configure it for the upcoming industrial automation application programming.
  • Paste the following file effectively in the thonny ide. This code will understand as an if LEDI've got pico W 1 instant on the pico W .

Moreover, this is what the result will be.

Uploading code to the Pico:

  • Link the Raspberry Pi Pico W at the computer by having a micro USB cable.

  • On the menu bar, Select "File" then select "Save as". Another way is through copy and paste or drag and drop.

  • Select the location in which you want to save your file on Raspberry Pi Pico, and make sure the extension is the file name with a prefix "py" (e.g. the name of the file is "blink.py").

  • On "Thonny" click "Run-menu" then pick "Run current script" or press F5 keys.

  • Thonny will upload the code to the Pico W, and the web server will stop working after the led server starts running.

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:

  • ?Open a browser anywhere which is running on a device connected to the same network through which the Raspberry Pi Pico W is connected.

  • For the ID of the Pico W, professional in the web browser of your linked device. Use our advanced AI to create unique and humanized content for your website. Our AI can read and comprehend your existing content, helping you to add more value and depth to your articles. In this case, for example, my smartphone because it is always with me. You will get the same after executing the code above ? it is displayed on the screen.

  • When you punch the address whether on Android or iOS for the purpose of browsing. It is that you can see a web page with buttons that when pressed the LED will start and stop flashing.

  • Press on the keys to illuminate/pause the LED.

要查看或添加评论,请登录

Jai Prajapati的更多文章

社区洞察

其他会员也浏览了