Ramji Patel
Published © CC BY

Getting started With Raspberry Pi Pico W

Start Physical Computing With Raspberry Pi Pico W and Micropython

BeginnerProtip2 hours505
Getting started With Raspberry Pi Pico W

Things used in this project

Hardware components

Raspberry Pi Pico W
Raspberry Pi Pico W
×1
Raspberry Pi Pico USB Cable
Raspberry Pi Pico USB Cable
×1
Solderless Breadboard Full Size
Solderless Breadboard Full Size
×1

Software apps and online services

Thonny

Hand tools and fabrication machines

Soldering Station, 110 V
Soldering Station, 110 V
Solder Wire, Lead Free
Solder Wire, Lead Free
Solder Flux, Soldering
Solder Flux, Soldering

Story

Read more

Schematics

Raspberry Pi Pico W schematic diagram pdf

Code

main.py

MicroPython
import machine
import network
import socket
from time import sleep

led = machine.Pin(15, machine.Pin.OUT)

ssid = 'Redmi Note 10'
password = '12345678'

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

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

def webpage(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>
      </body>
      </html>
      """
  return str(html)

def serve(connection):
  #Start a web server
  state = 'OFF'
  led.off()
  while True:
    client = connection.accept()[0]
    request = client.recv(1024)
    request = str(request)
    try:
      request = request.split()[1]
    except IndexError:
      pass
    if request == '/lighton?':
      led.on()
      state = 'ON'
    elif request =='/lightoff?':
      led.off()
      state = 'OFF'
    html = webpage(state)
    client.send(html)
    client.close()

try:
  ip = connect()
  connection = open_socket(ip)
  serve(connection)
except KeyboardInterrupt:
  machine.reset()

Credits

Ramji Patel
26 projects • 18 followers
Myself Ramji Patel. I am an Engineering student and pursuing my B-Tech from Institute of engineering and rural Technology Prayagraj, India.

Comments