In this video lesson we demonstrate a simple client server project on the Raspberry Pi Pico W. The Pico is configures as the server, and your desktop pc or laptop is configures to be the client. You will be running python on your PC. The project requests the user on the PC to specify a desired color. The color is then sent to the Pico, the Server.
For this lesson we are not using the breadvolt power supply, but we will use it in future lessons. For this lesson, you do not need to attach it, and if you do attach it, leave it ‘off’.
Below is the schematic for the Server Side of the project:

For your convenience, this is the code we developed in the video
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# ==================================================================== # DISCLAIMER: # This code is provided as-is for educational and experimental # purposes only. The author makes no representations or warranties of # any kind concerning the safety, suitability, or accuracy of this # code. Use at your own risk. The author assumes no liability for any # damages, system failures, security breaches, or network issues # resulting from the use or implementation of this script. # ==================================================================== import network import usocket as socket import secrets import time greenLED=machine.Pin(18,machine.Pin.OUT) yellowLED=machine.Pin(19,machine.Pin.OUT) redLED=machine.Pin(20,machine.Pin.OUT) # Set up WiFi connection wlan = network.WLAN(network.STA_IF) wlan.active(True) print(secrets.SSID,secrets.PASSWORD) wlan.connect(secrets.SSID,secrets.PASSWORD) # Wait for connection ts=1 while not wlan.isconnected(): time.sleep(ts) print("Connection Completed") print('WiFi connected') print(wlan.ifconfig()) # Set up UDP server server_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) server_socket.bind((wlan.ifconfig()[0], 12345)) print("Server is Up and Listening") print(wlan.ifconfig()[0]) while True: print('Waiting for a request from the client...') # Receive request from client color, client_address = server_socket.recvfrom(1024) color=color.decode() print("Client Request:",color) print("FROM CLIENT",client_address) if (color=="green"): greenLED.on() yellowLED.off() redLED.off() if (color=="yellow"): greenLED.off() yellowLED.on() redLED.off() if (color=="red"): greenLED.off() yellowLED.off() redLED.on() if (color=="off"): greenLED.off() yellowLED.off() redLED.off() # Send data to client data="LED "+color+" executed" server_socket.sendto(data.encode(), client_address) print(f'Sent data to {client_address}') # Optional: Pause for a short period to prevent overwhelming the client time.sleep(ts) |
Remember you must create a secrets.py file, and save it on the Pi Pico in the lib folder. You need to specify YOUR WiFi name and password in the file.
|
1 2 |
SSID="YOUR WIFI NAME" PASSWORD="YOUR WIFI PASSWORD" |
And finally, here is the code to run on the client side on your PC
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# ==================================================================== # DISCLAIMER: # This code is provided as-is for educational and experimental # purposes only. The author makes no representations or warranties of # any kind concerning the safety, suitability, or accuracy of this # code. Use at your own risk. The author assumes no liability for any # damages, system failures, security breaches, or network issues # resulting from the use or implementation of this script. # ==================================================================== import socket import time # Set up UDP client client_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) server_address = ('192.168.88.71', 12345) # Adjust IP address and port as needed while True: # Send request to the server myColor=input("Please Input Your Color (Green, Yellow, Red, Off )") myColor=myColor.lower() client_socket.sendto(myColor.encode(), server_address) # Receive data from the server data, addr = client_socket.recvfrom(1024) print('Received data:', data.decode()) |